【发布时间】:2021-07-27 08:31:55
【问题描述】:
当从 tqdm 运行两个进度条时,我在 Jupyter Notebook 中得到重复的行,在 Visual Studio Code 的 juypter Notebook 界面中运行:
from tqdm.auto import tqdm
from time import sleep
bar1 = tqdm(total=100, position=0, dynamic_ncols=True, leave=True, unit='file', desc="hi 1", bar_format='{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}]')
bar2 = tqdm(total=100, position=1, dynamic_ncols=True, leave=True, unit='file', desc="hi 2", bar_format='{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}]')
for i in range(100):
bar1.update(int(1))
bar2.update(int(1))
sleep(0.001)
我得到的输出是这样的:
hi 1: 0%| | 0/100 [00:00]
hi 1: 7%|▋ | 7/100 [00:00]
hi 1: 14%|█▍ | 14/100 [00:00]
hi 1: 21%|██ | 21/100 [00:00]
hi 1: 28%|██▊ | 28/100 [00:00]
hi 1: 36%|███▌ | 36/100 [00:00]
hi 1: 43%|████▎ | 43/100 [00:00]
hi 1: 51%|█████ | 51/100 [00:00]
hi 1: 58%|█████▊ | 58/100 [00:00]
hi 1: 65%|██████▌ | 65/100 [00:01]
hi 1: 72%|███████▏ | 72/100 [00:01]
hi 1: 79%|███████▉ | 79/100 [00:01]
hi 1: 86%|████████▌ | 86/100 [00:01]
hi 1: 93%|█████████▎| 93/100 [00:01]
hi 1: 100%|██████████| 100/100 [00:01]
hi 2: 100%|██████████| 100/100 [00:01]A
如果我改为执行单个循环:
from tqdm.auto import tqdm
from time import sleep
bar1 = tqdm(total=100, position=0, dynamic_ncols=True, leave=True, unit='file', desc="hi 1", bar_format='{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}]')
for i in range(100):
bar1.update(int(1))
sleep(0.001)
那么输出更接近我的预期:
hi 1: 94%|█████████▍| 94/100 [00:01]
版本信息: 蟒蛇:3.9.6 VS代码:1.58.2 Python 扩展:v2021.7.1060902895 Jupyter 扩展:v2021.8.1054968649
【问题讨论】:
-
您是否查看了源代码以了解此类进度条是如何显示的,以及是否允许多个进度条
-
如果您问“这在终端窗口中是否有效” - 是的,它确实有效。您可以通过将代码粘贴到文件中来测试这一点,然后通过 python 运行文件。如果您强制它使用笔记本扩展(ipywidgets),它也可以工作。
-
我尝试了一个控制台版本,我印象深刻的是它可以处理多个条形图。看代码,有一个特殊的
tqdm.notebook你试过了吗,但是tqdm.autonotebook好像不支持VSCODE -
我相信
tqdm旨在支持许多同时小节。如果我正确理解了您所指的tqdm.notebook中的逻辑,那么在vscode 中不支持jupyter notebook 接口。您所指的异常只是触发了控制台(逻辑是“奇怪的”,因为它使用了异常。当然,我可能会错误地阅读代码!现代 vscode 支持 ipywigets,因此可能应该更改,但是我认为这是一个独立于这个问题的问题。 -
如果它与 ipywidgets 一起工作,那么自动导入逻辑可能会失败,我在 tqdm.auto... 模块中看到了 ipywidgets 的导入
标签: visual-studio-code jupyter tqdm