Search code examples
pythonpython-3.xprogress-bartqdm

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!


Solution

  • 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]