How to create a progress bar in Python

Published on Aug. 22, 2023, 12:15 p.m.

To create a progress bar in Python, you can use the tqdm package, which provides a simple way to add progress bars to your code. Here is an example of how to use tqdm to create a progress bar:

from tqdm import tqdm
import time

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

In this example, the range() function generates a sequence of 100 integers, and the for loop iterates over this sequence. The tqdm() function wraps the loop and displays a progress bar showing the progress of the loop. The time.sleep(0.1) statement represents some arbitrary code that you want to execute in each iteration of the loop.

Note that you can customize the appearance of the progress bar by passing optional arguments to the tqdm() function. For example, you can change the color of the progress bar, the format of the output, and the width of the progress bar. Here is an example of using a custom format string:

from tqdm import tqdm
import time

for i in tqdm(range(100), bar_format="{l_bar}{bar:10}{r_bar}{bar:-10b}"):
    time.sleep(0.1)

In this example, the bar_format parameter is used to specify a custom format string for the progress bar. The l_bar and r_bar placeholders are used to represent the left and right borders of the progress bar, respectively. The bar:10 placeholder is used to represent the current progress of the bar, and the bar:-10b placeholder is used to represent the remaining or unfilled part of the progress bar.

To display custom parameters in the progress bar of tqdm in Python

To display custom parameters in the progress bar of tqdm in Python, you can pass a string to the desc parameter of the tqdm object. This string can include format specifiers that are replaced with the values of the specified variables. Here is an example:

from tqdm import tqdm
import time

for i in tqdm(range(100), desc='Processing file %d' % (i+1)):
    time.sleep(0.1)

In this example, the desc parameter is set to 'Processing file %d' % (i+1), where %d is a format specifier that is replaced with the value of (i+1) for each iteration of the loop. The resulting progress bar will display a message indicating the current file number being processed, as well as a visual indication of the progress.

You can also use string interpolation to achieve the same result. Here’s an example that uses an f-string:

from tqdm import tqdm
import time

for i in tqdm(range(100), desc=f'Processing file {i+1}'):
    time.sleep(0.1)

This does the same thing as the previous example.

To manually update a progress bar using tqdm in Python

To manually update a progress bar using tqdm in Python, you can use the update() method of the tqdm object. Here is an example:

from tqdm import tqdm
import time

pbar = tqdm(total=100)

for i in range(10):
    pbar.update(10)
    time.sleep(0.1)

pbar.close()

In this example, a tqdm object is created with a total of 100 steps. In each iteration of the loop, the update() method is called with an argument of 10, indicating that 10 steps have been completed. The time.sleep(0.1) statement simulates some arbitrary code that takes a certain amount of time to execute.

After the loop is finished, the close() method is called on the tqdm object to ensure that the progress bar is fully displayed and the console output is properly flushed.

Note that you can also update the description of the progress bar using the set_description() method of the tqdm object.

To install the tqdm package

To install the tqdm package, you can use pip, which is the package installer for Python. Here is how you can install the package using the pip command:

pip install tqdm

Alternatively, you can download the package source from the Python Package Index (PyPI) website at https://pypi.org/project/tqdm/ and install it manually by running python setup.py install from the extracted package directory.

It’s worth noting that if you’re using an Anaconda distribution of Python rather than the standard Python distribution, you may be able to install tqdm using the conda package manager instead of pip. Here’s how you can install the package using the conda command:

conda install -c conda-forge tqdm

Tags: