Mastering Python Progress Bars with tqdm: A Comprehensive Guide

tqdm in python

Sometimes, it’s important to know if a process, like an installation, is still running or not. While we can represent progress with numbers, it’s easier to understand with a visual progress bar. In this tutorial, we’ll learn how to create progress bars in Python using tqdm library, customize them, and view them in our command line and in our Python notebooks. By the end, you’ll have the knowledge to visually track progress in your Python programs. Let’s dive in!

The tqdm library in Python is an easy and interactive way to display progress bars in your programs. With just a few lines of code, you can add visual tracking to your loops, customize the length and description of the progress bar, and even display colored progress bars in Jupyter Notebooks.

Understanding the Python tqdm Library

“tqdm” stands for “taqaddum” which means “progress” in Arabic. The tqdm library is a Python library that helps you create progress bars in a very simple way. It provides a method to add progress bars to your loops and iterate over elements while displaying the progress in real time. With tqdm, you can add a progress bar to your loops with just a few lines of code.

Installing and Using tqdm: The Basics

We will start by installing the tqdm module using pip

pip install tqdm

The syntax for tqdm function usage is:

tqdm(iterable, desc = "Any text")
  • iterable : Any iterable over which the progress bar should be shown. We will take Python’s range(x) in a for loop here.
  • desc : The desc parameter in tqdm lets you specify a custom prefix string for the progress bar, which can give a context for the progress (e.g., “Downloading”, “Processing”, etc.)

Please keep in mind These parameters are only for getting us started, the actual number of parameters available in tqdm() is huge.

After installation, We will write a simple code to display a progress bar in our command line.:

from time import sleep
from tqdm import tqdm

for i in tqdm(range(10)):
    sleep(1)

Starting from importing the tqdm and time library we have written our sample for loop. Here we have taken range(10) as our iterable for tqdm() which gives an indication to our for loop to iterate 10 times. The sleep(1) makes the python program wait for 1 second in every iteration, this makes it easy for us to observe that the progress bar is working. Learn more about time.sleep() here.

Output:

Screenshot 2023 05 29 082340
Tqdm progress bar

The bar looks longer than needed? Let’s customize it and make it smaller!

Customizing Your Progress Bar: How to Adjust Length

tqdm module provides a parameter bar_format which can be used to alter the length of the progress bar in terms of number of characters.

Example:

from time import sleep
from tqdm import tqdm

for i in tqdm(range(10), bar_format='{l_bar}{bar:20}{r_bar}', desc ="Installing Happiness"):
    sleep(1)

Let’s break down the bar_format string used in the example code: {l_bar}{bar:20}{r_bar}

  • {l_bar}: Represents the leftmost part of the progress bar.
  • {bar:20}: Specifies the actual progress bar itself, here we have a width of 20 characters.
  • {r_bar}: Represents the rightmost part of the progress bar.

Output:

Screenshot 2023 05 31 004432
Progress bar with customized length

Observe that the progress bar is now only 20 characters in length which is significantly smaller. You can change the length of the bar to any measure according to your requirements.

Giving Context to Your Progress: Using the desc Parameter

Generally while installing Python packages we see keywords like Downloading/Installing on our command line, We can add text in front of the progress bars like these using the desc parameter of tqdm(). For example, in our code, we will add the parameter desc ="Installing Happiness" in the tqdm() function, now our code becomes:

from time import sleep
from tqdm import tqdm

for i in tqdm(range(10), bar_format='{l_bar}{bar:20}{r_bar}', desc ="Installing Happiness"):
    sleep(1)

This will give us the desired output with “Installing Happiness” text present before our progress bar:

Screenshot 2023 05 31 004721
Progress bar using dec parameter

Brighten Up Your Jupyter Notebook with Colored Progress Bars

Similar to tqdm(), tqdm_notebook() allows you to iterate over an iterable object while displaying a progress bar. However, tqdm.notebook.tqdm is designed to display progress bars with a richer interface in Jupyter Notebooks, in comparison to tqdm which is better suited for command-line interfaces. It provides a colored visual representation of the progress, including the iteration count, estimated time remaining, and various other customizable parameters.

Example:

from tqdm.notebook import tqdm as tqdm_notebook
import time

for i in tqdm_notebook(range(10), desc = "Tqdm in Jupyter Notebook"):
    time.sleep(1)

In the example above we have used tqdm_notebook() which has almost identical usage as the function tqdm(). The color of the progress bar changes as:

  • Blue: While the process is still running
  • Green: The process is completed
  • Red: The process has been stopped before the completion

Output:

Screenshot 2023 05 31 010001
Blue progress bar in Jupyter notebook
Screenshot 2023 05 31 010017
The green bar on completion

Learn more about tqdm here

Conclusion

We’ve just explored the power of the tqdm library in Python, turning tedious loops into visually delightful progress displays. Whether you’re iterating over a data crunching process or monitoring a time-consuming task, tqdm’s customizable progress bars got you covered. Now, what progress will you track in your next Python project?

Read more…