3 Ways to Visualize Loop Progress in Python | by Pranjal Saxena | Medium | Towards Data Science

Loops, in the programming language, are used to iterate over a set of objects. While iterating, we can take different actions on each object based on various conditions to make our task done.
Sometimes, when the list’s size is considerably large, or the objects in the list have high dimensions, it took much time to process the list. In that case, like a checkmark, we usually try to print the index number to check the progress of the list.
We can’t remember the size of the loop each time we run the code. To get rid of this issue, Instead of printing the list index to check the progress, we can utilize a python library that can show a progress bar to inspect the progress visually.
This article will cover how we can utilize that python library to make the progress bar and cover the various places where we can use that library.
tqdm derives from the Arabic word taqaddum, which can mean “progress” and is an abbreviation for “I love you so much” in Spanish (te quiero demasiado).
This library helps us show a progress bar for any loops. It also supports nested-loop functionality and can also be used with pandas, Keras & Jupyter Notebooks, and many other places.
Let us explore some of the best practices, where we generally use this approach to make our task more manageable and smarter.
Before beginning with the library usage part, let us first install it using the pip command in python.
pip install tqdm
That’s it. Let us explore some areas where we can utilize tqdm module.
We can utilize tqdm module with Jupyter Notebook to keep track of progress. In the machine learning area, when we train our model using a set of epochs, then to visualize the completion progress of each epoch, we can utilize tqdm.
IPython/Jupyter is supported using the tqdm.notebook submodule.
from tqdm.notebook import trange, tqdmfrom time import sleepfor i in trange(4): #first loop    for j in tqdm(range(100)): #Nested loop        sleep(0.01)
A keras callback is also available with tqdm to visualize the progress of model fitting. We can use it as follows:
from tqdm.keras import TqdmCallback...model.fit(..., verbose=0, callbacks=[TqdmCallback()])
The keras callback also has a display() method which can be used with tqdm while fitting the machine learning model as follows:
from tqdm.keras import TqdmCallbackcbk = TqdmCallback(display=False)cbk.display()model.fit(..., verbose=0, callbacks=[cbk])
We can utilize tqdm with general python, scripting tasks to visualize the task progress, including pulling data from the database and visualize the complete flow. The scope is endless.
def improve_guess_func(rt, n):    return (rt + n/rt) / 2guess = 1target = 2for i in trange(10):    guess = improve_guess(guess, target)
That’s all for this article. We have covered how effectively we can use tqdm function in python to visualize and track the loop’s progress in a variety of places.
We can utilize this function in any situation, where we don’t know how much time a loop is going to take to process its elements. In that situation, tdqm plays a promising role.
Thanks for reading!
Before you go...
If you liked this article and want to stay tuned with more exciting articles on Python & Data Science — do consider becoming a medium member by clicking here https://pranjalai.medium.com/membership.
Please do consider signing up using my referral link. In this way, the portion of the membership fee goes to me, which motivates me to write more exciting stuff on Python and Data Science.
Also, feel free to subscribe to my free newsletter: Pranjal’s Newsletter.