Python 如何让 ipywidgets 在 Jupyter Lab 中工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49542417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:07:10  来源:igfitidea点击:

How to get ipywidgets working in Jupyter Lab?

pythonipythonjupyteripywidgetsjupyter-lab

提问by user5601

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

在 Jupyter Notebook 中,ipywidgets 工作正常,但是它们似乎在 Jupyter Lab(据说比 Notebook 更好)中不起作用。

I followed these directions.

我遵循了这些指示

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

第 1 步:安装 Node.js ( https://nodejs.org/en/)

Step 2:Installed requirements on Python 3 with conda:

第 2 步:使用 conda 在 Python 3 上安装要求:

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

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

不幸的是,基本小部件在 jupyter 实验室笔记本中不起作用:

enter image description here

在此处输入图片说明

回答by scnerd

JupyterLab now prefers a model where arbitrary javascript is no longer allowedto 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 has an extension that can be activatedby running this on your command line (which assumes you already have NodeJSinstalled):

JupyterLab 现在更喜欢一种模型,在该模型中不再允许任意 javascript嵌入到单元格的输出中,这是过去可以工作的交互式 Jupyter Notebook 模块的数量。他们现在要求具有交互性的模块创建一个 JupyterLab 扩展。IPyWidgets 有一个扩展,可以通过在命令行上运行它来激活(假设您已经安装了NodeJS):

jupyter labextension install @jupyter-widgets/jupyterlab-manager

回答by 4givN

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

我有相同的 pbm,并尝试了这个解决方案(希望它可以帮助其他人):

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

jupyter labextension install @jupyter-widgets/jupyterlab-manager放弃这种在我的情况的错误:

> /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]

然后,我所做的是使用以前的版本 0.34 而不是 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 --versionand find match on its version compatibility.

UP(根据评论):您可以检查jupyter lab --version并找到其版本兼容性的匹配项

And it works now !

它现在起作用了!

enter image description here

在此处输入图片说明

回答by rocksteady

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

有同样的问题,今天对我有用的是运行“clean”命令,如下所述: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.

这让它现在适合我。

回答by David Albrecht

I was getting a Permission Deniederror, so adding sudoto the accepted command helped: sudo jupyter labextension install @jupyter-widgets/jupyterlab-manager.

我收到一个Permission Denied错误,所以添加sudo到接受的命令有帮助:sudo jupyter labextension install @jupyter-widgets/jupyterlab-manager.

回答by jtlz2

None of the other answers worked to me. It all seems to be down to version compatibility. Finally got it working - see below. All credit to @hainm on github (original link: https://github.com/jupyter-widgets/ipywidgets/issues/2488#issuecomment-509719214) My SO answer on this: https://stackoverflow.com/a/60059786/1021819Here is a straight copy of that answer:

其他答案都没有对我有用。这一切似乎都归结于版本兼容性。终于让它工作了 - 见下文。全部归功于 github 上的@hainm(原始链接:https: //github.com/jupyter-widgets/ipywidgets/issues/2488#issuecomment-509719214)我对此的回答:https://stackoverflow.com/a/60059786 /1021819这是该答案的直接副本:

Leveraging https://github.com/jupyter-widgets/ipywidgets/issues/2488#issuecomment-509719214, in a jupyterlab terminal - running on jupyterhub - execute:

利用https://github.com/jupyter-widgets/ipywidgets/issues/2488#issuecomment-509719214,在 jupyterlab 终端 - 在 jupyterhub 上运行 - 执行:

pythonversion=3.7
labversion=0.34.12
labmanagerversion=0.37.4
ipywidgetsversion=7.4.2

conda install ipywidgets=$ipywidgetsversion -c conda-forge -y --override-channels -c main
conda install jupyterlab=$labversion  -y -c conda-forge --override-channels -c main
jupyter-labextension install @jupyter-widgets/jupyterlab-manager@$labmanagerversion

At this point a jupyter lab clean; jupyter lab buildmight be of interest.

在这一点上,ajupyter lab clean; jupyter lab build可能会引起人们的兴趣。

Then in a .ipynb notebook running in the same jupyterlab window, hit the restart kernel button.

然后在同一个 jupyterlab 窗口中运行的 .ipynb 笔记本中,点击重启内核按钮。

IMPORTANT:Don't forget to also REFRESHthe browser page - or all efforts will have been in vain . :\

重要提示:不要忘记同时刷新浏览器页面 - 否则所有努力都将白费。:\

Then execute the example:

然后执行示例:

from ipywidgets import interact

@interact(x=(0, 100, 10))
def p(x=50):
    pass

I never thought I would live to see the day but - hey presto - the widget finally appears!

我从没想过我会活着看到这一天,但是 - 嘿,快 - 小部件终于出现了!

The sad things are that the setup is extremely sensitive to the installation of other extensions and the combination of compatible versions is very specific.

可悲的是,设置对其他扩展的安装非常敏感,并且兼容版本的组合非常具体。

enter image description here

在此处输入图片说明