0

I am trying to use ipywidgets with JupyterLab version 3.

There have been questions asked about this on Stack Overflow before, but only for older versions of JupyterLab. As far as I can tell, the answer that works for the least old version of JupyterLab (given here) is to set up an environment with the following precisely tuned package versions:

pythonversion=3.8.0
labversion=2.1.5
labmanagerversion=2.0
ipywidgetsversion=7.5.1
nodejsversion=10.13.0

conda create -n lab python=$pythonversion -y
conda activate lab
conda install nodejs=$nodejsversion -c conda-forge -y
conda install ipywidgets=$ipywidgetsversion -c conda-forge -y
conda install jupyterlab=$labversion  -y -c conda-forge
jupyter-labextension install @jupyter-widgets/jupyterlab-manager@$labmanagerversion

More recent versions of Python, such as 3.9, also seem to work with this setup. That it works can be verified by the following example (which was also used in the linked question)

from ipywidgets import interact

def f(x): 
  return x 

interact(f, x=10);

However, it only works for JupyterLab version 2. If we use this environment as a jumping-off point, and run conda update jupyterlab (which for me updates it to v3.4.4) then it fails to work.

According to the JupyterLab docs, "installing ipywidgets automatically configures JupyterLab 3.x to use widgets. The ipywidgets package does this by depending on the jupyterlab_widgets package, which configures JupyterLab 3 to display and use widgets."

However, I could not get this to work even after these packages have been downloaded. In the case of the example given above, the error manifests silently; the widget (in this case, a slider) does not appear, but no error message presents itself.


Since my minimal example's (nonexistent) error message is not informative, I figured it would be helpful to post the error message I got when I first started trying to solve this problem.

[Open Browser Console for more detailed log - Double click to close this message]
Failed to load model class 'VBoxModel' from module '@jupyter-widgets/controls'
Error: Module @jupyter-widgets/controls, version ^1.5.0 is not registered, however,         2.0.0 is
    at f.loadClass (http://localhost:8888/lab/extensions/@jupyter-widgets/jupyterlab-manager/static/134.bcbea9feb6e7c4da7530.js?v=bcbea9feb6e7c4da7530:1:74977)
    at f.loadModelClass (http://localhost:8888/static/lab/4416.9d6d0a2f3f9ed5d7b141.js?v=9d6d0a2f3f9ed5d7b141:1:11350)
    at f._make_model (http://localhost:8888/static/lab/4416.9d6d0a2f3f9ed5d7b141.js?v=9d6d0a2f3f9ed5d7b141:1:9238)
    at f.new_model (http://localhost:8888/static/lab/4416.9d6d0a2f3f9ed5d7b141.js?v=9d6d0a2f3f9ed5d7b141:1:6735)
    at f.handle_comm_open (http://localhost:8888/static/lab/4416.9d6d0a2f3f9ed5d7b141.js?v=9d6d0a2f3f9ed5d7b141:1:5426)
    at _handleCommOpen (http://localhost:8888/lab/extensions/@jupyter-widgets/jupyterlab-manager/static/134.bcbea9feb6e7c4da7530.js?v=bcbea9feb6e7c4da7530:1:73393)
    at b._handleCommOpen (http://localhost:8888/static/lab/jlab_core.3ec3cdaf8a223b56ec36.js?v=3ec3cdaf8a223b56ec36:2:994257)
    at async b._handleMessage (http://localhost:8888/static/lab/jlab_core.3ec3cdaf8a223b56ec36.js?v=3ec3cdaf8a223b56ec36:2:996247)
jupyter --version
# Selected Jupyter core packages...
# IPython          : 8.4.0
# ipykernel        : 6.15.2
# ipywidgets       : 7.6.0
# jupyter_client   : 7.3.5
# jupyter_core     : 4.11.1
# jupyter_server   : 1.18.1
# jupyterlab       : 3.4.4
# nbclient         : 0.5.13
# nbconvert        : 6.4.4
# nbformat         : 5.5.0
# notebook         : 6.4.12
# qtconsole        : not installed
# traitlets        : 5.1.1

jupyter labextension list
# Config option `kernel_spec_manager_class` not recognized by `ListLabExtensionsApp`.
[W 2022-10-16 14:03:34.946 LabApp] Config option `kernel_spec_manager_class` not recognized by `LabApp`.
# JupyterLab v3.4.4
# /Users/baileyandrew/opt/anaconda3/envs/Jupyter/share/jupyter/labextensions
#         jupyter-matplotlib v0.11.0 enabled OK
#         @jupyter-widgets/jupyterlab-manager v5.0.3 enabled OK (python, jupyterlab_widgets)

# Other labextensions (built into JupyterLab)
#    app dir: /Users/baileyandrew/opt/anaconda3/envs/Jupyter/share/jupyter/lab
#         @jupyter-widgets/controls v5.0.1 enabled OK
#         @jupyterlab/celltags v3.4.8 enabled OK
#         @oriolmirosa/jupyterlab_materialdarker v0.6.0 enabled OK


# The following source extensions are overshadowed by older prebuilt extensions:
#     jupyter-matplotlib

I have tried jupyter lab clean, as well as refreshing browser, restarting browser, and restarting terminal.


Update: I've found that widgets work if the kernel is my base installation, but not for any other environments. To reproduce:

# create new environment, python 3.9
# (I assume `conda` would be fine for all, instead of pip)
pip install jupyterlab
pip install ipympl
conda install nb_conda_kernels
BaileyA
  • 145
  • 1
  • 7

1 Answers1

0

It seems that in my kernel's conda environment, I have to have ipywidgets version 8 installed. This can be tested with:

conda create -n ipyV8 python=3.9
conda activate ipyV8
conda install conda-forge::ipywidgets=8

And then selecting that environment as the correct kernel (which can be done assuming my JupyterLab's environment has had conda install nb_conda_kernels run). Note specifically that versions of ipywidgets earlier than 8 do not work.

To re-iterate: ipywidgets=8 needs to be installed in the kernel's environment. Installing it in JupyterLab's environment won't solve the issue (although that environment would have its own kernel for which it would be solved, which is how I stumbled upon this solution).

BaileyA
  • 145
  • 1
  • 7
  • 1
    In my case, `ipywidgets=8` does not work, but `ipywidgets=7.7.2` works. I really have no idea which packages are conflicted with specific versions of ipywidgets. – RibomBalt May 18 '23 at 09:19