Progress Bars in Python


Progress bars in Python are visual indicators that provide feedback on the progress of a task or operation. They are useful for long-running processes or iterations where it's helpful to show how much work has been completed and how much is remaining.

A progress bar typically consists of a visual representation, such as a horizontal bar or a textual representation, which dynamically updates to reflect the progress of the task. It also includes additional information like the percentage of completion, estimated time remaining, and any relevant messages or labels.

The following are the purposes served by the progress bars in python.

  • Visual Feedback

  • Time Estimation

  • User experience

Here are a few approaches to implementing progress bars in Python. Let’s see each approach in detail.

Using the tqdm library

The tqdm library is a popular choice for creating progress bars in Python. It provides a simple and flexible API for creating progress bars with minimal code. The tqdm library automatically calculates and displays the progress of the loop based on the iterable's length. It also provides additional features like elapsed time, estimated time remaining, and customizable appearance.

To use tqdm, we need to install it first using pip in the python environment.

Example

pip install tqdm

Output

F:\>pip install tqdm
Defaulting to user installation because normal site-packages is not writeable
Collecting tqdm
   Downloading tqdm-4.65.0-py3-none-any.whl (77 kB)
   ---------------------------------------- 77.1/77.1 kB 1.1 MB/s eta 0:00:00
Requirement already satisfied: colorama in c:\users\krishna\appdata\roaming\python\python311\site-packages (from tqdm) (0.4.6)
Installing collected packages: tqdm
   WARNING: The script tqdm.exe is installed in 'C:\Users\Krishna\AppData\Roaming\Python\Python311\Scripts' which is not on PATH.
   Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed tqdm-4.65.0

Example

In this example, we iterating over the data range, and tqdm wraps the loop, displaying a progress bar that updates in real-time after the completion of each iteration. The time.sleep(0.5) line simulates some work being done within each iteration.

from tqdm import tqdm
import time
data = range(10)
for item in tqdm(data):
   time.sleep(0.5)

Output

100%|██████████████████████████████████████████| 10/10 [00:05<00:00,  1.99it/s]

Manually updating the progress bar

In this approach, we manually calculate the progress percentage and update the progress bar by printing it to the console with a carriage return (\r) character to overwrite the previous line. The below is the example of using updating the progress bar manually by using the print statement.

Example

import time
total_iterations = 10 
for i in range(total_iterations):
   time.sleep(0.5)  
   progress = (i + 1) / total_iterations * 100
   print(f"Progress: {progress:.1f}%", end="\r")

Output

Progress: 10.0% 
Progress: 20.0% 
Progress: 30.0% 
Progress: 40.0% 
Progress: 50.0% 
Progress: 60.0% 
Progress: 70.0% 
Progress: 80.0% 
Progress: 90.0%
Progress: 100.0%

Using third-party libraries

Besides tqdm, there are other third-party libraries like progressbar2 and alive_progress that offer additional features and customization options for progress bars.

progressbar2

progressbar2 is another popular library that provides a range of progress bar styles and options. To use the progressbar2 library first we have to install it with pip.

pip install progressbar2

Example

Here, we are creating a progress bar with custom widgets using the widgets list. We specify the maximum value for the progress bar as 10 using the max_value parameter. The bar.update(i + 1) line updates the progress bar for each iteration.

from progressbar import ProgressBar
import time
total_iterations = 10  
with ProgressBar(max_value=total_iterations) as bar:
   for i in range(total_iterations):
      time.sleep(0.5) 
      bar.update(i)

Output

100% (10 of 10) |########################| Elapsed Time: 0:00:04 Time:  0:00:04

Alive-progress

Alive-progress is a modern, feature-rich library for creating interactive progress bars with advanced customization options. To work with alive-progress firstly we have to install the library using pip.

pip install alive-progress
Defaulting to user installation because normal site-packages is not writeable
Collecting alive-progress
   Downloading alive_progress-3.1.4-py3-none-any.whl (75 kB)
      -------------------------------------- 75.9/75.9 kB 842.2 kB/s eta 0:00:00
Collecting about-time==4.2.1 (from alive-progress)
   Downloading about_time-4.2.1-py3-none-any.whl (13 kB)
Collecting grapheme==0.6.0 (from alive-progress)
   Downloading grapheme-0.6.0.tar.gz (207 kB)
      -------------------------------------- 207.3/207.3 kB 4.2 MB/s eta 0:00:00
   Preparing metadata (setup.py) ... done
Building wheels for collected packages: grapheme
   Building wheel for grapheme (setup.py) ... done
Successfully built grapheme

Example

In this example, we use the alive_bar context manager to create a progress bar. The len(data) specifies the total number of iterations. The bar() function is called within the loop to update the progress bar.

from alive_progress import alive_bar
import time
data = range(10)
with alive_bar(len(data)) as bar:
   for item in data:
      time.sleep(0.5)
      bar()

Output

|████████████████████████████████████████| 10/10 [100%] in 5.1s (1.91/s) ←[K ←[?25h←[J

Updated on: 02-Aug-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements