Search code examples
pythonpython-3.xtqdm

How can you change the precision of the percentage field in tqdm?


I have a very large iterable which means a lot of iterations must pass before the bar updates by 1%. It populates a sqlite database from legacy excel sheets.

Minimum reproducible example is something like this.

from tqdm import tqdm, trange
import time

percentage = 0
total = 157834

l_bar = '{desc}: {percentage:.3f}%|'
r_bar = '| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, ' '{rate_fmt}{postfix}]'
format = '{l_bar}{bar}{r_bar}'
for row in tqdm(range(2, total), ncols=100, bar_format=format):
    percentage = row/total * 100
    time.sleep(0.1)

In this example I have left all these strings as their default values except for trying to modify the percentage field in l_bar in an attempt to get decimals of a percent to print. And I haven't been able to find a default definition of bar anywhere in the docs so this implementation causes the loading bar to stop working.

From the documentation:

bar_format : str, optional
Specify a custom bar string formatting. May impact performance. [default: '{l_bar}{bar}{r_bar}'], where l_bar='{desc}: {percentage:3.0f}%|' and r_bar='| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, ' '{rate_fmt}{postfix}]' Possible vars: l_bar, bar, r_bar, n, n_fmt, total, total_fmt, percentage, elapsed, elapsed_s, ncols, nrows, desc, unit, rate, rate_fmt, rate_noinv, rate_noinv_fmt, rate_inv, rate_inv_fmt, postfix, unit_divisor, remaining, remaining_s, eta. Note that a trailing ": " is automatically removed after {desc} if the latter is empty.

However I try it seems to come out as a flat 0% and then a jump to 1% every time.

How am I misunderstanding the documentation here?


Solution

  • bar_format doesn't work like that - it's not going to look up values of l_bar or r_bar that you define in your own code. All format specifiers will be filled in with values provided on tqdm's end.

    Use a single layer of formatting, based on the variables tqdm provides:

    for row in tqdm(whatever, bar_format='{desc}: {percentage:3.2f}%|{bar}{r_bar}'):
        ...