DEV Community

Pierre
Pierre

Posted on • Updated on • Originally published at Medium

🐍 Logging loop progress like a pro in Python

Or the end of print(i) inside a for loop.

If you have ever wrote something like this:

    for i in range(0,10000):
         compute(i)
         print(i)
Enter fullscreen mode Exit fullscreen mode

to control the progress of your execution, this short article is for you 🙂.

All hail tqdm

tqdm, from the Arabic “taqadum” which means “progress” is one of the many Python libs that are as useful as they are easy to use. Install it the usual way with pip install tqdm and then:

VoilĂ  !VoilĂ  !

And you can even make it work with loops inside loops:

VoilĂ ! VoilĂ !VoilĂ ! VoilĂ !

That’s it.

I hope this lib will help you and that you learn something today. The whole doc is available here.

tqdm comes with many options that can help you make the progress bar exactly as you want. tqdm also works very well in Jupyter notebook, just do from tqdm import tqdm_notebook instead of from tqdm import tqdm.

Thank you for reading

I hope this short post will help you log loops better and impress your colleagues.

Please tell me in the comments if you liked the post and don't forget to subscribe to my newsletter, there is more to come (And you'll also get the first chapters of my next ebook for free 😎).

If you like JS, I've just published something you might like.

And if you prefer git, I got you covered.

Top comments (5)

Collapse
 
sebvercammen profile image
SĂ©bastien Vercammen • Edited

tqdm is a good recommendation. A different example from a project I worked on recently, using tqdm to measure continuous progress (without concrete end) and real-time rate per second, hope it helps:

from tqdm import tqdm

pbar = tqdm(desc='total update ticks', unit='tick')

def update():
    pbar.update(1)
    some_object = ...
    tqdm.write('Some text here with an object: {}'.format(some_object))

def main():
    while True:
        update()

^ will show log entries, with a final line (example w/ 15400 updates completed in 1h30min):

total update ticks: 15400tick [1:30:00, 2.85tick/s]

Bonus points if you use termcolor as well, e.g.:

from tqdm import tqdm
from termcolor import colored

tqdm.write(colored('Something bad happened', 'red'))
tqdm.write(colored('Something semi-bad happened', 'yellow'))
tqdm.write(colored('All good', 'green'))
tqdm.write(colored('Magic in the terminal', 'yellow', attrs=['bold', 'blink', 'underline']))
Collapse
 
daolf profile image
Pierre

Very nice snippets, I'm copying those in my note block.

Thank you !!

Collapse
 
lmolivera profile image
Lucas Olivera

I didn't know you could do this on Python, excellent post!

Collapse
 
daolf profile image
Pierre

Thank you, hope you'll use it ;)

Collapse
 
abdurrahmaanj profile image
Abdur-Rahmaan Janhangeer
#DidNotKnow