How to Display a Progress Bar in Python

[😁😁😁😁😁😁😁😁😁😁😁😁😁😁😁😁😁😁😁😁] 100%

Problem Formulation and Solution Overview

In this article, you’ll learn how to configure and display a progress bar.

A progress bar is commonly used in Python or, for that matter, any other programming language to show the user an application’s progress. For example, an installation, a transferring of files, or any other commands.

The Finxter Academy recommends implementing this to visually display to the user what is happening behind the scenes.


πŸ’¬ Question: How would we write code to display a progress bar?

We can accomplish this task by one of the following options:


Method 1: Use a For Loop

This method imports the time and sys libraries combined with a for loop to display a custom progress bar output on a single line.

from time import sleep
import sys

for x in range(0,21):
    sys.stdout.write('\r')
    sys.stdout.write("[%-20s] %d%%" % ('😁'*x, 5*x))
    sys.stdout.flush()
    sleep(0.75)

Above imports, the time library to call the sleep command and the sys library to write the contents to the terminal.

Next, a for loop is instantiated. This loop uses the range function to set the start position (0 by default) and the stop position (21-1). Inside this loop, the following occurs:

  • The first line uses a carriage return (\r) to start the code on a new line.
  • The following line determines how the progress bar appears by:
    • Left-aligning the emoji(s) inside the square brackets [%-20s].
    • Configuring the progress percentage %d%%" % (example 15%).
    • Determining the visual emoji progress ('😁'*x, 5*x).
  • Then sys.stdout.flush() writes everything in the buffer to the terminal.
  • The code pauses for the stated period (0.75).

The loop continues until 100% has been attained. Below is the end result.

[😁😁😁😁😁😁😁😁😁😁😁😁😁😁😁😁😁😁😁😁] 100%

πŸ’‘Note: Any emoji or another symbol can be substituted for the above selection.


Method 2: Use apytl Library

This method imports the time and apytl libraries to generate a custom progress bar.

To run this code error-free, install the required library. Click here for installation instructions.

import time
import apytl

total_iterations = 10

for index, value in enumerate(range(total_iterations)):
    apytl.Bar().drawbar(value, total_iterations, fill='*')
    time.sleep(0.75)

Above imports the time library to call the sleep command and the apytl library to display the progress bar.

Next, the total number of iterations to carry out is declared as 10 and saved to total_iterations.

To understand what is going on in the for loop, let’s write some test code to see what the values of index, value and total_iterations are doing:

total_iterations = 10

for index, value in enumerate(range(total_iterations)):
    print(index, value, total_iterations)

As you can see from the output, the index and value variables count from 0 (range default start position) to the stop position of 9 (10-1). Notice the value of total_iterations does not change.

0 0 10
1 1 10
2 2 10
3 3 10
4 4 10
5 5 10
6 6 10
7 7 10
8 8 10
9 9 10

Referring back to the code directly below the Method 2 heading, the next line is: apytl.Bar().drawbar(value, total_iterations, fill='*').

This function is passed three (3) arguments:

  • The variable value, which you can see from the above, increments by one (1) each iteration.
  • The variable total_iterations, which remains constant at 10.
  • The fill value. This example uses the asterisk (*) character. However, feel free to use an emoji or other symbol.
Progress |***********************************| 100.0% Complete

Method 3: Use alive-progress

The alive-progress library moves progress bars to the next level! With its many display options, it’s a must-a-try!

To run this code error-free, install the required library. Click here for installation instructions.

from alive_progress import alive_bar
from time import sleep

for x in range(10):
    with alive_bar(x, bar='solid') as bar:
        for i in range(x):
            sleep(.001)
            bar()

Above imports the alive-progress library to use the progress bar and the time library to call the sleep command.

Next, a for loop is instantiated. This loop uses the range function to set the stop position at 9 (10-1). Inside this loop, the following occurs:

  • The alive_bar is called and passed the argument x and the bar type. For this example, solid was selected.
    • A new for loop is instantiated and loops through range passing x as an argument.
    • The code halts (sleep) for the stated period (.001).
    • The bar is output to the terminal each iteration.

The loop continues until 100% has been attained.

<β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– > 0 in 0.0s (0.00/s)
<β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– > 1/1 [100%] in 0.0s (333.28/s)
<β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– > 2/2 [100%] in 0.0s (62.48/s)
<β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– > 3/3 [100%] in 0.0s (63.96/s)
<β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– > 4/4 [100%] in 0.1s (62.39/s)
<β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– > 5/5 [100%] in 0.1s (63.32/s)
<β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– > 6/6 [100%] in 0.1s (64.84/s)
<β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– > 7/7 [100%] in 0.1s (63.79/s)
<β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– > 8/8 [100%] in 0.1s (63.53/s)
<β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– > 9/9 [100%] in 0.1s (64.09/s)

πŸ’‘Note: To view a list of all available progress bars, click here.


Method 4: Use tqdm

A simple yet elegant progress bar, tqdm is a must-see!

To run this code error-free, install the required library. Click here for installation instructions.

from tqdm import tqdm
from time import sleep

for i in tqdm(range(100)):
    sleep(0.02)

Above imports the tqdm library for the progress bar and the time library to call the sleep() command.

Next, a for loop is instantiated. This loop uses the tqdm library with the range function to set the stop position at 99 (100-1). Inside this loop, the following occurs:

The progress bar displays on one line. Each iteration increases the progress by a percentage.


Method 5: Use progress Library

This method uses the progress library to display a progress bar. This library has many fun options to select from.

To run this code error-free, install the required library. Click here for installation instructions.

from time import sleep
from progress.spinner import MoonSpinner

with MoonSpinner('Processing…') as bar:
    for i in range(100):
        sleep(0.02)
        bar.next()

Above imports the time library to call the sleep command and the progress library to display a progress bar, specifically the MoonSpinner.

The following code displays the word Processing... at the terminal, and the MoonSpinner rotates, sleeps, and continues until the value of the range stop position (100-1) is attained.

Processing…◑

πŸ’‘Note: Spend some time on this library page to test the different types of progress bars.


Summary

There is so much more to Python Progress Bars than was covered in this article. May we suggest you take time to delve into each method outlined above to determine the best one for your requirements.

Happy Coding!


Programming Humor – Python

“I wrote 20 short programs in Python yesterday. It was wonderful. Perl, I’m leaving you.”xkcd