Save Jupyter Notebook with Plotly Express widgets displaying

Question:

I have a Jupyter notebook (python) where I used plotly express to plot in the notebook for analysis purposes.
I want to share this notebook with non-coders, and have the interactive visuals be available still – but it does not seem to work.

I tried following recommendations made here but even after saving widgets state and using nbconvert, when I open the new HTML file, the visuals are not available.

A sample line of plotting can be seen below:

import plotly_express as px
fig = px.scatter(
    df, 
    x='size', 
    y='size_y', 
    color='clients',
    hover_data=['id'], 
    marginal_y="histogram", 
    marginal_x="histogram"
)
fig.show()
Asked By: guyts

||

Answers:

I was having similar issues but with JupyterLab. Followed the instructions here: https://plot.ly/python/renderers/ .

import plotly.io as pio
pio.renderers.keys()

I had to add the following snippet to my script:

import plotly.io as pio
pio.renderers.default = 'jupyterlab'

Exporting as HTML after that worked for me. You can read “Overriding the default renderer”.

I suppose you would need

pio.renderers.default = 'notebook' 
Answered By: hamiq

After running plotly.offline.init_notebook_mode() in a cell,
you can export the notebook with full interactivity via the file menu:
File --> Export Notebook as... --> Export Notebook to HTML.

Answered By: joelostblom

You can specify the default renderers from plotly with:

    import plotly.io as pio
    pio.renderers.default = 'pdf'

or when displaying the images with:

    fig.show(renderer="pdf")

The 2 choices for you are:

  • ‘notebook’: work well with jupyter notebook;
  • ‘pdf’: perfect when using nbconvert to convert to HTML or LATEX

You can also join the 2 with "notebook+pdf" so you have iterative plots when running the notebook and static images when converting with nbconvert.

Answered By: rgime

I just had a problem that resulted in this error message.

nbconvert/filters/widgetsdatatypefilter.py:69: UserWarning: Your element with mimetype(s) dict_keys(['application/vnd.plotly.v1+json']) is not able to be represented.
  warn("Your element with mimetype(s) {mimetypes}"

Google brought me here, but I could not find a solution from the answers above.

I’m sharing the setup which caused my problem and the solution.

I was running the notebook in VS Code and then on the command line using: jupyter nbconvert

To solve it, I had to start jupyter lab and then run it and save it before running nbconvert.

Answered By: haugstve