@@ -3,7 +3,6 @@ package main
33import (
44 "errors"
55 "fmt"
6- "image"
76 r "reflect"
87 "strings"
98
@@ -14,102 +13,139 @@ import (
1413// See http://ipython.readthedocs.io/en/stable/api/generated/IPython.display.html#IPython.display.display
1514// for a good overview of the support types. Note: This is missing _repr_markdown_ and _repr_javascript_.
1615
17- var globalReceipt * msgReceipt // ugly global variable. any alternative?
18-
1916const (
20- mimeTypeHTML = "text/html"
21- mimeTypeMarkdown = "text/markdown"
22- mimeTypeLatex = "text/latex"
23- mimeTypeSVG = "image/svg+xml"
24- mimeTypePNG = "image/png"
25- mimeTypeJPEG = "image/jpeg"
26- mimeTypeJSON = "application/json"
27- mimeTypeJavaScript = "application/javascript"
17+ MIMETypeHTML = "text/html"
18+ MIMETypeJavaScript = "application/javascript"
19+ MIMETypeJPEG = "image/jpeg"
20+ MIMETypeJSON = "application/json"
21+ MIMETypeLatex = "text/latex"
22+ MIMETypeMarkdown = "text/markdown"
23+ MIMETypePNG = "image/png"
24+ MIMETypePDF = "application/pdf"
25+ MIMETypeSVG = "image/svg+xml"
2826)
2927
28+ // injected as placeholder in the interpreter, it's then replaced at runtime
29+ // by a closure that knows how to talk with Jupyter
30+ func stubDisplay (DisplayData ) error {
31+ return errors .New ("cannot display: connection with Jupiter not registered" )
32+ }
33+
3034// TODO handle the metadata
3135
32- func render2 (mimeType string , data interface {}) error {
33- return render3 (mimeType , fmt .Sprint (data ), data )
36+ func MakeDisplayData (mimeType string , data interface {}) DisplayData {
37+ return DisplayData {
38+ Data : BundledMIMEData {
39+ "text/plain" : fmt .Sprint (data ),
40+ mimeType : data ,
41+ },
42+ }
3443}
3544
36- func render3 (mimeType string , text string , data interface {}) error {
37- receipt := globalReceipt
38- if receipt == nil {
39- return errors .New ("msgReceipt is nil, cannot send display_data message" )
40- }
41- return receipt .PublishDisplayData (
42- bundledMIMEData {
43- "text/plain" : text ,
45+ func MakeDisplayData3 (mimeType string , plaintext string , data interface {}) DisplayData {
46+ return DisplayData {
47+ Data : BundledMIMEData {
48+ "text/plain" : plaintext ,
4449 mimeType : data ,
45- }, make (bundledMIMEData ))
50+ },
51+ }
52+ }
53+
54+ func Bytes (mimeType string , bytes []byte ) DisplayData {
55+ return MakeDisplayData3 (mimeType , mimeType , bytes )
4656}
4757
48- func HTML (html string ) error {
49- return render2 ( mimeTypeHTML , html )
58+ func HTML (html string ) DisplayData {
59+ return MakeDisplayData ( MIMETypeHTML , html )
5060}
5161
52- func Markdown ( markdown string ) error {
53- return render2 ( mimeTypeMarkdown , markdown )
62+ func JSON ( json map [ string ] interface {}) DisplayData {
63+ return MakeDisplayData ( MIMETypeJSON , json )
5464}
5565
56- func SVG ( svg string ) error {
57- return render2 ( mimeTypeSVG , svg )
66+ func JavaScript ( javascript string ) DisplayData {
67+ return MakeDisplayData ( MIMETypeJavaScript , javascript )
5868}
5969
60- func PNG ( png []byte ) error {
61- return render3 ( mimeTypePNG , "{png- image} " , png ) // []byte are encoded as base64 by the marshaller
70+ func JPEG ( jpeg []byte ) DisplayData {
71+ return MakeDisplayData3 ( MIMETypeJPEG , "jpeg image" , jpeg ) // []byte are encoded as base64 by the marshaller
6272}
6373
64- func JPEG ( jpeg [] byte ) error {
65- return render3 ( mimeTypeJPEG , "{jpeg-image}" , jpeg ) // []byte are encoded as base64 by the marshaller
74+ func Latex ( latex string ) DisplayData {
75+ return MakeDisplayData3 ( MIMETypeLatex , latex , "$" + strings . Trim ( latex , "$" ) + "$" )
6676}
6777
68- func Image ( img image. Image ) error {
69- return publishImage ( img , globalReceipt )
78+ func Markdown ( markdown string ) DisplayData {
79+ return MakeDisplayData ( MIMETypeMarkdown , markdown )
7080}
7181
72- func Math (latex string ) error {
73- return render3 ( mimeTypeLatex , latex , "$$" + strings .Trim (latex , "$" )+ "$$" )
82+ func Math (latex string ) DisplayData {
83+ return MakeDisplayData3 ( MIMETypeLatex , latex , "$$" + strings .Trim (latex , "$" )+ "$$" )
7484}
7585
76- func Latex ( latex string ) error {
77- return render3 ( mimeTypeLatex , latex , "$" + strings . Trim ( latex , "$" ) + "$" )
86+ func PDF ( pdf [] byte ) DisplayData {
87+ return MakeDisplayData3 ( MIMETypePDF , "pdf document" , pdf ) // []byte are encoded as base64 by the marshaller
7888}
7989
80- func JSON ( json map [ string ] interface {}) error {
81- return render2 ( mimeTypeJSON , json )
90+ func PNG ( png [] byte ) DisplayData {
91+ return MakeDisplayData3 ( MIMETypePNG , "png image" , png ) // []byte are encoded as base64 by the marshaller
8292}
8393
84- func JavaScript ( javascript string ) error {
85- return render2 ( mimeTypeJavaScript , javascript )
94+ func String ( mimeType string , s string ) DisplayData {
95+ return MakeDisplayData ( mimeType , s )
8696}
8797
88- // MIME renders the data as a plain MIME bundle. The keys of the map are the MIME type of the
89- // data (value) associated with that key. The data will be some JSON serializable object but the structure is
90- // determined by what the frontend expects. Some easier-to-use formats supported by the Jupyter frontend
98+ func SVG (svg string ) DisplayData {
99+ return MakeDisplayData (MIMETypeSVG , svg )
100+ }
101+
102+ // MIME encapsulates the data and metadata into a DisplayData.
103+ // The 'data' map is expected to contain at least one {key,value} pair,
104+ // with value being a string, []byte or some other JSON serializable representation,
105+ // and key equal to the MIME type of such value.
106+ // The exact structure of value is determined by what the frontend expects.
107+ // Some easier-to-use functions for common formats supported by the Jupyter frontend
91108// are provided by the various functions above.
92- func MIME (data , metadata map [string ]interface {}) error {
93- return globalReceipt . PublishDisplayData ( data , metadata )
109+ func MIME (data , metadata map [string ]interface {}) DisplayData {
110+ return DisplayData { data , metadata , nil }
94111}
95112
96113// prepare imports.Package for interpreted code
97114var display = imports.Package {
98115 Binds : map [string ]r.Value {
99- "HTML" : r .ValueOf (HTML ),
100- "JPEG" : r .ValueOf (JPEG ),
101- "JSON" : r .ValueOf (JSON ),
102- "JavaScript" : r .ValueOf (JavaScript ),
103- "Latex" : r .ValueOf (Latex ),
104- "Markdown" : r .ValueOf (Markdown ),
105- "Math" : r .ValueOf (Math ),
106- "MIME" : r .ValueOf (MIME ),
107- "PNG" : r .ValueOf (PNG ),
108- "SVG" : r .ValueOf (SVG ),
116+ "Bytes" : r .ValueOf (Bytes ),
117+ "HTML" : r .ValueOf (HTML ),
118+ "Image" : r .ValueOf (Image ),
119+ "JPEG" : r .ValueOf (JPEG ),
120+ "JSON" : r .ValueOf (JSON ),
121+ "JavaScript" : r .ValueOf (JavaScript ),
122+ "Latex" : r .ValueOf (Latex ),
123+ "MakeDisplayData" : r .ValueOf (MakeDisplayData ),
124+ "MakeDisplayData3" : r .ValueOf (MakeDisplayData3 ),
125+ "Markdown" : r .ValueOf (Markdown ),
126+ "Math" : r .ValueOf (Math ),
127+ "MIME" : r .ValueOf (MIME ),
128+ "MIMETypeHTML" : r .ValueOf (MIMETypeHTML ),
129+ "MIMETypeJavaScript" : r .ValueOf (MIMETypeJavaScript ),
130+ "MIMETypeJPEG" : r .ValueOf (MIMETypeJPEG ),
131+ "MIMETypeJSON" : r .ValueOf (MIMETypeJSON ),
132+ "MIMETypeLatex" : r .ValueOf (MIMETypeLatex ),
133+ "MIMETypeMarkdown" : r .ValueOf (MIMETypeMarkdown ),
134+ "MIMETypePDF" : r .ValueOf (MIMETypePDF ),
135+ "MIMETypePNG" : r .ValueOf (MIMETypePNG ),
136+ "MIMETypeSVG" : r .ValueOf (MIMETypeSVG ),
137+ "PDF" : r .ValueOf (PDF ),
138+ "PNG" : r .ValueOf (PNG ),
139+ "String" : r .ValueOf (String ),
140+ "SVG" : r .ValueOf (SVG ),
141+ },
142+ Types : map [string ]r.Type {
143+ "BundledMIMEData" : r .TypeOf ((* BundledMIMEData )(nil )).Elem (),
144+ "DisplayData" : r .TypeOf ((* DisplayData )(nil )).Elem (),
109145 },
110146}
111147
112- // allow import of "display" and "github.com/gopherdata/gophernotes" packages
148+ // allow importing "display" and "github.com/gopherdata/gophernotes" packages
113149func init () {
114150 imports .Packages ["display" ] = display
115151 imports .Packages ["github.com/gopherdata/gophernotes" ] = display
0 commit comments