question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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 issue

The 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:open
  • Created 4 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jonmmeasecommented, Apr 22, 2019

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

import plotly.graph_objs as go
import plotly.io as pio
pio.renderers.default = 'notebook'

def my_thing():
    xs = [0, 1, 2, 3, 4]
    ys = [4, 3, 2, 1, 0]
    trace = go.Scatter(
        x = xs,
        y = ys,
        mode = 'markers'
    )
    data = [trace]
    fig = go.Figure(data=data)
    pio.show(fig)

    # First call causes plotly.js to be loaded in the notebook
    my_thing()

    # Subsequent calls don't load plotly.js again
    my_thing()

The nice thing about this approach is that you can completely change rendering context without modifying my_thing. If you change pio.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?

0reactions
jonmmeasecommented, May 16, 2019

Hi @ociule,

Yeah, the only difference between pio.renderers.default = 'notebook' and init_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.

  1. Use JupyterLab and the @jupyterlab/plotly-extension extension and don’t call init_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.
  2. Display figures as 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 the plotlywidget extension.
  3. Use the 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.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Displaying figures in Python - Plotly
The default renderer that you see when you display pio.renderers might be different than what is shown here. This is because plotly.py attempts...
Read more >
Display Plotly plot inside VS code - python - Stack Overflow
the plot opens in my default browser, which is not what i want. Is it either possible to show the figure directly in...
Read more >
[fastpages] Plotly plot doesn't load, JS error in browser - nbdev
The full notebook is here, in case that helps: ... I think there might be some additional steps, and I don't know what...
Read more >
How to use the plotly.offline.init_notebook_mode function in ...
For # now we just do a stacked bar plot. import plotly.offline as offline ... should not be called in normal usage from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found