Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested tqdm outputs to new line

When I try to use tqdm for nested loops, the loops update to new lines. The progress bar was working fine for just a single loop. I am running python 3.6.5. Is this an issue with the python version compatibility? If this is not currently possible in tqdm, how would I do this using another module?

I have tried using the progressbar module, but it had the same issue.

from tqdm import tqdm
from time import sleep
for i in tqdm(range(20), desc = 'sleeping'):
     for j in tqdm(range(15), desc = 'inside', leave = False):
        sleep(.1)

This is what I am getting as output:

sleeping:   0%|                                                                                 | 0/20 [00:00<?, ?it/s]
inside:   0%|                                                                                   | 0/15 [00:00<?, ?it/s]
inside:   7%|█████                                                                      | 1/15 [00:00<00:01,  9.94it/s]
inside:  13%|██████████                                                                 | 2/15 [00:00<00:01,  9.94it/s]
inside:  20%|███████████████                                                            | 3/15 [00:00<00:01,  9.91it/s]
inside:  27%|████████████████████                                                       | 4/15 [00:00<00:01,  9.88it/s]
inside:  33%|█████████████████████████                                                  | 5/15 [00:00<00:01,  9.86it/s]
inside:  40%|██████████████████████████████                                             | 6/15 [00:00<00:00,  9.84it/s]
inside:  47%|███████████████████████████████████                                        | 7/15 [00:00<00:00,  9.86it/s]
inside:  53%|████████████████████████████████████████                                   | 8/15 [00:00<00:00,  9.87it/s]
inside:  60%|█████████████████████████████████████████████                              | 9/15 [00:00<00:00,  9.88it/s]
inside:  67%|█████████████████████████████████████████████████▎                        | 10/15 [00:01<00:00,  9.88it/s]
inside:  73%|██████████████████████████████████████████████████████▎                   | 11/15 [00:01<00:00,  9.87it/s]
inside:  80%|███████████████████████████████████████████████████████████▏              | 12/15 [00:01<00:00,  9.86it/s]
inside:  87%|████████████████████████████████████████████████████████████████▏         | 13/15 [00:01<00:00,  9.85it/s]
inside:  93%|█████████████████████████████████████████████████████████████████████     | 14/15 [00:01<00:00,  9.86it/s]
inside: 100%|██████████████████████████████████████████████████████████████████████████| 15/15 [00:01<00:00,  9.84it/s]
sleeping:   5%|███▋                                                                     | 1/20 [00:01<00:28,  1.52s/it]

I expected this would only produce 2 progress bars of output.

like image 697
Reagan Wiggs Avatar asked Jan 28 '19 18:01

Reagan Wiggs


2 Answers

The key is the inner loop and tqdm(your iterator) also returns a iterator. You can think it as the combination of your iterator and a process bar.

Therefore, when it exhausted, next loop will create a new tqdm iterator and consequently, new line.

My solution is do not use tqdm as your iterator and just use it as process bar.

outter_bar = tqdm(range(20), desc = 'sleeping')
outter_loop = range(20)

inner_bar = tqdm(range(15), desc = 'inside', leave = False)
inner_loop = range(15)

for i in outter_loop:
    outter_bar.update(1)
    for j in inner_loop:
        inner_bar.update(1)
        sleep(0.5)
    inner_bar.reset()

I test my code on jupyter notebook and it works.

Hope it helpful!

PS: To control various inner loop

outter_loop = tqdm(range(2))
inner = [range(10), range(15)]
inner_bar = tqdm(None, total=10)

for i in outter_loop:
    inner_bar.total = len(inner[i])
    for _ in inner[i]:
        sleep(0.5)
        inner_bar.update(1)
    inner_bar.reset()
like image 169
Kurisu Makise Avatar answered Oct 04 '22 07:10

Kurisu Makise


As Windows terminal does not allow for ANSI escape character sequences, another module must be installed to make nested tqdm work on Windows. Installing colorama solved this problem for me.

like image 35
Reagan Wiggs Avatar answered Oct 04 '22 07:10

Reagan Wiggs