Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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}")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading