25

I'm using plotly at jupyter lab, but I'm getting a blanked output. I'm having exactly the same problem described here: plotly.offline.iplot gives a large blank field as its output - why?

And I tried what they suggested in the answers, but it didn't work.

Here is the code I'm using:

import pandas as pd
import numpy as np
%matplotlib inline

from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
cf.go_offline()

df = pd.DataFrame(np.random.randn(100,4), columns='A B C D'.split())
df2 = pd.DataFrame({'category':['A','B','C'], 'values':[32,43,50]})

df.iplot(kind='scatter', x='A',y='B', mode='markers', size=10)

One of the suggestions was to change the notebook to 'trusted'. Do you know how can I do it in jupyter lab?

YaOzI
  • 16,128
  • 9
  • 76
  • 72
Leda Grasiele
  • 413
  • 1
  • 6
  • 14
  • Observation: the code works well in jupyter notebook. The problem only happens in jupyter lab. – Leda Grasiele Mar 01 '19 at 01:01
  • 2
    The same *Observation* here. The problem is related to the `labextension` of JupyterLab. – YaOzI Jun 26 '19 at 17:04
  • For those who had the same problem with jupyter notebook, a solution that worked for me was calling `fig.show('notebook')`. This solution was suggested here: https://plotly.com/python/troubleshooting/#import-problems – Leda Grasiele Apr 21 '20 at 17:03
  • 3
    One solution for people who run into similar problems could be trying out another browser. I had a similar problem and tried to trouble shoot it only to find out that my standard browser was the source of my problem (don't know why) but switching to another browser fixed my problem. – LeSchwambo Jun 05 '20 at 14:55

4 Answers4

15

To properly display the plotly offline graphs in JupyterLab,

Step 1: We need to first install the plotly-extension for JupyterLab:

$ jupyter labextension install @jupyterlab/plotly-extension

(Note that the above step requires Node.js >= 4, if Node.js is not available on your OS, install it from its Official Website.)

Step 2: Check the status after the installation of @jupyterlab/plotly-extension:

$ jupyter labextension list 
JupyterLab v0.35.5
Known labextensions:
   app dir: /Users/yourname/anaconda3/share/jupyter/lab
        @jupyterlab/plotly-extension v0.18.2  enabled  OK

Build recommended, please run `jupyter lab build`:
    @jupyterlab/plotly-extension needs to be included in build

Step 3: Follow the suggestion, re-build the JupyterLab with its newly installed labextensions:

$ jupyter lab build

After these, restart JupyterLab, and set plotly.offline.init_notebook_mode(connected=True) at the start of each notebook session, then plotly.offline.iplot should correctly display the plots in the notebook.

YaOzI
  • 16,128
  • 9
  • 76
  • 72
  • This was a quick fix for me using the [new extension](https://www.npmjs.com/package/jupyterlab-plotly) since the [prior](https://www.npmjs.com/package/@jupyterlab/plotly-extension) has been deprecated. Thanks @YaOzl! – Jason R Stevens CFA Jun 16 '20 at 04:48
  • 2
    This package (@jupyterlab/plotly-extension) is being deprecated. Please use the Plotly-supported jupyterlab-plotly (https://www.npmjs.com/package/jupyterlab-plotly). – Michael Belvedersky Feb 18 '21 at 12:38
12

@YaOzI answer is partially correct but @jupyterlab/plotly-extension is deprecated and not supported by the official plotly team as you can read here.

This can give you the following error:

ValueError: The extension "@jupyterlab/plotly-extension" does not yet support the current version of JupyterLab.


Conflicting Dependencies:
JupyterLab                        Extension      Package
>=2.2.1 <2.3.0                    >=1.3.0 <2.0.0 @jupyterlab/rendermime-interfaces
See the log file for details:  /tmp/jupyterlab-debug-a3i3t9j4.log

>>> TL;DR:

Following the official advice (make sure your kernel is shut down, otherwise you will need to run jupyter lab build):

jupyter labextension install jupyterlab-plotly

If you get something like:

ValueError: Please install Node.js and npm before continuing installation.

You need to install nodejs first (thanks @Travis):

conda install -c conda-forge nodejs

Done.

If you want to be sure it worked:

jupyter labextension list

May give you something like this:

JupyterLab v2.2.9
Known labextensions:
   app dir: /home/user/anaconda3/envs/your-py-env/share/jupyter/lab
        jupyterlab-dash v0.3.0  enabled  OK
        jupyterlab-plotly v4.14.1  enabled  OK

David
  • 121
  • 1
  • 7
4

Installing extensions for Jupyter lab has changed for version > 3.0.

To see how the make plotly work in Jupyter lab. Check the packages that need to be installed in the official documentation

dhiraj suvarna
  • 505
  • 7
  • 20
0

I had the same issue but the sollution was for me a little different.

I'm using jupyterhub and my virtual environments are jupyter kernels so using the above steps didnt help.

I had to:

  1. login in to my jupyterhub docker container docker exec -it jupyterhub bash
  2. ensure the right packages where installed pip install "jupyterlab>=3" "ipywidgets>=7.6"
  3. install the plotly extension jupyter labextension install jupyterlab-plotly
  4. build jupyter jupyter lab build