2018-06-02 17:55:12 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-10-01 21:16:39 +02:00
|
|
|
"bytes"
|
2018-06-02 17:55:12 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2018-10-01 21:16:39 +02:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2018-06-02 17:55:12 +02:00
|
|
|
r "reflect"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/cosmos72/gomacro/imports"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Support an interface similar - but not identical - to the IPython (canonical Jupyter kernel).
|
|
|
|
// See http://ipython.readthedocs.io/en/stable/api/generated/IPython.display.html#IPython.display.display
|
|
|
|
// for a good overview of the support types. Note: This is missing _repr_markdown_ and _repr_javascript_.
|
|
|
|
|
|
|
|
const (
|
2018-06-04 21:55:35 +02:00
|
|
|
MIMETypeHTML = "text/html"
|
|
|
|
MIMETypeJavaScript = "application/javascript"
|
|
|
|
MIMETypeJPEG = "image/jpeg"
|
|
|
|
MIMETypeJSON = "application/json"
|
|
|
|
MIMETypeLatex = "text/latex"
|
|
|
|
MIMETypeMarkdown = "text/markdown"
|
|
|
|
MIMETypePNG = "image/png"
|
|
|
|
MIMETypePDF = "application/pdf"
|
|
|
|
MIMETypeSVG = "image/svg+xml"
|
2018-06-02 17:55:12 +02:00
|
|
|
)
|
|
|
|
|
2018-06-04 21:55:35 +02:00
|
|
|
// injected as placeholder in the interpreter, it's then replaced at runtime
|
|
|
|
// by a closure that knows how to talk with Jupyter
|
2018-06-10 23:09:07 +02:00
|
|
|
func stubDisplay(Data) error {
|
|
|
|
return errors.New("cannot display: connection with Jupyter not available")
|
2018-06-04 21:55:35 +02:00
|
|
|
}
|
|
|
|
|
2018-06-02 17:55:12 +02:00
|
|
|
// TODO handle the metadata
|
|
|
|
|
2018-10-01 21:16:39 +02:00
|
|
|
func read(data interface{}) ([]byte, string) {
|
|
|
|
var b []byte
|
|
|
|
var s string
|
|
|
|
switch x := data.(type) {
|
|
|
|
case string:
|
|
|
|
s = x
|
|
|
|
case []byte:
|
|
|
|
b = x
|
|
|
|
case io.Reader:
|
|
|
|
bb, err := ioutil.ReadAll(x)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
b = bb
|
|
|
|
case io.WriterTo:
|
|
|
|
var buf bytes.Buffer
|
|
|
|
x.WriteTo(&buf)
|
|
|
|
b = buf.Bytes()
|
|
|
|
default:
|
|
|
|
panic(errors.New(fmt.Sprintf("unsupported type, cannot display: expecting string, []byte, io.Reader or io.WriterTo, found %T", data)))
|
|
|
|
}
|
|
|
|
if len(s) == 0 {
|
|
|
|
s = fmt.Sprint(data)
|
|
|
|
}
|
|
|
|
return b, s
|
|
|
|
}
|
|
|
|
|
|
|
|
func Any(mimeType string, data interface{}) Data {
|
|
|
|
b, s := read(data)
|
|
|
|
if len(mimeType) == 0 {
|
|
|
|
mimeType = http.DetectContentType(b)
|
|
|
|
}
|
|
|
|
d := Data{
|
|
|
|
Data: BundledMIMEData{
|
|
|
|
"text/plain": s,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if mimeType != "text/plain" {
|
|
|
|
d.Data[mimeType] = b
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2018-06-10 23:09:07 +02:00
|
|
|
func MakeData(mimeType string, data interface{}) Data {
|
2018-10-01 21:16:39 +02:00
|
|
|
d := Data{
|
2018-06-04 21:55:35 +02:00
|
|
|
Data: BundledMIMEData{
|
2018-10-01 21:16:39 +02:00
|
|
|
mimeType: data,
|
2018-06-04 21:55:35 +02:00
|
|
|
},
|
|
|
|
}
|
2018-10-01 21:16:39 +02:00
|
|
|
if mimeType != "text/plain" {
|
|
|
|
d.Data["text/plain"] = fmt.Sprint(data)
|
|
|
|
}
|
|
|
|
return d
|
2018-06-02 17:55:12 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:09:07 +02:00
|
|
|
func MakeData3(mimeType string, plaintext string, data interface{}) Data {
|
|
|
|
return Data{
|
2018-06-04 21:55:35 +02:00
|
|
|
Data: BundledMIMEData{
|
|
|
|
"text/plain": plaintext,
|
2018-06-02 17:55:12 +02:00
|
|
|
mimeType: data,
|
2018-06-04 21:55:35 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-10 23:09:07 +02:00
|
|
|
func Bytes(mimeType string, bytes []byte) Data {
|
2018-10-01 21:16:39 +02:00
|
|
|
if len(mimeType) == 0 {
|
|
|
|
mimeType = http.DetectContentType(bytes)
|
|
|
|
}
|
2018-06-10 23:09:07 +02:00
|
|
|
return MakeData3(mimeType, mimeType, bytes)
|
2018-06-02 17:55:12 +02:00
|
|
|
}
|
|
|
|
|
2018-10-01 21:16:39 +02:00
|
|
|
func File(mimeType string, path string) Data {
|
|
|
|
bytes, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return Bytes(mimeType, bytes)
|
|
|
|
}
|
|
|
|
|
2018-06-10 23:09:07 +02:00
|
|
|
func HTML(html string) Data {
|
2018-10-01 21:16:39 +02:00
|
|
|
return String(MIMETypeHTML, html)
|
2018-06-02 17:55:12 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:09:07 +02:00
|
|
|
func JSON(json map[string]interface{}) Data {
|
|
|
|
return MakeData(MIMETypeJSON, json)
|
2018-06-02 17:55:12 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:09:07 +02:00
|
|
|
func JavaScript(javascript string) Data {
|
2018-10-01 21:16:39 +02:00
|
|
|
return String(MIMETypeJavaScript, javascript)
|
2018-06-02 17:55:12 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:09:07 +02:00
|
|
|
func JPEG(jpeg []byte) Data {
|
2018-10-01 21:16:39 +02:00
|
|
|
return Bytes(MIMETypeJPEG, jpeg)
|
2018-06-02 17:55:12 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:09:07 +02:00
|
|
|
func Latex(latex string) Data {
|
|
|
|
return MakeData3(MIMETypeLatex, latex, "$"+strings.Trim(latex, "$")+"$")
|
2018-06-02 17:55:12 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:09:07 +02:00
|
|
|
func Markdown(markdown string) Data {
|
2018-10-01 21:16:39 +02:00
|
|
|
return String(MIMETypeMarkdown, markdown)
|
2018-06-02 17:55:12 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:09:07 +02:00
|
|
|
func Math(latex string) Data {
|
|
|
|
return MakeData3(MIMETypeLatex, latex, "$$"+strings.Trim(latex, "$")+"$$")
|
2018-06-02 17:55:12 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:09:07 +02:00
|
|
|
func PDF(pdf []byte) Data {
|
2018-10-01 21:16:39 +02:00
|
|
|
return Bytes(MIMETypePDF, pdf)
|
2018-06-02 17:55:12 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:09:07 +02:00
|
|
|
func PNG(png []byte) Data {
|
2018-10-01 21:16:39 +02:00
|
|
|
return Bytes(MIMETypePNG, png)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Reader(mimeType string, r io.Reader) Data {
|
|
|
|
b, err := ioutil.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return Bytes(mimeType, b)
|
2018-06-02 17:55:12 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:09:07 +02:00
|
|
|
func String(mimeType string, s string) Data {
|
2018-10-01 21:16:39 +02:00
|
|
|
if len(mimeType) == 0 {
|
|
|
|
mimeType = http.DetectContentType([]byte(s))
|
|
|
|
}
|
|
|
|
return MakeData3(mimeType, s, s)
|
2018-06-02 17:55:12 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:09:07 +02:00
|
|
|
func SVG(svg string) Data {
|
2018-10-01 21:16:39 +02:00
|
|
|
return String(MIMETypeSVG, svg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func WriterTo(mimeType string, to io.WriterTo) Data {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
_, err := to.WriteTo(&buf)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return Bytes(mimeType, buf.Bytes())
|
2018-06-04 21:55:35 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:09:07 +02:00
|
|
|
// MIME encapsulates the data and metadata into a Data.
|
2018-06-04 21:55:35 +02:00
|
|
|
// The 'data' map is expected to contain at least one {key,value} pair,
|
|
|
|
// with value being a string, []byte or some other JSON serializable representation,
|
|
|
|
// and key equal to the MIME type of such value.
|
|
|
|
// The exact structure of value is determined by what the frontend expects.
|
|
|
|
// Some easier-to-use functions for common formats supported by the Jupyter frontend
|
2018-06-02 17:55:12 +02:00
|
|
|
// are provided by the various functions above.
|
2018-06-10 23:09:07 +02:00
|
|
|
func MIME(data, metadata map[string]interface{}) Data {
|
|
|
|
return Data{data, metadata, nil}
|
2018-06-02 17:55:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// prepare imports.Package for interpreted code
|
|
|
|
var display = imports.Package{
|
|
|
|
Binds: map[string]r.Value{
|
2018-10-01 21:16:39 +02:00
|
|
|
"Any": r.ValueOf(Any),
|
2018-06-04 21:55:35 +02:00
|
|
|
"Bytes": r.ValueOf(Bytes),
|
2018-10-01 21:16:39 +02:00
|
|
|
"File": r.ValueOf(File),
|
2018-06-04 21:55:35 +02:00
|
|
|
"HTML": r.ValueOf(HTML),
|
|
|
|
"Image": r.ValueOf(Image),
|
|
|
|
"JPEG": r.ValueOf(JPEG),
|
|
|
|
"JSON": r.ValueOf(JSON),
|
|
|
|
"JavaScript": r.ValueOf(JavaScript),
|
|
|
|
"Latex": r.ValueOf(Latex),
|
2018-06-10 23:09:07 +02:00
|
|
|
"MakeData": r.ValueOf(MakeData),
|
|
|
|
"MakeData3": r.ValueOf(MakeData3),
|
2018-06-04 21:55:35 +02:00
|
|
|
"Markdown": r.ValueOf(Markdown),
|
|
|
|
"Math": r.ValueOf(Math),
|
|
|
|
"MIME": r.ValueOf(MIME),
|
|
|
|
"MIMETypeHTML": r.ValueOf(MIMETypeHTML),
|
|
|
|
"MIMETypeJavaScript": r.ValueOf(MIMETypeJavaScript),
|
|
|
|
"MIMETypeJPEG": r.ValueOf(MIMETypeJPEG),
|
|
|
|
"MIMETypeJSON": r.ValueOf(MIMETypeJSON),
|
|
|
|
"MIMETypeLatex": r.ValueOf(MIMETypeLatex),
|
|
|
|
"MIMETypeMarkdown": r.ValueOf(MIMETypeMarkdown),
|
|
|
|
"MIMETypePDF": r.ValueOf(MIMETypePDF),
|
|
|
|
"MIMETypePNG": r.ValueOf(MIMETypePNG),
|
|
|
|
"MIMETypeSVG": r.ValueOf(MIMETypeSVG),
|
|
|
|
"PDF": r.ValueOf(PDF),
|
|
|
|
"PNG": r.ValueOf(PNG),
|
2018-10-01 21:16:39 +02:00
|
|
|
"Reader": r.ValueOf(Reader),
|
2018-06-04 21:55:35 +02:00
|
|
|
"String": r.ValueOf(String),
|
|
|
|
"SVG": r.ValueOf(SVG),
|
2018-10-01 21:16:39 +02:00
|
|
|
"WriterTo": r.ValueOf(WriterTo),
|
2018-06-04 21:55:35 +02:00
|
|
|
},
|
|
|
|
Types: map[string]r.Type{
|
|
|
|
"BundledMIMEData": r.TypeOf((*BundledMIMEData)(nil)).Elem(),
|
2018-06-10 23:09:07 +02:00
|
|
|
"Data": r.TypeOf((*Data)(nil)).Elem(),
|
2018-06-02 17:55:12 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-06-04 21:55:35 +02:00
|
|
|
// allow importing "display" and "github.com/gopherdata/gophernotes" packages
|
2018-06-02 17:55:12 +02:00
|
|
|
func init() {
|
|
|
|
imports.Packages["display"] = display
|
|
|
|
imports.Packages["github.com/gopherdata/gophernotes"] = display
|
|
|
|
}
|