Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support of "application/vnd.vegalite.v2+json" #127

Closed
cnliao opened this issue Oct 8, 2018 · 4 comments · May be fixed by JuliaLang/IJulia.jl#756
Closed

support of "application/vnd.vegalite.v2+json" #127

cnliao opened this issue Oct 8, 2018 · 4 comments · May be fixed by JuliaLang/IJulia.jl#756

Comments

@cnliao
Copy link

cnliao commented Oct 8, 2018

The latest Jupyterlab comes with vegalite included, so I imagine that this should work:

spec = raw"""
{
  "$schema": "https://vega.github.io/schema/vega-lite/v2.json",
  "description": "A simple bar chart with embedded data.",
  "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": "bar",
  "encoding": {
    "x": {"field": "a", "type": "ordinal"},
    "y": {"field": "b", "type": "quantitative"}
  }
}
"""
struct A
    s::String
end
function Base.show(io::IO, m::MIME"application/vnd.vegalite.v2+json", x::A)
    print(io, x.s)
end
display("application/vnd.vegalite.v2+json", A(spec)) # or simply `A(spec)`

So vegalite can work in jupyterlab even without (the excellent) VegaLite.jl.
But currently the above code snippet gives a "Javascript Error: undefined" error.
Any thought on how to fix this?

@davidanthoff
Copy link
Member

We support that MIME type by default (see here). So if you run in JupyterLab, we use exactly that mechanism to show specs.

We also support a vl string macro that allows you to type in specs directly, see here.

Let me know if you were thinking of anything else!

@cnliao
Copy link
Author

cnliao commented Oct 9, 2018

Currently the following does not work. I think it's an issue with IJulia and will post a fix soon.

using VegaLite
spec = vl"""
{
  "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": "bar",
  "encoding": {
    "x": {"field": "a", "type": "ordinal"},
    "y": {"field": "b", "type": "quantitative"}
  }
}
"""
display("application/vnd.vegalite.v2+json", spec)

display(spec) already works, but IJulia will render and send every supported mime type in this case (a.k.a. additional png and svg). I want an interactive app to be more responsive so would like to avoid that overhead.

Another use case is to use already constructed vega(lite) json specs directly. The @vl macro is excellent at constructing such json strings.

@davidanthoff
Copy link
Member

Yeah, that seems more on the IJulia side of things. Here we just implement the various show methods, and then it is really up to the client (IJulia) which ones to call.

If you have your spec as a string already, you can turn it into a vega-lite spec with VLSpec{:plot}(JSON.parse(content)). We should probably make the easier, maybe by providing a constructor that just takes a String, so that one doesn't have to worry about the JSON parsing... There will of course be a small overhead of turning things into a Dict etc tree again...

Btw., there is also full support for vega (not -lite) specs in this package, it is just all not exported and documented :)

cnliao added a commit to cnliao/IJulia.jl that referenced this issue Oct 9, 2018
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)
```
@cnliao
Copy link
Author

cnliao commented Oct 9, 2018

(Hopefully) fixed. Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants