10
votes

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()
3
Can you share a link to your notebook? Are you using FigureWidget? If you're not, then just figure.show() to display the chart and export to HTML, works without issue for me. - nicolaskruchten
I cannot share the notebook fully, but added a the line of code where I plot. I used figure.show() but it gave the same results, of not displaying the figures when exported to html - guyts
which version of Jupyter and Nbconvert are you using? what's the nbconvert command you're running? this works for me... - nicolaskruchten
nbconvert 5.4.1 and jupyter 1.0.0 (with Anaconda 2019.03) - guyts
I've also received this warning when trying to convert: C:\ProgramData\Anaconda3\lib\site-packages\nbconvert\filters\datatypefilter.py:41: UserWarning: Your element with mimetype(s) dict_keys(['application/vnd.plotly.v1+json']) is not able to be represented. mimetypes=output.keys()) - guyts

3 Answers

14
votes

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.

1
votes

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' 
-3
votes

I agree with @hamiq. With the new version of plotly and Dash, it's a lot easier to use both inside JupyterLab and to work with interactive plots. Here's a link for a tutorial I made that explains how to get started with JupyterLab and JupyterDash. https://youtu.be/QkOKkrKqI-k

I hope this helps everyone with the installation and the set-up.