Plots.jl: Support for Jupyter lab with the plotly backend

Created on 7 Feb 2018  路  9Comments  路  Source: JuliaPlots/Plots.jl

The following is fine in jupyter notebook, but will not display in jupyter lab (at least as far as I can tell):

using Plots
plotly()
plot(rand(3))

Apparently this requires a a line of code to register a MIME type.

Most helpful comment

Have you installed the jupyterlab plotly mimerender extension?

https://github.com/jupyterlab/jupyter-renderers/tree/master/packages/plotly-extension

Note: this happens _outside_ anything Julia related

All 9 comments

@sglyon can you explain a little more how you did it?

Sure thing.

jupyterlab and nteract support custom mime types. The one for plotly.js is application/vnd.plotly.v1+json. Took hook into that we just need to overload IJulia.display_dict and return a dict that maps from that mimetype to a valid json structure for plotly.js to inject

It looks like this (see here and/or here):

function IJulia.display_dict(p::JupyterPlot)
    Dict("text/html" => html_body(p),
         "application/vnd.plotly.v1+json" => JSON.lower(p))
end

EDIT

Note that you _must_ use a Dict, and not a json string, for the value associated with the application/vnd.plotly.v1+json mimetype. Otherwise jupyterlab and nteract will fail to recognize it. See here for more info: https://github.com/JuliaLang/IJulia.jl/pull/621/files

I should note that instead of overloading the (unexported) IJulia.display_dict you can instead do what David A. did and teach IJulia about that mimetype. Then you need to define Base.show(::IO, ::MIME"my_mime", ::MyType)

See here for IJulia bit: https://github.com/JuliaLang/IJulia.jl/pull/611

and here for Base.show implementaiton: https://github.com/fredo-dedup/VegaLite.jl/blob/d19ca9a33a67170601b6071f41881814ad424823/src/show.jl#L15-L17

As custom mime types are the future of customizing display in jupyter land (lab/nteract), I think we should make some space in IJulia that allows other packages to _register_ custom mimetypes. This would mean that David wouldn't have had to change IJulia source code itself to make it aware of the Vega mimetype.

I have thoughts for one way to extend IJulia in this way, but haven't had the time to open an RFC PR

That sounds a lot better 馃憤

I'm still having this issue and I suspect that I'm just not using the right versions of things. Is there a way to force an upgrade to the correct versions of all libraries involved?

julia> foreach(x->println("$x   :   $(Pkg.installed(x))"), ("Plots", "PlotlyJS"))
Plots   :   0.17.0
PlotlyJS   :   0.10.2

I'm currently doing some work with jupyter-notebook, but would enjoy switching over to jupyter-lab.

julia> Pkg.status.(split("Plots PlotlyJS"))
 - Plots                         0.17.0+            master
 - PlotlyJS                      0.10.2

Looks like you're fine on the version side. Could it be your IJulia version that is too old?

@mkborregaard How old is too old?

julia> Pkg.status.(split("Plots PlotlyJS IJulia"))
 - Plots                         0.17.0
 - PlotlyJS                      0.10.2
 - IJulia                        1.8.0

Have you installed the jupyterlab plotly mimerender extension?

https://github.com/jupyterlab/jupyter-renderers/tree/master/packages/plotly-extension

Note: this happens _outside_ anything Julia related

I must confess; I didn't realize that was required. It's working great for me after installing the jupyter plotly-extension.

As an aside, I'm very thankful to be part of a community with such helpful and kind people. :-)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PallHaraldsson picture PallHaraldsson  路  4Comments

dancsi picture dancsi  路  4Comments

cortner picture cortner  路  4Comments

Cody-G picture Cody-G  路  4Comments

apalugniok picture apalugniok  路  3Comments