HCADatalab / IClojure

Clojure Jupyter kernel based on the Unrepl Protocol
Eclipse Public License 1.0
78 stars 6 forks source link

rendering vega-lite specs #2

Closed keesterbrugge closed 5 years ago

keesterbrugge commented 5 years ago

Hi,

Thanks for your work on this kernel.

Currently I mostly use ggplot in R for visualisations. I would love to have vega-lite available to me in a notebook environment with clojure.

In the ipython kernel a vega-lite spec can be rendered inline as follows

from IPython.display import display

display({
    "application/vnd.vegalite.v2+json": {
        "$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"}
        }
    }
}, raw=True)

Does similar functionality exist in iClojure?

cgrand commented 5 years ago
#unrepl/mime {:content-type "application/vnd.vegalite.v2+json" :content {
        "$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"}
        }}}

should work

keesterbrugge commented 5 years ago

Hi Christophe,

Thanks! This indeed works, see the screenshot below.

screenshot 2019-01-14 at 13 48 45

However, if I want to use a vega-lite spec that's bound to a symbol – vl-spec in this case – I don't know how to use #unrepl/mime anymore

screenshot 2019-01-14 at 13 47 52

and if I want to plot the result of a function call it seems to turn into an infinite loop

screenshot 2019-01-14 at 13 47 33

Is this expected behaviour?

I would like to be able to create my vega-lite specs with a library like oz and/or read in json files and then plot them inline with unrepl/mime

I would appreciate suggestions on how to do that.

Thanks again for your work!

cgrand commented 5 years ago

Actually you are breaking grounds. Let's me explain how it works right now:

  1. If the returned value is a #unrepl/mime tag literal then the payload goes through the MIME facilities of Jupyter Lab
  2. tag literals are literals, not expressions, so lists (or symbols) inside them are not evaluated
  3. up to earlier today there was a bug with "long" (10-ish) lists, it has been fixed on master.

The workaround for 2/ is to do (tagged-literal 'unrepl/mime (my-spec)) but I'm considering evaluating expressions inside literals.

Does it shed enough light on the matter to help you solve the issue?

keesterbrugge commented 5 years ago

Thank for you fast response.

I can confirm this now works

screenshot 2019-01-14 at 18 12 24

This helps a lot

metasoarous commented 5 years ago

@cgrand Thanks for unblocking us! Would vega work more or less the same way with s/vegalite/vega/ applied to the :content-type? Also, I'm assuming v2 is for version here; Do you know if its possible to get a RC version through this :content-type specification?

Thanks again!

cgrand commented 5 years ago

@metasoarous honestly I have no idea. IClojure does nothing special (except edn -> json conversion) so whatever works with IPython.display should work with unrepl/mime.

cgrand commented 5 years ago

@keesterbrugge with the commit above your original code should work (without explicit tagged-literal call)

keesterbrugge commented 5 years ago

You're right

screenshot 2019-01-16 at 12 24 01

Cheers!