Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format large number output in Python's tqdm module for readability

is there any way we can format a big number in tqdm's progress bar for better readability? For example, 1000000row/s must be 1,000,000rows/s .

I know about unit scale, but it formats a number like 1,21M rows/s which is not currently what I am trying to achieve

Thanks!

like image 779
Stanislav Avatar asked Oct 23 '25 16:10

Stanislav


1 Answers

A little bit of reverse-engineering how tqdm handles it:

from time import sleep

from tqdm import tqdm

tqdm.format_sizeof = lambda x, divisor=None: f"{x:,}" if divisor else f"{x:5.2f}"

with tqdm(total=1_000_000, unit_scale=True) as t:
    for _ in range(1_000_000):
        sleep(0.00001)
        t.update()

Prints:

  8%|████████████▊                     | 78,888/1,000,000 [00:04<00:56,  0.00it/s]
like image 126
Andrej Kesely Avatar answered Oct 26 '25 06:10

Andrej Kesely



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!