When working with tqdm for a project, I've noticed that when I use a print statement just before I call a function that has a tqdm progress bar, the print statement is printed somewhere in between the progress bars.

I've noticed that this behaviour only occurs in ipython notebooks, not when run in terminal.

example output in ipython

example output in terminal

from tqdm import tqdm

def long_progress():
    for _ in tqdm(range(100)):
        time.sleep(.05)

for i in range(5):
    print("Show parameters of the progress")
    long_progress()

This behaviour doesn't break anything, I'm just really curious why it is happening.

I've also noticed that when I use time.sleep(0.2) before I call tqdm it's resolved, so it seems like some timing issue.

Edit: Changed print statement to reflect my goal more precisely

1

There are 1 answers

1
Ekure Edem On

Define the print statement outside the loop i.e

print("Lets start long progress")
for i in range(5):
    long_progress()

This will call the print before the loop execution.