Whenever you download a file or start a game, you see an aesthetic animation that updates itself until completed. This is a progress bar. A progress bar is a graphical element used to visualize the progress of a task such as downloading, uploading, or transferring files.

There are two types of progress bars: determinate and indeterminate. Determinate progress bars track the progress of a task over time. Indeterminate progress bars run infinitely with a looping animation.

So, how can you create a determinate progress bar in a Python CLI program?

What Is the tqdm Module?

tqdm is a module primarily maintained by Casper da Costa-Luis along with ten other members. tqdm derives from the Arabic word taqaddum which can mean "progress," and is an abbreviation for "I love you so much" in Spanish (te quiero demasiado).

Using the tqdm module you can make an attractive, functional progress bar right in your terminal. To install the tqdm module, open your terminal and run:

        pip install tqdm
    

What Is the time Module?

Python's standard utility module includes the time module by default, so you don't have to install it. You can use the sleep function provided in the time module to add delay to your applications which will help visualize the progress of the bar.

You can use the time module to get the date and time, schedule tasks, and build useful applications like a simple alarm clock, or a stopwatch.

How to Add a Progress Bar and Customize It

The default progress bar is easy to use and you can customize it with several options.

1. Simple Progress Bar

You can create a simple progress bar by importing the tqdm class from the tqdm module and the sleep function from the time module. Use a for loop and iterate tqdm on your desired range.

The range can be anything up to 9e9 (nine billion). Pass the number of seconds to the sleep function to add a delay so that you can visualize and see the progress bar in action.

You can implement a simple progress bar like this:

        from tqdm import tqdm
from time import sleep
 
for i in tqdm(range(100)):
    sleep(.1)

Instead of importing tqdm, you can import trange from the tqdm module to combine tqdm and range and pass the parameter directly.

        from tqdm import trange
from time import sleep
 
for i in trange(100):
    sleep(.1)

The output you obtain on creating a simple progress bar looks like this:

Simple Progress Bar

This output includes graphical and textual versions of the progress, along with timing statistics.

2. Progress Bar With Description Text

You can add a descriptive label to the progress bar to explain its purpose. Pass the text you want to display wrapped in double quotes as the desc parameter:

        for i in tqdm(range(0, 10), desc ="Progress: "):
    sleep(.4)

The output you obtain on adding a description text along with the progress bar looks like this:

Progress bar with text

3. Progress Bar With Customized Width

By default, the width of the progress bar is set dynamic to the size of the output window. You can customize it according to your liking with the help of the ncols parameter.

You can implement a progress bar with customized width as:

        for i in tqdm(range(0, 10), ncols = 100, desc ="Progress: "):
    sleep(.1)

The output you obtain on elongating the width of the progress bar looks like this:

Progress bar with description and ncols

4. Progress Bar Using Color

You can customize the color of the progress bar using the color parameter. You can do this in two ways:

  1. Color name: You can use the name of the color such as green, black, cyan, and more.
  2. Hex Code: Hex code is a format in which the computer stores the color. You can denote Hex color with a hash (#) followed by 6 characters in hexadecimal (0-9, a-f). The hex code #000000 represents black while #ffffff represents white. By varying the intensity of these numbers, you obtain different shades of colors. You can use the Color Picker tool from Google to select and paste the Hex Code color.

You can implement a progress bar with customized colors as:

        for i in tqdm(range(0, 100), colour="#00ffff", desc ="Progress: "):
    sleep(.1)

The output you obtain on changing the color of the progress bar:

Progress bar with color

5. Progress Bar With Minimum Interval

You can create a progress bar that progress that updates with a minimum interval instead of the default setting. You can pass a number such as 1.5 or 2 that will serve as the interval between two updates. The default value of the minimum interval is 0.1.

You can implement a progress bar with a minimum interval as:

        for i in tqdm(range(0, 100), mininterval = 2, desc ="Progress: "):
    sleep(.1)

The output you obtain by specifying a minimum interval to the progress bar:

Progress bar with min interval

6. Progress Bar Using ASCII Characters

You can create a progress bar using ASCII characters instead of the usual bars that you see on the screen. To use ASCII characters set the ascii parameter to the desired format.

If you use something like 12345*, each of the columns of the range iterates from one to asterisk sequentially. While it is cool and customizable, make sure to use user-friendly when using it in real applications.

You can implement a progress bar using ASCII characters as:

        for i in tqdm(range(0, 100), ascii ="12345*"):
    sleep(.1)

The output you obtain by using ASCII characters in the progress bar looks like this:

Progress bar with ASCII characters

7. Progress Bar Using Specified Start Point

Instead of starting a progress bar from zero, you can set a specific starting point for the progress bar. Pass a starting value like 50 to the initial parameter.

You can implement a progress bar using a specific start point as:

        for i in tqdm(range(0, 100), initial = 50, desc ="Progress: "):
    sleep(.1)

The output you obtain on starting the progress bar at a specified point:

Progress bar with initial parameter

8. Progress Bar With a Specified Number of Iterations

You can create a progress bar that runs a specific number of iterations. If you want to run 500 iterations out of 50000, pass 500 as the range and 50000 as the total number of iterations to the total parameter.

You can implement a progress bar using a specific number of iterations as:

        for i in tqdm(range(0, 500), total = 50000, desc ="Progress: "):
    sleep(.1)

The output you obtain using a progress bar with a specified number of iterations:

Progress bar with speicified number of iterations

Applications of the Progress Bar

You'll see progress bars in various situations, like the playback of a media player or showing the steps through an online form.

While a progress bar looks aesthetic it does an important job of providing feedback to the user in the absence of which they would leave the website.