Jupyter笔记本未显示进度条

51

我正在尝试在Jupyter笔记本中创建进度条。这是一台新电脑,我通常做的事情似乎不起作用:

from tqdm import tqdm_notebook
example_iter = [1,2,3,4,5]
for rec in tqdm_notebook(example_iter):
    time.sleep(.1)

生成以下文本输出并不显示任何进度条。

HBox(children=(IntProgress(value=0, max=5), HTML(value='')))

同样的,这段代码:

from ipywidgets import FloatProgress
from IPython.display import display
f = FloatProgress(min=0, max=1)
display(f)
for i in [1,2,3,4,5]:
    time.sleep(.1)

生成这个文本输出:

FloatProgress(value=0.0, max=1.0)
有没有什么设置是我忽略了的,可以让Jupyter显示这些进度条?

3
你在使用 Jupyter Notebook 还是 Jupyter Lab 时遇到了这个问题? - Mihai Chelaru
你在哪里运行这个单元格?我只在 PyCharm 中收到 HBox() 消息,但在 Chrome 浏览器中运行时却可以正常工作。 - DaveStSomeWhere
1
@MihaiChelaru 我在 Jupyter Lab 中遇到了这个问题。 - Rafay
@Rafay 是的,我不确定这是否与纯 Jupyter 有关。也许有人应该编辑问题标题和标签,以反映清楚问题和答案是关于 Jupyter Lab 的。 - Mihai Chelaru
6个回答

79

答案在这个GitHub问题中。

关键是要确保您使用以下命令启用了ipywidgets笔记本扩展:

jupyter nbextension enable --py widgetsnbextension

对于旧版JupyterLab 2.0,您还需要安装JupyterLab扩展

jupyter labextension install @jupyter-widgets/jupyterlab-manager

对于老版本的JupyterLab 2.0,使用上述命令安装JupyterLab扩展需要已安装Node.js。从Node.js网站下载的安装程序包括所需的npm,该命令也需要它运行。

当使用JupyterLab 3.0时,当您使用pipconda安装它时,扩展将与ipywidgets一起自动安装。JupyterLab 3.0不再需要Node.js。


2
我尝试了最近版本的Juypterlab。第二个命令需要10分钟才能运行,但并没有解决问题。 - Zephaniah Grunschlag
3
值得一提的是需要安装Node.js。我没有使用conda,因此在Mac上执行了“brew install node”。 - Oren
1
我也安装了npm。否则,lab-manager的安装将失败。 - HWM-Rocker
3
希望这个能够自动安装并且随着jupyter lab一起启用。 - Thamme Gowda
4
很遗憾,这个解决方案对我没用。使用 node.js v12.16.1 并安装了 ipywidgets 扩展后,我仍然只能得到简单的 HBOX(children=...) 静态打印输出。我相信我已经安装了 @jupyter-widgets/jupyterlab-manager,尝试重新安装会导致一个大约 20 页的递归错误回溯。 - rocksNwaves
显示剩余5条评论

10

一个重要的考虑因素是需要node版本>=10.0.0才能使这个工作正常运行。 检查您的node版本,请使用以下命令:

node -v

此外,您可能已安装了版本 >= 10 的 node,但未被选中。要检查已安装的 node 版本列表,您可以使用 node 版本管理器 nvm,命令如下:

nvm ls

在下面的示例中,所选版本为9.11.2:
->      v9.11.2
        v10.4.0
        v12.5.0

为了解决这个问题,我需要运行以下命令:
nvm use 12.5.0

现在,我可以运行 @Mihai 提到的这两个命令:

jupyter nbextension enable --py widgetsnbextension
jupyter labextension install @jupyter-widgets/jupyterlab-manager

在刷新 Jupyter 浏览器标签后,现在应该可以正常工作了。


注意:对于 JupyterLab 3.0+,不需要 Node.js;该扩展可以通过 pip 安装,并且应该自动激活。 - krassowski

6

如果您不想完全解决它,可以尝试以下快速方法:

运行tqdm的命令行版本,即用from tqdm import tqdm替换from tqdm import tqdm_notebook,并运行for i in tqdm(range(10000)): pass

这对我来说产生了可以接受的输出结果。


3

在执行命令前,请先阅读所有内容:

我已经按照这里的所有说明尝试了多次,但都没有成功。

在最后一次尝试中,我进行了以下操作:

创建新环境并安装jupyterlab

https://github.com/nodesource/distributions/blob/master/README.md#debinstall

# Using Ubuntu
curl -fsSL https://deb.nodesource.com/setup_15.x | sudo -E bash -
sudo apt-get install -y nodejs

# Using Debian, as root
curl -fsSL https://deb.nodesource.com/setup_15.x | bash -
apt-get install -y nodejs

那么:

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

仍然没有起作用。然后按照这里的建议,我做了以下操作:

jupyter labextension install js

重新启动了Jupyter Lab,尝试了以下代码:

import ipywidgets as widgets
widgets.IntSlider()

最终它成功了。所以我猜缺少的是通过jupyter labextension安装js。


1

如果您没有安装node,可以按照这里的说明操作:https://github.com/nodesource/distributions/blob/master/README.md#debinstall

curl -sL https://deb.nodesource.com/setup_15.x | bash -
apt-get install -y nodejsapt-get install -y nodejs

但有时候通过conda安装会更好:
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
chmod +x Miniconda3-latest-Linux-x86_64.sh
 ./Miniconda3-latest-Linux-x86_64.sh

然后:

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

参考资料:https://ipywidgets.readthedocs.io/en/latest/user_install.html


-4

我发现Bartosz Mikulski在他的博客上提供的解决方案非常简单易行:如何在Jupyter Notebook中显示进度条

import time, sys
from IPython.display import clear_output

def update_progress(progress):
    bar_length = 20
    if isinstance(progress, int):
        progress = float(progress)
    if not isinstance(progress, float):
        progress = 0
    if progress < 0:
        progress = 0
    if progress >= 1:
        progress = 1

block = int(round(bar_length * progress))

clear_output(wait = True)
    text = "Progress: [{0}] {1:.1f}%".format( "#" * block + "-" * (bar_length - block), progress * 100)
    print(text)

您还可以参考ChristopheD在Stack Overflow上的回答《Towards Data Science博客》上的“3个提高数据科学工作流程的技巧”


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接