Home > OS >  Get the postfix string in tqdm
Get the postfix string in tqdm

Time:04-09

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?

CodePudding user response:

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}")
  • Related