So I'm using the Python tqdm library to display a progress bar in the terminal when I execute a Python script that includes a for loop.
For example, when I do this:
import time
from tqdm import tqdm
for i in tqdm(range(10)):
time.sleep(1)
print(' '*5+str(i))
I get this displayed in the terminal after a few seconds:
, as expected. Here the print statement is not important, is just a computation (I used print just to display the fact that the computation was done).
Now I add a continue statement in this way:
for i in tqdm(range(10)):
time.sleep(1)
if i < 5:
continue
print(' '*5+str(i))
and I get this:
So, I understand that the print statement is skipped for each iteration below 5, but why the progress bar is not displayed? Is tqdm somehow adding a "display a progress bar" statement at the end of the loop and thus this order is been skipped like the print statement? I feel like it's like if tqdm is still storing the data of the progress bar (the one that should have been displayed), so it knows where to follow after the continue statement is no longer executed.
In any case I would like to know if there is a way of having the progress bar printed in each iteration, even with a continue statement inside the for loop and even if my loop won't execute print(' '*5+str(i)) until it surpasses the fifth iteration of the loop. Just to be clear, I expect to see something like this:
Notice that this displayed the progress bar through all the iterations in the for loop but didn't add the printed number of iterations until the fifth one.


