在使用tqdm运行两个进度条时,我在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]
版本信息: Python:3.9.6 VSCode:1.58.2 Python扩展:v2021.7.1060902895 Jupyter扩展:v2021.8.1054968649
tqdm.notebook
,你试过吗?但在tqdm.autonotebook
中,似乎不支持VSCODE。 - rioV8tqdm
的设计是支持许多同时运行的进度条。如果我正确理解了你提到的tqdm.notebook
中的逻辑,那么不支持的是 vscode 中的 jupyter notebook 接口。你所提到的异常只会触发控制台(它的逻辑有点“奇怪”,因为它使用了异常。当然,我可能没有正确地阅读代码!现代的 vscode 已经支持了 ipywidgets,所以这个问题应该会得到改变,但这是一个独立于本问题的问题,我认为。 - Gordon