plotly.py: Plotly plots not working in Jupyterlab

When I run this example:

import plotly.graph_objects as go
fig = go.Figure(data=go.Bar(y=[2, 3, 1]))
fig.show()

in a Jupyter notebook under jupyter lab I get no output. The output cell expands as if it is going to show output, but it is just blank. I see this error in the browser console:

VM458:2 Uncaught ReferenceError: require is not defined
    at <anonymous>:2:17
    at t.attachWidget (vendors~main.479571ea0f6c7741ac01.js:2)
    at t.insertWidget (vendors~main.479571ea0f6c7741ac01.js:2)
    at C._insertOutput (vendors~main.479571ea0f6c7741ac01.js:2)
    at C.onModelChanged (vendors~main.479571ea0f6c7741ac01.js:2)
    at m (vendors~main.479571ea0f6c7741ac01.js:2)
    at Object.c [as emit] (vendors~main.479571ea0f6c7741ac01.js:2)
    at e.emit (vendors~main.479571ea0f6c7741ac01.js:2)
    at f._onListChanged (vendors~main.479571ea0f6c7741ac01.js:2)
    at m (vendors~main.479571ea0f6c7741ac01.js:2)

Googling I see others that had that problem had bad installs of extensions, I don’t think I have that:

My jupyterlab + extension versions are:

xbk@30c1f8a93eeb:~/xbk_trade_advisory/code/chquery$ jupyter --version
jupyter core     : 4.6.2
jupyter-notebook : 6.0.3
qtconsole        : 4.7.1
ipython          : 7.12.0
ipykernel        : 5.1.4
jupyter client   : 5.3.4
jupyter lab      : 2.0.1
nbconvert        : 5.6.1
ipywidgets       : 7.5.1
nbformat         : 5.0.4
traitlets        : 4.3.3
xbk@30c1f8a93eeb:~/xbk_trade_advisory/code/chquery$ jupyter labextension list
JupyterLab v2.0.1
Known labextensions:
   app dir: /usr/local/share/jupyter/lab
        @jupyter-widgets/jupyterlab-manager v2.0.0  enabled  OK
        jupyterlab-plotly v4.9.0  enabled  OK
        plotlywidget v4.9.0  enabled  OK

Thanks for any help with this.

Matt

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 17
  • Comments: 26 (4 by maintainers)

Most upvoted comments

I managed to make it work in jupyter lab using:

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

I figured out that setting pio.renderers.default to iframe, iframe_connected, or browser works. But not jupyterlab or notebook. When set to notebook an empty space shows up where the plot is supposed to be, when set to jupyterlab, it finishes the computation and nothing happens.

I got it working by installing the “jupyterlab-plotly” extension.

In the anaconda command prompt:

jupyter labextension install jupyterlab-plotly

and then

jupyter lab build

Even though I am using jupyterlab version 3.0.14 and it says in the documents that we don’t need to install extra extensions for jupyterlab. But it seems that’s not the case for me.

now when I use:

import plotly.graph_objects as go fig = go.Figure(data=go.Bar(y=[2, 3, 1])) fig.show()

I get the outupt in my jupyterlab notebook as: image

I’ve got the same issue with latest version of Lab & Dash: https://github.com/plotly/jupyter-dash/issues/57

I too tried all of the above without any joy. The thing that ended up working for me was to reset the application state (after first installing the jupyterlab-plotly lab extension and running jupyter lab build) which can be done through the JupyterLab UI by doing the following:

  • In the left hand navbar click the search icon
  • Search for “Reset application state”
  • Click the first result
  • Wait for the page to refresh and retry building your plot

I’ve attached a GIF of this process for clarity:

reset-state

For reference, my lab extensions look like the following:

(base) jovyan@visualisation-plotly:~$ jupyter labextension list
JupyterLab v2.2.8
Known labextensions:
   app dir: /opt/conda/share/jupyter/lab
        @jupyter-widgets/jupyterlab-manager v2.0.0  enabled  OK
        jupyterlab-plotly v4.14.1  enabled  OK

And my method of actually rendering the plot is to use iplot:

from plotly.offline import download_plotlyjs, init_notebook_mode, iplot, plot

...

iplot(fig, validate=False)

Right, so JupyterLab and Classic Notebook have two completely different rendering paths.

  • fig.show("notebook") works in Classic Notebook and doesn’t require any special installation of extensions, and is not intended to work in JupyterLab.
  • fig.show("jupyterlab") works in JupyterLab and requires the jupyterlab-plotly JupyterLab extension to be installed.

The renderer system auto-detects the notebook vs lab environment and sets the default mode for the current environment, so in principle you shouldn’t need to specify the mode by passing anything into fig.show().

If things aren’t working for you in JupyterLab, then something is wrong with the jupyterlab-plotly extension for some reason. It’s strange that you see no output but also no error in this mode… I’m not quite sure what more to suggest in this case!

I got it working using the steps from here https://github.com/plotly/plotly_express/issues/38 because I had similar js errors in my browser console log.

The exact commands I used were as follows

export NODE_OPTIONS=--max-old-space-size=4096
jupyter labextension list
jupyter labextension install @jupyter-widgets/jupyterlab-manager --no-build
jupyter labextension install plotlywidget@4.9.0 --no-build
jupyter labextension install jupyterlab-plotly@4.9.0 --no-build
jupyter labextension install jupyterlab-chart-editor --no-build
jupyter lab build
unset NODE_OPTIONS
jupyter lab

I’m not sure exactly which extension was causing issues but hope this helps someone else.