learner
learner

Reputation: 3472

Get the postfix string in tqdm

I have a tqdm progressbar. I set the postfix string using the method set_postfix_str in some part of my code. In another part, I need to append to this string. Here is an MWE.

import numpy as np
from tqdm import tqdm

a = np.random.randint(0, 10, 10)
loop_obj = tqdm(np.arange(10))

for i in loop_obj:
    loop_obj.set_postfix_str(f"Current count: {i}")
    a = i*2/3  # Do some operations
    loop_obj.set_postfix_str(f"After processing: {a}")  # clears the previous string
    
    # What I want
    loop_obj.set_postfix_str(f"Current count: {i}After processing: {a}")

Is there a way to append to the already set string using set_postfix_str?

Upvotes: 3

Views: 906

Answers (1)

robinood
robinood

Reputation: 1248

You could just append the new postfix to the old one like so:

import numpy as np
from tqdm import tqdm

a = np.random.randint(0, 10, 10)
loop_obj = tqdm(np.arange(10))

for i in loop_obj:
    loop_obj.set_postfix_str(f"Current count: {i}")
    a = i*2/3  # Do some operations
    loop_obj.set_postfix_str(loop_obj.postfix + f" After processing: {a}")

Upvotes: 2

Related Questions