Search code examples
visual-studio-codejupytertqdm

Using multiple progress bars in tqdm in vscode's jupyter notebook interface


When running two progress bars from tqdm, I get repeated lines in a Jupyter Notebook, running in visual studio code's juypter notebook interface:

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)

The output I get looks like this:

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

If I instead do a single loop:

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)

Then the output is closer to what I expected:

hi 1:  94%|█████████▍| 94/100 [00:01]

Version information: python: 3.9.6 vscode: 1.58.2 Python extension: v2021.7.1060902895 Jupyter Extension: v2021.8.1054968649


Solution

  • Make sure you have ipywidgets installed (pip package), then it works just fine:

    Screenshot