tqdm 在 Jupyter Notebook 中未计算迭代次数

3

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

我可以这样做:

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

print(a)

有输出

enter image description here

然而,在循环中使用 print 语句会出现问题(见此处)。 当我执行时

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

print(a)

我明白了。

enter image description here

也就是说,这些迭代已经被执行了,但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相关。

1
我已经在这里提交了一个问题:https://github.com/tqdm/tqdm/issues/1149 - Stefan_EOX
1个回答

2

我在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

事实证明,我的环境出了一些问题。尽管我已经安装了 widgetsnbextensionipywidgets,但它们并没有被正确识别。在清洁安装了我的环境之后,现在一切都按预期工作了。 - Stefan

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