如何让ipywidgets在JupyterLab 3.x版本中正常工作?
JupyterLab 3.4.4 中 ipywidgets 无法正常使用的问题及解决方法
问题描述
原在JupyterLab 2.x中通过指定依赖版本可正常使用ipywidgets,验证代码如下:
from ipywidgets import interact def f(x): return x interact(f, x=10);
升级至JupyterLab 3.4.4后,该代码无法显示滑块且无报错。最初排查到版本不匹配错误:
[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
已尝试jupyter lab clean、刷新/重启浏览器、重启终端等操作,问题未解决。
更新:多环境下的复现情况
ipywidgets仅在base环境内核中可用,新建环境均失效,复现步骤:
# 创建Python 3.9环境 # 用conda或pip均可,以下为pip示例 pip install jupyterlab pip install ipympl conda install nb_conda_kernels
解决方法
1. 修复版本不匹配问题
你的环境中ipywidgets版本为7.6.0,而JupyterLab配套的@jupyter-widgets/controls扩展版本为5.0.1,两者版本不兼容。JupyterLab 3.x需搭配ipywidgets 7.7+ 或 8.x版本,同时确保jupyterlab_widgets扩展版本匹配。
执行以下命令升级依赖:
# pip方式 pip install --upgrade ipywidgets jupyterlab_widgets # conda方式 conda install -c conda-forge ipywidgets=8 jupyterlab_widgets
2. 多环境适配方案
每个需要使用ipywidgets的conda环境,必须单独安装完整依赖,不能仅依靠nb_conda_kernels:
# 激活目标环境 conda activate 你的环境名 # 安装依赖 conda install -c conda-forge ipywidgets jupyterlab_widgets # 或pip方式 pip install ipywidgets jupyterlab_widgets
3. 彻底清理缓存
升级后若仍有问题,执行以下操作清空缓存并重建JupyterLab:
jupyter lab clean jupyter lab build
重启JupyterLab后,清空浏览器缓存再打开页面。
4. 验证安装结果
执行以下命令确认版本兼容:
# 查看ipywidgets版本 pip show ipywidgets # 查看Lab扩展状态 jupyter labextension list
确保@jupyter-widgets/jupyterlab-manager和@jupyter-widgets/controls版本与ipywidgets版本对应。
内容的提问来源于stack exchange,提问作者BaileyA
相关产品推荐
相关产品推荐

