Plotly.py: Plotly plots not working in Jupyterlab

Created on 28 Jul 2020  路  20Comments  路  Source: plotly/plotly.py

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

Most helpful comment

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.

All 20 comments

I have the same issues, with the same browser console error.

$ jupyter --version
jupyter core     : 4.6.3
jupyter-notebook : 6.1.3
qtconsole        : not installed
ipython          : 7.8.0
ipykernel        : 5.3.4
jupyter client   : 6.1.6
jupyter lab      : 2.2.5
nbconvert        : 5.6.1
ipywidgets       : 7.5.0
nbformat         : 5.0.7
traitlets        : 4.3.2
JupyterLab v2.2.5
Known labextensions:
   app dir: /local/mnt/workspace/user/.virtualenvs/nnpy/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

Figures show up fine in Jupyter Notebook however. Any help would be appreciated.

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 [email protected] --no-build
jupyter labextension install [email protected] --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.

This is the same issue as #2742 I suspect.

I have the same problem with the same error in the brower javascript console both in Safari and Chrome. The solution by @shrestha-pranav did not fix it for me. Plotly works in jupyter notebook.

$ jupyter --version
jupyter core     : 4.6.3
jupyter-notebook : 6.1.3
qtconsole        : not installed
ipython          : 7.18.1
ipykernel        : 5.3.4
jupyter client   : 6.1.7
jupyter lab      : 2.2.6
nbconvert        : 5.6.1
ipywidgets       : 7.5.1
nbformat         : 5.0.7
traitlets        : 4.3.3
$ jupyter labextension list
JupyterLab v2.2.6
Known labextensions:
   app dir: /Users/janhomann/opt/anaconda3/envs/JupyterLabTest/share/jupyter/lab
        @jupyter-widgets/jupyterlab-manager v2.0.0  enabled  OK
        jupyterlab-chart-editor v4.9.0  enabled  OK
        jupyterlab-dash v0.3.0  enabled  OK
        jupyterlab-plotly v4.9.0  enabled  OK
        plotlywidget v4.9.0  enabled  OK

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.

In some cases I've found that just running jupyter lab build on the command line fixes this issue, even though in principle it shouldn't. Worth a try!

Either way, if folks could share their NodeJS version as well, that might help pinpoint a common cause for these issues.

Hi Nicolas. jupyter lab build didn't help. And my node.js version is v14.9.0 as far as i can see (typing node -v in the terminal)

Ok, thanks for that version! What error message appears in the JS console when you render a plot and see no output?

Uncaught ReferenceError: require is not defined
    at <anonymous>:2:17
    at t.attachWidget (vendors~main.b10336539d1031afa7d6.js:2)
    at t.insertWidget (vendors~main.b10336539d1031afa7d6.js:2)
    at _insertOutput (vendors~main.b10336539d1031afa7d6.js:2)
    at _.onModelChanged (vendors~main.b10336539d1031afa7d6.js:2)
    at v (vendors~main.b10336539d1031afa7d6.js:2)
    at Object.u [as emit] (vendors~main.b10336539d1031afa7d6.js:2)
    at e.emit (vendors~main.b10336539d1031afa7d6.js:2)
    at u._onListChanged (vendors~main.b10336539d1031afa7d6.js:2)
    at v (vendors~main.b10336539d1031afa7d6.js:2)

This is the renderer configuration that it has.

>>> import plotly.io as pio
>>> pio.renderers
Renderers configuration
-----------------------
    Default renderer: 'plotly_mimetype+notebook'
    Available renderers:
........

when i do
fig.show(renderer = 'jupyterlab')

nothing happens and no error shows up in the java script console.

when i do
fig.show(renderer = 'notebook')

i get a blank space and the error shows up in the java script console. So I assume it does the notebook renderer when i don't specify anything.

In chrome i can click on 'require not defined` and this code shows up:

                require(["plotly"], function(Plotly) {
                    window.PLOTLYENV=window.PLOTLYENV || {};

                if (document.getElementById("e7921b12-5927-4af5-a39f-cd49b021efe1")) {
                    Plotly.newPlot(
                        'e7921b12-5927-4af5-a39f-cd49b021efe1',
                        [{"type": "bar", "y": [2, 1, 3]}],
                        {"template": {"data": {"bar": [{"error_x": {"color": "#2a3f5f"}, "error_y": {"color": "#2a3f5f"}, "marker": {"line": {"color": "#E5ECF6", "width": 0.5}}, "type": "bar"}], "barpolar": [{"marker": {"line": {"color": "#E5ECF6", "width": 0.5}}, "type": "barpolar"}], "carpet": [{"aaxis": {"endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f"}, "baxis": {"endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f"}, "type": "carpet"}], "choropleth": [{"colorbar": {"outlinewidth": 0, "ticks": ""}, "type": "choropleth"}], "contour": [{"colorbar": {"outlinewidth": 0, "ticks": ""}, "colorscale": [[0.0, "#0d0887"], [0.1111111111111111, "#46039f"], [0.2222222222222222, "#7201a8"], [0.3333333333333333, "#9c179e"], [0.4444444444444444, "#bd3786"], [0.5555555555555556, "#d8576b"], [0.6666666666666666, "#ed7953"], [0.7777777777777778, "#fb9f3a"], [0.8888888888888888, "#fdca26"], [1.0, "#f0f921"]], "type": "contour"}], "contourcarpet": [{"colorbar": {"outlinewidth": 0, "ticks": ""}, "type": "contourcarpet"}], "heatmap": [{"colorbar": {"outlinewidth": 0, "ticks": ""}, "colorscale": [[0.0, "#0d0887"], [0.1111111111111111, "#46039f"], [0.2222222222222222, "#7201a8"], [0.3333333333333333, "#9c179e"], [0.4444444444444444, "#bd3786"], [0.5555555555555556, "#d8576b"], [0.6666666666666666, "#ed7953"], [0.7777777777777778, "#fb9f3a"], [0.8888888888888888, "#fdca26"], [1.0, "#f0f921"]], "type": "heatmap"}], "heatmapgl": [{"colorbar": {"outlinewidth": 0, "ticks": ""}, "colorscale": [[0.0, "#0d0887"], [0.1111111111111111, "#46039f"], [0.2222222222222222, "#7201a8"], [0.3333333333333333, "#9c179e"], [0.4444444444444444, "#bd3786"], [0.5555555555555556, "#d8576b"], [0.6666666666666666, "#ed7953"], [0.7777777777777778, "#fb9f3a"], [0.8888888888888888, "#fdca26"], [1.0, "#f0f921"]], "type": "heatmapgl"}], "histogram": [{"marker": {"colorbar": {"outlinewidth": 0, "ticks": ""}}, "type": "histogram"}], "histogram2d": [{"colorbar": {"outlinewidth": 0, "ticks": ""}, "colorscale": [[0.0, "#0d0887"], [0.1111111111111111, "#46039f"], [0.2222222222222222, "#7201a8"], [0.3333333333333333, "#9c179e"], [0.4444444444444444, "#bd3786"], [0.5555555555555556, "#d8576b"], [0.6666666666666666, "#ed7953"], [0.7777777777777778, "#fb9f3a"], [0.8888888888888888, "#fdca26"], [1.0, "#f0f921"]], "type": "histogram2d"}], "histogram2dcontour": [{"colorbar": {"outlinewidth": 0, "ticks": ""}, "colorscale": [[0.0, "#0d0887"], [0.1111111111111111, "#46039f"], [0.2222222222222222, "#7201a8"], [0.3333333333333333, "#9c179e"], [0.4444444444444444, "#bd3786"], [0.5555555555555556, "#d8576b"], [0.6666666666666666, "#ed7953"], [0.7777777777777778, "#fb9f3a"], [0.8888888888888888, "#fdca26"], [1.0, "#f0f921"]], "type": "histogram2dcontour"}], "mesh3d": [{"colorbar": {"outlinewidth": 0, "ticks": ""}, "type": "mesh3d"}], "parcoords": [{"line": {"colorbar": {"outlinewidth": 0, "ticks": ""}}, "type": "parcoords"}], "pie": [{"automargin": true, "type": "pie"}], "scatter": [{"marker": {"colorbar": {"outlinewidth": 0, "ticks": ""}}, "type": "scatter"}], "scatter3d": [{"line": {"colorbar": {"outlinewidth": 0, "ticks": ""}}, "marker": {"colorbar": {"outlinewidth": 0, "ticks": ""}}, "type": "scatter3d"}], "scattercarpet": [{"marker": {"colorbar": {"outlinewidth": 0, "ticks": ""}}, "type": "scattercarpet"}], "scattergeo": [{"marker": {"colorbar": {"outlinewidth": 0, "ticks": ""}}, "type": "scattergeo"}], "scattergl": [{"marker": {"colorbar": {"outlinewidth": 0, "ticks": ""}}, "type": "scattergl"}], "scattermapbox": [{"marker": {"colorbar": {"outlinewidth": 0, "ticks": ""}}, "type": "scattermapbox"}], "scatterpolar": [{"marker": {"colorbar": {"outlinewidth": 0, "ticks": ""}}, "type": "scatterpolar"}], "scatterpolargl": [{"marker": {"colorbar": {"outlinewidth": 0, "ticks": ""}}, "type": "scatterpolargl"}], "scatterternary": [{"marker": {"colorbar": {"outlinewidth": 0, "ticks": ""}}, "type": "scatterternary"}], "surface": [{"colorbar": {"outlinewidth": 0, "ticks": ""}, "colorscale": [[0.0, "#0d0887"], [0.1111111111111111, "#46039f"], [0.2222222222222222, "#7201a8"], [0.3333333333333333, "#9c179e"], [0.4444444444444444, "#bd3786"], [0.5555555555555556, "#d8576b"], [0.6666666666666666, "#ed7953"], [0.7777777777777778, "#fb9f3a"], [0.8888888888888888, "#fdca26"], [1.0, "#f0f921"]], "type": "surface"}], "table": [{"cells": {"fill": {"color": "#EBF0F8"}, "line": {"color": "white"}}, "header": {"fill": {"color": "#C8D4E3"}, "line": {"color": "white"}}, "type": "table"}]}, "layout": {"annotationdefaults": {"arrowcolor": "#2a3f5f", "arrowhead": 0, "arrowwidth": 1}, "coloraxis": {"colorbar": {"outlinewidth": 0, "ticks": ""}}, "colorscale": {"diverging": [[0, "#8e0152"], [0.1, "#c51b7d"], [0.2, "#de77ae"], [0.3, "#f1b6da"], [0.4, "#fde0ef"], [0.5, "#f7f7f7"], [0.6, "#e6f5d0"], [0.7, "#b8e186"], [0.8, "#7fbc41"], [0.9, "#4d9221"], [1, "#276419"]], "sequential": [[0.0, "#0d0887"], [0.1111111111111111, "#46039f"], [0.2222222222222222, "#7201a8"], [0.3333333333333333, "#9c179e"], [0.4444444444444444, "#bd3786"], [0.5555555555555556, "#d8576b"], [0.6666666666666666, "#ed7953"], [0.7777777777777778, "#fb9f3a"], [0.8888888888888888, "#fdca26"], [1.0, "#f0f921"]], "sequentialminus": [[0.0, "#0d0887"], [0.1111111111111111, "#46039f"], [0.2222222222222222, "#7201a8"], [0.3333333333333333, "#9c179e"], [0.4444444444444444, "#bd3786"], [0.5555555555555556, "#d8576b"], [0.6666666666666666, "#ed7953"], [0.7777777777777778, "#fb9f3a"], [0.8888888888888888, "#fdca26"], [1.0, "#f0f921"]]}, "colorway": ["#636efa", "#EF553B", "#00cc96", "#ab63fa", "#FFA15A", "#19d3f3", "#FF6692", "#B6E880", "#FF97FF", "#FECB52"], "font": {"color": "#2a3f5f"}, "geo": {"bgcolor": "white", "lakecolor": "white", "landcolor": "#E5ECF6", "showlakes": true, "showland": true, "subunitcolor": "white"}, "hoverlabel": {"align": "left"}, "hovermode": "closest", "mapbox": {"style": "light"}, "paper_bgcolor": "white", "plot_bgcolor": "#E5ECF6", "polar": {"angularaxis": {"gridcolor": "white", "linecolor": "white", "ticks": ""}, "bgcolor": "#E5ECF6", "radialaxis": {"gridcolor": "white", "linecolor": "white", "ticks": ""}}, "scene": {"xaxis": {"backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white"}, "yaxis": {"backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white"}, "zaxis": {"backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white"}}, "shapedefaults": {"line": {"color": "#2a3f5f"}}, "ternary": {"aaxis": {"gridcolor": "white", "linecolor": "white", "ticks": ""}, "baxis": {"gridcolor": "white", "linecolor": "white", "ticks": ""}, "bgcolor": "#E5ECF6", "caxis": {"gridcolor": "white", "linecolor": "white", "ticks": ""}}, "title": {"x": 0.05}, "xaxis": {"automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": {"standoff": 15}, "zerolinecolor": "white", "zerolinewidth": 2}, "yaxis": {"automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": {"standoff": 15}, "zerolinecolor": "white", "zerolinewidth": 2}}}, "title": {"text": "A Figure Displayed with the 'svg' Renderer"}},
                        {"responsive": true}
                    ).then(function(){

var gd = document.getElementById('e7921b12-5927-4af5-a39f-cd49b021efe1');
var x = new MutationObserver(function (mutations, observer) {{
        var display = window.getComputedStyle(gd).display;
        if (!display || display === 'none') {{
            console.log([gd, 'removed!']);
            Plotly.purge(gd);
            observer.disconnect();
        }}
}});

// Listen for the removal of the full notebook cells
var notebookContainer = gd.closest('#notebook-container');
if (notebookContainer) {{
    x.observe(notebookContainer, {childList: true});
}}

// Listen for the clearing of the current output cell
var outputEl = gd.closest('.output');
if (outputEl) {{
    x.observe(outputEl, {childList: true});
}}

                        })
                };
                });

I can click on any of them (where it's underlined) and share it here, if that helps any...

Screen Shot 2020-09-08 at 7 55 15 PM

In jupyter notebook it works. Same environment.

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!

@nicolaskruchten believe it or not, but i managed to get it working on one of my environments. Though that one is a bit older than the other one.

jupyter --version
jupyter core     : 4.6.3
jupyter-notebook : 6.0.3
qtconsole        : 4.7.6
ipython          : 7.14.0
ipykernel        : 5.3.0
jupyter client   : 6.1.3
jupyter lab      : 2.2.6
nbconvert        : 5.6.1
ipywidgets       : 7.5.1
nbformat         : 5.0.6
traitlets        : 4.3.3

```
jupyter labextension list
JupyterLab v2.2.6
Known labextensions:
app dir: /Users/...... path to environment...... /.venv/share/jupyter/lab
jupyterlab-plotly v4.9.0 enabled OK

OK nice! So... what's different about this environment? Do things break if you install the other labextensions? All the ones you had before were developed at Plotly or required by them (the manager) so I'd be surprised if they conflict with each other somehow but hey, if they do I know who to ask to get it fixed :)

I installed two more extensions and it still works. Now i have interactive widget support =) That's what i was looking for.
I am kind of scared to install the other two, jupyterlab-chart-editor and jupyterlab-dash because they might break things. But the chart editor would be nice to have...

@jupyter-widgets/jupyterlab-manager v2.0.0  enabled  OK
jupyterlab-plotly v4.9.0  enabled  OK
plotlywidget v4.9.0  enabled  OK

@nicolaskruchten

Same problem, fig = go.Figure() ... (fig.definitions) ... fig.show() doesn't render the plot in JupyterLab, works in Jupyter Notebook.

My installation:

- Platform: Linux-5.4.0-47-generic-x86_64-with-glibc2.10
- Python version: 3.8.1

$ node -v
v14.10.1

$  jupyter --version
jupyter core     : 4.6.3
jupyter-notebook : 6.1.1
qtconsole        : 4.7.6
ipython          : 7.18.1
ipykernel        : 5.3.4
jupyter client   : 6.1.6
jupyter lab      : 2.2.6
nbconvert        : 5.6.1
ipywidgets       : 7.5.1
nbformat         : 5.0.7
traitlets        : 4.3.3

plotly  4.10.0

~$ labextension list
JupyterLab v2.2.6
Known labextensions:
   app dir: /home/dn/anaconda3/envs/nlpu/share/jupyter/lab
        @jupyter-widgets/jupyterlab-manager v2.0.0  enabled  OK
        jupyterlab-plotly v4.10.0  enabled  OK
        plotlywidget v4.10.0  enabled  

jupyter lab build didn't help.

_Update_: Just noticed the JupyterLab extensions for plotly, installed from the command-line per plotly instructions, do not subsequently show-up in the "INSTALLED" section of the JupyterLab Extension Manager. Could that be the issue? I can't say presently. Will keep investigating.

_2nd update_: Got it working. I had an earlier JupyterLab version installed in my "base" environment, and the latest JupyterLab version and extensions in my working environment. I was starting JupyterLab from the base environment and not getting the latest version + extensions. All's well.

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!

  • fig.show("jupyterlab") <--- This worked for me. Thanks for the solve.

sample blank image under the cell on jupyterlab

jupyter core     : 4.6.3
jupyter-notebook : 6.0.3
qtconsole        : not installed
ipython          : 7.13.0
ipykernel        : 5.1.4
jupyter client   : 6.0.0
jupyter lab      : 2.2.9
nbconvert        : 5.6.1
ipywidgets       : 7.5.1
nbformat         : 5.0.4
traitlets        : 4.3.3
JupyterLab v2.2.9
Known labextensions:
   app dir: <path>
        @jupyter-widgets/jupyterlab-manager v2.0.0  enabled  OK
        @jupyterlab/git v0.20.0  enabled  OK
        @jupyterlab/toc v4.0.0  enabled  OK
        jupyterlab-plotly v4.12.0  enabled  OK
        nbdime-jupyterlab v2.0.0  enabled  OK
        plotlywidget v4.12.0  enabled  OK

Uninstalled core extensions:
    @telamonian/theme-darcula
    jupyterlab-plotly

None of the above solutions worked

  1. using fig.show("jupyterlab")
  2. jupyter lab build
  3. enabling trusted notebook
  4. Default renderer is 'plotly_mimetype+notebook', changing to 'plotly_mimetype+jupyterlab' or 'plotly_mimetype', did not solve the issue

setting pio.renderers.default to iframe

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tssweeney picture tssweeney  路  4Comments

gv-collibris picture gv-collibris  路  4Comments

ivirshup picture ivirshup  路  3Comments

keithjjones picture keithjjones  路  3Comments

astrojuanlu picture astrojuanlu  路  4Comments