Skip to content

Python Progress Bars: TQDM, Alive-Progress, and Progressbar2

Python Progress Bars TQDM, Alive-Progress, and Progressbar2 Cover Image

In this tutorial, we’ll explore three popular libraries for creating progress bars in Python: tqdmalive-progress, and progressbar2. Progress bars are an essential tool for providing users with visual feedback on the progress of tasks, such as file downloads, data processing, or web scraping. For example, when working with the Python requests library, you can track web tasks. By implementing progress bars in your Python projects, you can enhance the user experience, making it more engaging and informative.

We’ll cover the following topics in this tutorial:

  1. Deep Dive into TQDM: Using in Terminal and Notebook
  2. Using Python progressbar2
  3. Using alive-progress

Each library has its unique features and advantages, so we’ll guide you through the process of installing, using, and customizing progress bars with each of them. By the end of this tutorial, you’ll have a solid understanding of how to create progress bars in Python and be able to choose the best library for your specific needs. Whether you’re working on a simple script or a complex application, progress bars can help you keep users informed and engaged throughout the execution of your Python projects.

Creating Python Progress Bars for For Loops with TQDM

In this section, we’ll explore how to use the Python TQDM library in more detail, allowing you to create straightforward progress bars, both in the terminal and in Jupyter notebooks. Take a look at the visualization below to see what kind of progress bars you can make with TQDM.

Customizing Python Progress Bars in TQDM

TQDM isn’t part of the standard Python library. Because of this, we need to first install it, using either pip or conda.

pip install tqdm
conda install tqdm

What’s great about TQDM is how easy it is to use. The tqdm() function wraps around an iterable using a Python for loop and creates a progress bar with no boilerplate. Let’s see what this looks like:

# Creating a Simple Progress Bar with TQDM
from tqdm import tqdm
import time

for i in tqdm(range(100)):
    time.sleep(0.1)

By running this code, we generate a simple progress bar in the terminal. The progress bar will update as the loop iterates over the range. The image below shows what the progress bar looks like:

Creating Python Progress Bars with TQDM

Customizing Python Progress Bars in TQDM

While TQDM allows you to generate progress bars with little to no boilerplate, it provides significant flexibility in customizing how your progress bars display. In this section, we’ll explore how to customize the progress bars you create for your Python programs.

The tqdm() function accepts a number of different parameters to help customize the progress bar:

  • desc: A string describing the progress bar’s purpose (e.g., “Processing files”).
  • total: The total number of iterations, if it can’t be inferred from the iterable.
  • unit: A string describing the units of progress (e.g., “files”).
  • ncols: The width of the progress bar in characters.

Let’s take a look at another example to see how we can customize our progress bars using these parameters:

# Customizing Progress Bars with TQDM
from tqdm import tqdm
import time

for i in tqdm(
    range(100), 
    desc="Processing files", 
    unit="files", 
    ncols=75,
    colour='#37B6BD',
    ):
    time.sleep(0.1)

In the code above, we passed in a number of customizations. We now have a better description of what the code is doing and what unit the progress should be measured in. Similarly, we customized the width. Finally, we used a beautiful datagy teal, by customizing the resulting color of the bar.

This returns the progress bar below:

Customizing Python Progress Bars in TQDM

Let’s now dive into how to use TQDM in Jupyter notebooks.

Using TQDM in Jupyer Notebooks

TQDM also supports creating progress bars in Jupyter notebooks. Depending on how recent your version is, this works in the same way as the progress bars you developed above. If you’re using an older version, you have to use the  tqdm.notebook module and use the  tqdm.notebook.tqdm() function instead of the regular tqdm() function. Here’s an example:

# Running a Progress Bar in a Jupyter Notebook
from tqdm.notebook import tqdm
import time

for i in tqdm(range(100)):
    time.sleep(0.1)

Now, let’s dive into other libraries that allow you to create helpful progress bars in Python.

Creating Python Progress Bars with progressbar

Another popular library for creating progress bars in Python is progressbar. In this section, we’ll show you how to use the progressbar library to create and customize progress bars for your Python projects.

Similar to TQDM, progressbar isn’t part of the standard library. Because of this, we can install it using either pip or conda:

pip install progressbar2
conda install progressbar2

Note that there are two similar libraries, progressbar and progressbar2. We recommend using progressbar2 as it’s more actively maintained and up-to-date.

To use the progressbar library, you’ll need to create a ProgressBar object and update it manually as your loop iterates. Here’s a basic example:

# Using progressbar to Create a Simple Progress Bar
import progressbar
import time

bar = progressbar.ProgressBar(max_value=50)
for i in range(50):
    time.sleep(0.1)
    bar.update(i)

This creates the following progress bar:

Creating Python Progress Bars with progressbar

In the code block above, we first created a ProgressBar() object. In it, we passed in a max_value of 50. In our for loop, we then processed some actions and updated the bar using the iteration.

Customizing Progress Bars in Python with progressbar

The progressbar library provides several built-in widgets that you can use to customize the appearance and behavior of your progress bar. Some common widgets include:

  • progressbar.Bar: The progress bar itself.
  • progressbar.Percentage: The current percentage of progress.
  • progressbar.Counter: The current iteration count.
  • progressbar.Timer: The elapsed time since the progress bar was created.
  • progressbar.ETA: The estimated time remaining until the progress bar reaches 100%.

To customize your progress bar, simply pass a list of widgets to the ProgressBar constructor. Here’s an example demonstrating a custom progress bar:

# Customizing a progressbar Progress Bar
import progressbar
import time

widgets = [
    'Processing: ', progressbar.Percentage(),
    ' ', progressbar.Bar(marker='*', left='[', right=']'),
    ' ', progressbar.Counter(), '/100',
    ' ', progressbar.Timer(),
    ' ', progressbar.ETA()
]

bar = progressbar.ProgressBar(widgets=widgets, max_value=100)
for i in range(100):
    time.sleep(0.1)
    bar.update(i)

This will create a progress bar with custom widgets, displaying the percentage, a custom progress bar style, the current iteration count, elapsed time, and the estimated time remaining.

This returns the following progress bar visualization:

Customizing Progress Bars in Python with progressbar

Note that we passed in a list of both strings and progressbar objects. This allowed us then to also pass in our widgets directly into the ProgressBar object.

Let’s now take a look at a third option: alive-bar.

Creating Python Progress Bars with alive-progress

alive-progress is another library for creating progress bars in Python that offers a unique, animated appearance and a user-friendly interface. In this section, we’ll show you how to use the alive-progress library to create and customize progress bars for your Python projects.

First, you’ll need to install the alive-progress library if you haven’t already. You can do this using pip:

pip install alive-progress
conda install alive-progress

To use the alive-progress library, you’ll need to create a progress bar using the alive_bar() function in a with statement. The progress bar will automatically update as your loop iterates. Here’s a basic example:

# Creating a Progress Bar with alive_bar
from alive_progress import alive_bar
import time

with alive_bar(50) as bar:
    for i in range(50):
        time.sleep(0.1)
        bar()

This will display an animated progress bar in your terminal that updates as the loop iterates over the range.

Creating Python Progress Bars with alive-progress

Let’s now take a look at how we can customize these progress bars.

Customizing Progress Bars in Python with alive-bar

The alive-progress library provides several built-in styles and configurations that you can use to customize the appearance and behavior of your progress bar. Some common options include:

  • bar: The style of the progress bar (e.g., ‘classic’, ‘smooth’, ‘blocks’, etc.).
  • spinner: The style of the spinner animation (e.g., ‘dots_waves’, ‘arrow’, ‘bouncing’, etc.).
  • length: The width of the progress bar in characters.
  • title: A string describing the progress bar’s purpose (e.g., “Processing files”).

To customize your progress bar, simply pass the desired options to the alive_bar() function. Here’s an example demonstrating a custom progress bar:

# Customizing a Progress Bar with alive_bar
from alive_progress import alive_bar
import time

with alive_bar(50, bar='blocks', spinner='arrow', length=50, title='Processing files') as bar:
    for i in range(50):
        time.sleep(0.1)
        bar()

This will create a progress bar with custom style, spinner animation, width, and title. This allows you to create a progress bar that looks and feels exactly how you’d want it to. What’s great about this is how many different customizations the library provides.

The following progress bar is returned:

Customizing Progress Bars in Python with alive-bar

With the alive-progress library, you can easily create and customize animated progress bars for your Python projects, providing users with engaging visual feedback on the progress of tasks.

Frequently Asked Questions

How do you make a progress bar in Python?

To create a progress bar in Python, you can use popular libraries like tqdmalive-progress, or progressbar2. These libraries allow you to easily create and customize progress bars for various tasks, such as file downloads, data processing, or web scraping. To get started, install the desired library using pip (e.g., pip install tqdm) and then follow the library’s documentation to create and update the progress bar within your Python script.

How to use TQDM progress bars in Python?

To use TQDM progress bars in Python, first install the library with pip install tqdm. Next, import the tqdm module in your script using from tqdm import tqdm. To create a progress bar, simply wrap your iterable (e.g., a list or a range) with the tqdm() function in a loop. For example:
from tqdm import tqdm
import time
for i in tqdm(range(100)):
time.sleep(0.1)
This will display a progress bar in your terminal that updates as the loop iterates over the range. You can also customize the progress bar’s appearance and behavior by passing additional arguments to the tqdm() function.

How do I set a value in a Python progress bar?

To set a value in a Python progress bar, you typically update the progress bar within your loop as the task progresses. The method to update the progress bar depends on the library you’re using. For example:
With tqdm, the progress bar updates automatically when you wrap your iterable with the tqdm() function.
With alive-progress, you create a progress bar using the alive_bar() function within a with statement and update it by calling the bar() method. 

Conclusion

In this tutorial, we’ve explored three popular Python libraries for creating progress bars: tqdm, alive-progress, and progressbar2. Each library offers its own unique features and customization options, allowing you to create progress bars that best suit your project’s needs and enhance the user experience.

We’ve covered the installation, basic usage, and customization of progress bars with each library, as well as provided examples to help you get started. With this knowledge, you can now confidently implement progress bars in your Python projects, providing users with engaging and informative visual feedback on the progress of tasks.

As you continue to develop your Python skills, remember that progress bars are just one of many tools available to improve user experience and communication. Keep exploring different libraries and techniques to make your Python projects more efficient, user-friendly, and enjoyable for your users. Happy coding!

To learn more about the libraries covered here, check out their official documentation:

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials.View Author posts

Leave a Reply

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