loading...

🐍 Logging loop progress like a pro in Python

daolf profile image Pierre Updated on ・2 min read

Python tips (3 Part Series)

1) 🐍 Beware of Python dict.get() 2) 🐍 Logging loop progress like a pro in Python 3) 🐍 Writing tests faster with pytest.parametrize

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)

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.

Python tips (3 Part Series)

1) 🐍 Beware of Python dict.get() 2) 🐍 Logging loop progress like a pro in Python 3) 🐍 Writing tests faster with pytest.parametrize

Posted on by:

daolf profile

Pierre

@daolf

Tech Explorer, Python Lover and Indie Hacker Co-Founder at ScrapingBee; Co-Founded and sold PricingBot & ShopToList; Writer at daolf.com

Discussion

markdown guide
 

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']))
 

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

Thank you !!

 

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

 

Thank you, hope you'll use it ;)