From fd2a82f0f49a043f10ab448ecee1e5acb32c29aa Mon Sep 17 00:00:00 2001 From: cnliao Date: Tue, 9 Oct 2018 10:48:33 +0800 Subject: [PATCH] fix display(d, m, x) where m expects a json string Fix fredo-dedup/VegaLite.jl#127 After the fix the following works out of the box in new versions of jupyterlab, generating an interactive VegaLite visualization, without additional packages except Ijulia. ``` spec = raw""" { "data": { "values": [ {"a": "A","b": 28}, {"a": "B","b": 55}, {"a": "C","b": 43}, {"a": "D","b": 91}, {"a": "E","b": 81}, {"a": "F","b": 53}, {"a": "G","b": 19}, {"a": "H","b": 87}, {"a": "I","b": 52} ] }, "mark": "point", "encoding": { "x": {"field": "a", "type": "ordinal"}, "y": {"field": "b", "type": "quantitative"} } } """ display("application/vnd.vegalite.v2+json", spec) ``` --- src/inline.jl | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/inline.jl b/src/inline.jl index 39e73cf2..e504ed0e 100644 --- a/src/inline.jl +++ b/src/inline.jl @@ -47,6 +47,19 @@ function limitstringmime(mime::MIME, x) return String(take!(buf)) end +const ipy_mime_json = [ + "application/vnd.dataresource+json", + "application/vnd.vegalite.v2+json", + "application/vnd.vega.v3+json", +] +_display_dict(m::MIME, m_str, x) = Dict(m_str=>limitstringmime(m, x)) +# escape JSON string correctly before send_ipython +for mime in ipy_mime_json + @eval begin + _display_dict(m::MIME{Symbol($mime)}, m_str, x) = Dict(m_str=>JSON.JSONText(limitstringmime(m, x))) + end +end + for mime in ipy_mime @eval begin function display(d::InlineDisplay, ::MIME{Symbol($mime)}, x) @@ -54,7 +67,7 @@ for mime in ipy_mime msg_pub(execute_msg, "display_data", Dict( "metadata" => metadata(x), # optional - "data" => Dict($mime => limitstringmime(MIME($mime), x))))) + "data" => _display_dict(MIME($mime), $mime, x)))) end displayable(d::InlineDisplay, ::MIME{Symbol($mime)}) = true end