Skip to content

Commit

Permalink
fix display(d, m, x) where m expects a json string
Browse files Browse the repository at this point in the history
Fix queryverse/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)
```
  • Loading branch information
cnliao authored Oct 9, 2018
1 parent 7b00bb5 commit fd2a82f
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/inline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,27 @@ 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)
send_ipython(publish[],
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
Expand Down

0 comments on commit fd2a82f

Please sign in to comment.