Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

Python: How to replace tqdm progress bar by next one in nested loop?

Writer Olivia Zamora

I use tqdm module in Jupyter Notebook. And let's say I have the following piece of code with a nested for loop.

import time
from tqdm.notebook import tqdm
for i in tqdm(range(3)): for j in tqdm(range(5)): time.sleep(1)

The output looks like this:

100%|██████████| 3/3 [00:15<00:00, 5.07s/it]
100%|██████████| 5/5 [00:10<00:00, 2.02s/it]
100%|██████████| 5/5 [00:05<00:00, 1.01s/it]
100%|██████████| 5/5 [00:05<00:00, 1.01s/it]

Is there any option, how to show only current j progress bar during the run? So, the final output after finishing the iteration would look like this?

100%|██████████| 3/3 [00:15<00:00, 5.07s/it]
100%|██████████| 5/5 [00:05<00:00, 1.01s/it]

4 Answers

You can use leave param when create progress bar. Something like this:

import time
from tqdm import tqdm
for i in tqdm(range(3)): for j in tqdm(range(5), leave=bool(i == 2)): time.sleep(1)
4

You can achieve this by resetting the progress bar object every time before inner loop starts.

Try the following code to achieve the results you want.

import time
from tqdm.notebook import tqdm
#initializing progress bar objects
outer_loop=tqdm(range(3))
inner_loop=tqdm(range(5))
for i in range(len(outer_loop)): inner_loop.refresh() #force print final state inner_loop.reset() #reuse bar outer_loop.update() #update outer tqdm for j in range(len(inner_loop)): inner_loop.update() #update inner tqdm time.sleep(1)

Output:

Output

1

Thanks @michael-schroter for bringing this to my attention and @jaroslav-bezděk for the original post.

I'd prefer @anton-pomieshchenko's answer. However as noted, the inner bar keeps disappearing and re-appearing each time the outer bar updates, which could look bad if it happens quickly and frequently.

Correct answer (assuming you don't need to re-use the outer bar, but do want to re-use the inner one):

from time import sleep
from tqdm.auto import tqdm
with tqdm(range(3)) as outer: inner_total = 3 with tqdm(total=inner_total) as inner: for i in outer: inner.reset(inner_total) # reinitialise without clearing for j in range(inner_total): sleep(1) inner.update() inner.refresh() # print last state without clearing

Please find the modified answer from @hamza-khurshid for three loops.

import time
from tqdm.auto import tqdm
#initializing progress bar objects
outer_loop=tqdm(range(3))
mid_loop=tqdm(range(5))
inner_loop=tqdm(range(7))
for i in range(len(outer_loop)): mid_loop.refresh() #force print final state mid_loop.reset() #reuse bar outer_loop.update() #update outer tqdm for j in range(len(mid_loop)): inner_loop.refresh() #force print final state inner_loop.reset() #reuse bar mid_loop.update() ##update mid tqdm for k in range(len(inner_loop)): inner_loop.update() #update inner tqdm time.sleep(1)
print(i,j,k)

Please find the output below:

enter image description here

This is also the answer for the question on github

Thanks & Best Regards

Michael

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy