Can I add message to the tqdm progressbar?

PythonTqdm

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

The example shown in Usage of tqdm works well for me.

pbar = tqdm(["a", "b", "c", "d"])
for char in pbar:
    pbar.set_description("Processing %s" % char)

Or alternatively, starting Python 3.8 which supports the walrus operator :=:

for char in (pbar := tqdm(["a", "b", "c", "d"])):
    pbar.set_description("Processing %s" % char)

Solution 2 - Python

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).

Solution 3 - Python

Other answers focus on dynamic description, but for a static description you can add a desc argument into the tqdm function.

from tqdm import tqdm

x = [5]*1000
for _ in tqdm(x, desc="Example"):
    pass
 
Example: 100%|██████████████████████████████████| 1000/1000 [00:00<00:00, 1838800.53it/s]

Solution 4 - Python

You can use set_postfix to add values directly to the bar.

Example:

from tqdm import tqdm
pbar = tqdm(["a", "b", "c", "d"])
num_vowels = 0
for ichar in pbar:
    if ichar in ['a','e','i','o','u']:
        num_vowels += 1
    pbar.set_postfix({'num_vowels': num_vowels})

The postfix dictionary is integrated into the progress bar:

100%|███████████| 4/4 [00:11<00:00,  2.93s/it, num_vowels=1]

Instead of a dictionary, you can use set_postfix_str to just add a string to the end of the progress bar.

Solution 5 - Python

I personally find it much cleaner to use the with statement:

from tqdm import tqdm

with tqdm(['a','b','c']) as t:
  for c in t:
    t.set_description(f'{c}')

Solution 6 - Python

I personally use .set_description() and a progression_bar assignment statement before the for loop:

from tqdm import tqdm

progression_bar = tqdm(["An", "iterable", "object"])
for element in (progression_bar):
    progression_bar.set_description("Processing « %s »" % str(element))

Solution 7 - Python

Although all the answers here are correct, tqdm also provides a set_postfix_str method. The advantage over set_postfix is that you can pass your own formatted string in place of key value pairs. Also set_postfix sorts the key value pairs alphabetically. Here is an MWE.

from tqdm import tqdm
import numpy as np

loop_obj = tqdm(np.arange(10))

for i in loop_obj:
    loop_obj.set_description(f"Count: {i}")  # Adds text before progessbar
    loop_obj.set_postfix_str(f"Next count: {i+1}")  # Adds text after progressbar

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDror HilmanView Question on Stackoverflow
Solution 1 - PythonGhruaView Answer on Stackoverflow
Solution 2 - PythongaborousView Answer on Stackoverflow
Solution 3 - PythonBhanuka ManeshaView Answer on Stackoverflow
Solution 4 - PythonMarkusView Answer on Stackoverflow
Solution 5 - PythonMatt RaymondView Answer on Stackoverflow
Solution 6 - PythonClaude COULOMBEView Answer on Stackoverflow
Solution 7 - PythonlearnerView Answer on Stackoverflow