Plotly express is not rendered in jupyter lab
Asked Answered
L

5

18

The following code does not render in Jupyter lab:

%matplotlib widget
import plotly.express as px  
import numpy as np 
import pandas as pd

df = pd.DataFrame(np.random.randint(0,100,size=(5, 4)), columns=list('ABCD'))
px.bar(df, x='A', y='B')

enter image description here I have tried to install all the dependencies and extensions mentioned in here https://plot.ly/python/getting-started/#jupyterlab-support-python-35

but also the steps in here https://github.com/matplotlib/jupyter-matplotlib

Nothing worked

Here is my set up:

jupyter lab --version
1.0.2

python --version
Python 3.6.1 :: Continuum Analytics, Inc.

conda list jupyterlab
# packages in environment at C:\Users\***\Anaconda3:
#
# Name                    Version                   Build  Channel
jupyterlab                1.0.2            py36hf63ae98_0
jupyterlab_launcher       0.13.1                   py36_0
jupyterlab_server         1.0.0                      py_0

conda list nodejs
# packages in environment at C:\Users\***\Anaconda3:
#
# Name                    Version                   Build  Channel
nodejs                    0.1.1                    pypi_0    pypi

conda list plotly
# packages in environment at C:\Users\***\Anaconda3:
#
# Name                    Version                   Build  Channel
plotly                    4.1.0                    pypi_0    pypi
plotly-express            0.4.1                    pypi_0    pypi

EDIT:

jupyter-labextension list
JupyterLab v1.0.2
Known labextensions:
   app dir: C:\Users\***\Anaconda3\share\jupyter\lab
        @jupyter-widgets/jupyterlab-manager v1.0.2 enabled  ok
        @jupyterlab/git v0.8.0 enabled  ok
        @jupyterlab/plotly-extension v1.0.0 enabled  ok
        jupyter-matplotlib v0.4.2 enabled  ok
        jupyterlab-chart-editor v1.2.0 enabled  ok
        jupyterlab-plotly v1.1.0 enabled  ok
        plotlywidget v1.1.0 enabled  ok
Lymphadenitis answered 11/8, 2019 at 14:36 Comment(4)
DId you install the jupyterlab plotly extension, it works for me with a fresh installGlyceryl
yes, I did. Any command I can use to show you the extensions are installed?Lymphadenitis
jupyter-labextension listGlyceryl
thanks, edited my question with that information as wellLymphadenitis
N
10

EDIT: these instructions and more are now in our official Troubleshooting Guide!

It could be that remnants of previous installations or attempts at installation are causing issues. I recommend either starting with a clean install or uninstalling all Plotly modules (from both pip and conda!) and plotly-related jlab extensions, and then following the instructions here: https://plot.ly/python/getting-started/

Uninstalling the module is a matter of

conda uninstall plotly
pip uninstall plotly

And then reinstalling with one or the other but not both, according to the instructions linked above.

Uninstalling JupyterLab extensions is performed with

jupyter labextension uninstall @jupyterlab/plotly-extension
jupyter labextension uninstall jupyterlab-plotly 
jupyter labextension uninstall plotlywidget
Natter answered 11/8, 2019 at 17:47 Comment(3)
that's possible - could you mention the sequence of commands I should write using pip and conda to uninstall all plotly related modules and extensions?Lymphadenitis
It’s much easier to start from a fresh conda env or virtualenv to be honest :)Natter
I’ve edited my answer to include some uninstallation notesNatter
L
3

Following the official plotly.py repo https://github.com/plotly/plotly.py, for correct rendering of plotly in JupyterLab there's a need to installing the special extension by command

jupyter labextension install [email protected]
Liturgy answered 29/1, 2021 at 11:16 Comment(0)
C
2

I ran into the same problem but with a different cause and requiring a different solution. Just thought I'd share it for anyone encountering the same issue.

I'm running jupyterlab in a Docker container which did not yet have nodejs or npm installed.

I was unable to install the required extension via:

jupyter labextension install jupyterlab-plotly

Because it gave me this error:

ValueError: Please install nodejs and npm before continuing installation. nodejs may be installed using conda or directly from the nodejs website.

Conda was not available on the container and when installing node and npm via the jupyterlab terminal (through pip or apt-get) I got the same error, or a version mismatch (when using apt-get the nodejs version I got was too old).

The following steps helped me solve the problem.

  • Install nvm in the docker container when building the container, thus in the Dockerfile:
    • RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
    • Mind the version number, you might want to change that to whatever is the latest stable version
  • Make the nvm command available by loading some included init scripts:
    • SHELL ["bash", "-lc"] <-- Only necessary if your container is not using bash as shell already
    • RUN export NVM_DIR="$HOME/.nvm"
    • RUN [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
    • RUN [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
  • Install a specific nodejs version via nvm:
    • RUN nvm install 14.17.0
    • Mind the version number again, change to whatever version you need.
  • Install the jupyter extension:
    • RUN jupyter labextension install jupyterlab-plotly

Restart the kernel and happy plotting ;)

You might also consider installing conda and then nodejs via conda if that makes sense for your use case. I have not tested if that works though.

Content answered 5/6, 2021 at 19:25 Comment(0)
G
0

Try installing jupyterlab dash, it worked for me!

you can do it through the jupyterlab menu or by following these instructions.

https://github.com/plotly/jupyter-dash

It looks like you should upgrade your plotly as well, because plotly express is now part of plotly i.e.

import plotly.express as px
Glyceryl answered 11/8, 2019 at 14:54 Comment(2)
I have tried importing it in both ways: plotly_express and plotly.express. both failed. see last editLymphadenitis
I do have dash now: jupyterlab-dash v0.1.0-alpha.3 enabled ok Closed and reopened jupyterlab. Problem is still there.Lymphadenitis
S
0

For anyone still struggling to make this work – these steps worked for me:

  • If you have jupyter lab running – shut down your session.
  • Uninstall plotly and all other related packages like @nicolaskruchten recommends here.
  • Make a fresh install of plotly and jupyter-dash with EITHER conda or pip, but do not mix between both package managers.
  • Execute jupyter lab from terminal again. Now you should see a window Build recommended – jupyterlab-dash needs to be included in build. Confirm this.
  • After the successful build close the jupyter-lab session and initiate again.
Stereoscopy answered 24/12, 2021 at 15:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.