Fix Python – Can I add message to the tqdm progressbar?

Question

Asked By – Dror Hilman

When using the tqdm progress bar: can I add a message to the same line as the progress bar in a loop?

I tried using the “tqdm.write” option, but it adds a new line on every write. I would like each iteration to show a short message next to the bar, that will disappear in the next iteration. Is this possible?

Now we will see solution for issue: Can I add message to the tqdm progressbar?


Answer

You can change the description to show a small message before the progress bar, like this:

from tqdm import trange
from time import sleep
t = trange(100, desc='Bar desc', leave=True)
for i in t:
    t.set_description("Bar desc (file %i)" % i)
    t.refresh() # to show immediately the update
    sleep(0.01)

/EDIT: in the latest releases of tqdm, you can use t.set_description("text", refresh=True) (which is the default) and remove t.refresh() (thanks to Daniel for the tip).

This question is answered By – gaborous

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0