Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly express is not rendered in jupyter lab

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
like image 420
Mth Clv Avatar asked Aug 11 '19 14:08

Mth Clv


3 Answers

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.

like image 64
kvdv Avatar answered Oct 12 '22 02:10

kvdv


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
like image 10
nicolaskruchten Avatar answered Nov 12 '22 02:11

nicolaskruchten


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]
like image 3
eugeneral Avatar answered Nov 12 '22 00:11

eugeneral