A tip A day – Python Tip #4 – Show Progress Bar using TQDM

Have you ever wished to see a progress bar in Jupyter Notebook when executing a complex function in for loop?

Its possible in Python.

Photo by Gaelle Marcel on Unsplash

There is a package tqdm which allows us to show progress bar.
Also it is simple to use!!!

While implementing a for loop wrap around the iterable object using the function tqdm to show the progress of the for loop execution.

Lets see how to do it.

As we are going to use a simple for loop of 10 numbers using range(), lets use a sleep method to slower the process. So that we can see the use of progress bar.

from tqdm import tqdm
from time import sleep
sum = 0
for i in tqdm(range(10)):
    sum = sum + i
    sleep(0.25)

Another example with list object:

st = ''
for char in tqdm(['a', 'b', 'c', 'd', 'e']):
    sleep(0.25)
    st = st + char

Add a description to the progress bar:

st = ''
for char in tqdm(['a', 'b', 'c', 'd', 'e'], desc = 'Concatenating Characters'):
    sleep(0.25)
    st = st + char

Hope you enjoyed the learning.

For more information on using tqdm and to customize the progress bar, please refer their documentation here (https://tqdm.github.io/https://github.com/tqdm/tqdm).

We will meet with a new tip in Python. Thank you! 👍

Like to support? Just click the heart icon ❤️.

Happy Programming!🎈

0

Leave a Reply

Your email address will not be published. Required fields are marked *