init_notebook_mode(connected=False) and pio.renderers.default='notebook' should not include full plotly.js on each call, but should check if already loaded
See original GitHub issueThe current js generated by init_notebook_mode(connected=False) is naive: it includes a require.undef(“plotly”) to remove plotly and the full plotlyjs code to create it again. This adds 6MB for each call to init_notebook_mode to the notebook size. It also makes opening the notebook slower.
Including it once should be enough.
def iplot_my_thing():
init_notebook_mode(connected=False)
xs = [0, 1, 2, 3, 4]
ys = [4, 3, 2, 1, 0]
trace = go.Scatter(
x = xs,
y = ys,
mode = 'markers'
)
data = [trace]
plotly.offline.iplot(data)
Now just call iplot_my_thing twice, in two different jupyter cells. Save the notebook after each call and check its size.
The code shown wraps init_notebook_mode and iplot. This is necessary so calls to iplot_my_thing work under all circumstances, including users that load a notebook without restarting the kernel and not running the cell containing init_notebook_mode.
If this js fix is implemented, plotly.offline.iplot could just call init_notebook_mode directly and init_notebook_mode would not be needed in the public plotly API which would be a better UX.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (2 by maintainers)
Top GitHub Comments
Hi @ociule,
Thanks for the report. This behavior should be possible with the new renderers subsystem that was recently merged in 3.8.0. See https://github.com/plotly/plotly.py/pull/1474 for more info. Note this is still considered somewhat experimental and isn’t documented yet. That will wait until version 4.0.
Basically, you would set the default renderer to
'notebook'
once at the top of the notebook. This doesn’t immediately cause plotly.js to be loaded in the notebook, that will happen lazily (and only once) the first time the figure is displayed.Something like
The nice thing about this approach is that you can completely change rendering context without modifying
my_thing
. If you changepio.renderers.default = 'jupyterlab'
for use in JupyterLab then plotly.js won’t be loaded into the notebook at all.Does this work for your usecase?
Hi @ociule,
Yeah, the only difference between
pio.renderers.default = 'notebook'
andinit_notebook_mode(connected=False)
is that the plotly.js bundle is loaded into the notebook lazily, the first time show is called after setting the default renderer.You’re right that it’s not really possible, as far as I’m aware, to check whether plotly.js is loaded into the browser from Python. In addition to the
notebook_connected
workflow, here are a couple of options/thoughts.@jupyterlab/plotly-extension
extension and don’t callinit_notebook_mode
at all. In this case, plotly.js is never loaded into the notebook, instead plotly.js is made available in the JupyterLab extension.FigureWidget
objects. https://plot.ly/python/figurewidget/. This works in either the classic notebook or JupyterLab and also does note load plotly.js directly into the notebook, because plotly.js is probided by theplotlywidget
extension.iframe
renderer. See https://community.plot.ly/t/plotly-notebook-very-slow-render-with-many-graphs/16861/7. This works by saving figures as separate html files and displaying them in the notebook using iframes. So again, plotly.js is not loaded into the notebook.