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

Time:12-03

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 = f'{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 enter image description here

CodePudding user response:

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}'):
    ...
  • Related