HTML export from Jupyter Notebook for offline viewing

When I export HTML from Jupyter Notebook, the plot.ly figures only show if there is an internet connection. Otherwise, I get the following error message:

Uncaught ReferenceError: require is not defined

Is this intended behavior? I would have imagined that a static HTML file is supposed to be able to function on its own.

Hi @realtime, which version of plotly are you using? And how do you export to HTML? (with fig.write_html('tmp.html')?) Could you please share a reproducible code example using dummy data so that we can try to reproduce or diagnose the problem?

I use version 4.3.0
I create the export via the Jupyter Notebook menu: File - Download as - HTML
The following trivial code reproduces the issue for me:

import plotly.express as px
iris = px.data.iris()
px.scatter(iris, x="sepal_width", y="sepal_length", color="petal_length")

Thanks for your support.

Are you using Jupyter notebook or Jupyter lab? Your problem seems quite close to https://github.com/plotly/plotly_express/issues/38 which was fixed following the instructions in https://github.com/plotly/plotly.py/blob/master/README.md#jupyterlab-support-python-35

It’s Notebook. For me, Lab is too fragile for daily use. :wink:

OK. It could also be related to a template issue as decribed in https://github.com/plotly/plotly.py/issues/1033#issuecomment-402894617. Could you maybe do more diagnosis using nbconvert instead of the jupyter menu as descrived in the former issue? Do you confirm that you want to download the whole notebook and not only the figure? Because you can also save a figure with fig.write_html.

jupyter nbconvert --to html ...

shows exactly the same behavior, the output files are identical. And, yes, I want the full notebook.

I also tried fig.write_html(...) as you suggested, and this indeed works flawlessly. But it is not what I want.

With the nbconvert command, did you try the different templates as in https://github.com/plotly/plotly.py/issues/1033 (--template full)?

--full gives the identical HTML output as before, and with --basic the figure does not work at all, even online. I don’t mind the latter since the overall file renders quite ugly anyway and thus I strongly prefer --full. :wink:

I am still not sure where we are here:

Is this a bug or a missing feature? So do I file a bug report or a feature request?

Oh it’s definitely a bug, I just don’t know whether it’s more a plotly or a jupyter/nbconvert bug. Anyway since we’re at the end of the chain you might as well open an issue on https://github.com/plotly/plotly.py/issues/new. Thanks!

I opened the issue report https://github.com/plotly/plotly.py/issues/1983

Oh I had completely missed the fact that the problem only happens when there is no Internet connection. Did you try to change the renderer to notebook (see https://plot.ly/python/renderers/#interactive-renderers)

import plotly.io as pio
pio.renderers.default = "notebook"

pio.renderers.default = "notebook" does not change anything. Same issue, same error message.

Shouldn’t this be the default renderer for Jupyter Notebook anyway since it is the most universal? The description in the documentation is quite fuzzy:

plotly.py attempts to autodetect an appropriate renderer at startup.