Python: How to Replace Tqdm Progress Bar by Next One in Nested Loop?
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 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)
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:
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:
This is also the answer for the question on github
Thanks & Best Regards
Michael