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]
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With