首页 > 解决方案 > tqdm 不计算 jupyter notebook 中的迭代次数

问题描述

tqdm用来显示迭代的进度。jupyter notebook但是它在我的环境中无法正常工作。

我可以

from tqdm import tqdm
a = 0
for i in tqdm(range(10)):
    a += i

print(a)

带输出

在此处输入图像描述

然而,这print对循环中的语句有问题(请参见此处)。当我做

from tqdm.notebook import tqdm
a = 0
for i in tqdm(range(10)):
    a += i

print(a)

我明白了

在此处输入图像描述

即迭代被执行,但 tqdm 不考虑它们。

知道如何解决这个问题或问题可能是什么?

的输出jupyter --version

jupyter core     : 4.7.1
jupyter-notebook : 6.2.0
qtconsole        : not installed
ipython          : 7.20.0
ipykernel        : 5.1.0
jupyter client   : 6.1.11
jupyter lab      : not installed
nbconvert        : 5.6.1
ipywidgets       : 7.6.3
nbformat         : 5.1.2
traitlets        : 5.0.5

编辑 这个问题似乎与 MacOS 相关(我运行的是 10.15.7),因为我没有在 CentOS 上观察到它。

除此之外,我尝试了更新我的 conda 环境

jupyter core     : 4.7.1
jupyter-notebook : 6.2.0
qtconsole        : 5.0.2
ipython          : 7.20.0
ipykernel        : 5.5.0
jupyter client   : 6.1.11
jupyter lab      : not installed
nbconvert        : 6.0.7
ipywidgets       : 7.6.3
nbformat         : 5.1.2
traitlets        : 5.0.5

在 python 3.8.8 上。(我也在 3.7.10 上试过)。 这个问题讨论了类似的问题,问题似乎与ipywidgets.

标签: pythonjupyter-notebooktqdm

解决方案


我在 Jupyter Lab (3.0.9) 中遇到了同样的问题,这对我有用:

jupyter labextension install @jupyter-widgets/jupyterlab-manager \ 
&& jupyter lab

即使ipywidgets应该自动安装 labextension,它也没有在jupyter labextension list. 当我手动安装它时,进度条工作。

对于经典笔记本,ipywidgets 文档提到了这一点:

jupyter nbextension enable --py widgetsnbextension

推荐阅读