How to get ipywidgets working in Jupyter Lab?

In Jupyter Notebook, ipywidgets work fine, however they seem to not work in Jupyter Lab (which is supposedly better than Notebook).

I followed these directions.

Step 1: Installed Node.js (https://nodejs.org/en/)

Step 2: Installed requirements on Python 3 with conda:

conda install -c conda-forge ipywidgets
jupyter labextension install @jupyter-widgets/jupyterlab-manager

Unfortunately, basic widgets do not work inside the jupyter lab notebook:

enter image description here


Solution 1:

JupyterLab now prefers a model where arbitrary javascript is no longer allowed to be embedded in a cell's output, which is how many interactive Jupyter Notebook modules used to work. They now ask that modules with interactivity create a JupyterLab extension. ipywidgets provides @jupyter-widgets/jupyterlab-manager extension which satisfies this requirement.

When using ipywidgets 7.6 or newer in JupyterLab 3.0 or newer you do not need to do anything: it is installed by default. You can check if this is installed by running:

jupyter labextension list

which should include a line like:

@jupyter-widgets/jupyterlab-manager v3.0.0 enabled OK (python, jupyterlab_widgets)

If you are using JupyterLab 1 or 2 (or old version of ipywidgets) you need to install this extension manually by running this on your command line (which assumes you already have NodeJS installed):

jupyter labextension install @jupyter-widgets/jupyterlab-manager

Solution 2:

I had the same pbm, and tried this solution (hope it can help others):

The jupyter labextension install @jupyter-widgets/jupyterlab-manager gave this kind of error in my case:

> /Users/user/.nvm/versions/node/v8.7.0/bin/npm pack @jupyter-widgets/jupyterlab-manager
jupyter-widgets-jupyterlab-manager-0.35.0.tgz

Errored, use --debug for full output:
ValueError:
"@jupyter-widgets/[email protected]" is not compatible with the current JupyterLab
Conflicting Dependencies:
JupyterLab              Extension            Package
>=0.15.4-0 <0.16.0-0    >=0.16.0-0 <0.17.0-0 @jupyterlab/application
>=1.1.4-0 <2.0.0-0      >=2.0.0-0 <3.0.0-0   @jupyterlab/services
>=0.15.4-0 <0.16.0-0    >=0.16.0-0 <0.17.0-0 @jupyterlab/rendermime
>=0.15.4-0 <0.16.0-0    >=0.16.0-0 <0.17.0-0 @jupyterlab/notebook

Then, what I did is to use a previous version 0.34 instead of 0.35: jupyter labextension install @jupyter-widgets/[email protected]

In fact, according to this, sometime teams get time to consider the last version.

UP (according to comments): You can check jupyter lab --version and find match on its version compatibility.

And it works now !

enter image description here

Solution 3:

Had the same issue, and what worked for me today was running the 'clean' command, as mentioned here: https://ipywidgets.readthedocs.io/en/latest/user_install.html#installing-the-jupyterlab-extension

So:

jupyter lab clean
jupyter labextension install @jupyter-widgets/jupyterlab-manager

And that got it working right for me just now.

Solution 4:

Note: most of the above answers are outdated (as of july 19 2021). It should be a lot more seamless with the latest versions of these packages. However, in 2021 I was having an issue rendering panel widgets in jupyterlab hosted on jupyterhub and came across this post. I tried a few of these answers at first but none of them worked. After digging into some of the libraries, I found the following:

  1. jupyter labextension install @jupyter-widgets/jupyterlab-manager is no longer required as of ipywidgets==7.6 and jupyterlab>=3.0. source. If you're on the latest version of jupyterlab, you should just need to install ipywidgets and the extension will be enabled automatically as long as widget extension authors also follow some steps.

    The main change in this release is that installing ipywidgets 7.6.0 will now automatically enable ipywidgets support in JupyterLab 3.0—a user has no extra JupyterLab installation step and no rebuild of JupyterLab, nor do they need Node.js installed. Simply install the python ipywidgets package with pip (pip install ipywidgets==7.6.0) or conda/mamba (conda install -c conda-forge ipywidgets=7.6.0) and ipywidgets will automatically work in classic Jupyter Notebook and in JupyterLab 3.0.

  2. panel has also followed suit and bundled the necessary extensions in the pyviz_comms package, so theoretically everything should have been included for my case. source

    In the classic Jupyter notebook environment and JupyterLab, first make sure to load the pn.extension(). Panel objects will then render themselves if they are the last item in a notebook cell. For versions of jupyterlab>=3.0 the necessary extension is automatically bundled in the pyviz_comms package, which must be >=2.0.

The answer to my problem turned out to be that I was installing panel after building the jupyterlab server through jupyterhub. (e.g. for a specific notebook, a user was running !pip install panel). This is where I get a little fuzzy since I'm not sure why this doesn't work, given that ipywidgets is already installed and panel install includes the pre-built jupyterlab extension. However, I was able to fix my issue by instead using an a jupyterlab image to spawn from jupyterhub which had ipywidgets>=7.6 and panel>=0.11.3 (and therefore pyviz_comms>=2.0) pre-installed. After this, panel widgets in jupyterlab on jupyterhub now work. Hope this helps anybody having a similar issue.