当从tqdm运行两个进度条时,我在Jupyter Notebook中得到重复的行,在visual studio代码的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
发布于 2021-10-12 08:02:16
确保你已经安装了ipywidgets
,然后它就能正常工作:
https://stackoverflow.com/questions/68541482
复制相似问题