I am wondering if it possible to add a custom variable to the tqdm bar where the value sometimes updates with each iteration. For example:
exception_count = 0
for _ in tqdm(range(1000), bar_format="Exceptions: counthere | Elapsed: {elapsed} |{rate_fmt}"):
    try:
        do_stuff()
    except Exception:
        exception_count += 1
I would like to add the exception_count variable somewhere in the bar_format parameter as a type of custom error counter. 
You can add additional stats to the progress bar using postfix.
import random
import time
from tqdm import tqdm
def do_stuff():
    time.sleep(0.01)
    if random.randint(0, 10) < 3:
        raise Exception()
exception_count = 0
with tqdm(
    bar_format="Exceptions: {postfix} | Elapsed: {elapsed} | {rate_fmt}",
    postfix=exception_count,
) as t:
    for _ in range(1000):
        try:
            do_stuff()
        except Exception:
            exception_count += 1
            t.postfix = exception_count
            t.update()
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With