Create progress bar in Python CLI

Every time you download a file or start a game, you see an aesthetic animation that updates until it’s finished. 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: definite and indefinite. Fixed progress bars track the progress of a task over time. Indefinite progress bars run endlessly with looping animation.

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


What is the tqdm module?

tqdm is a module mainly maintained by Casper da Costa-Luis 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).

By using the tqdm module, you can create an attractive and 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 feature provided in the time module to add a delay to your apps, which will help you view the progress of the bar.

You can use the time module to get date and time, schedule tasks and create 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 through tqdm over the desired range.

The range can be up to 9e9 (nine billion). Switch the number of seconds to sleep function to add a delay so you can preview 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 get from creating a simple progress bar looks like this:

This output includes graphical and text versions of progress, as well as timing statistics.

2. Progress bar with descriptive text

You can add a descriptive label to the progress bar to explain its purpose. Pass the text you want to display surrounded by double quotes like desc setting:

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

The output you get by adding description text with the progress bar looks like this:

3. Progress bar with custom width

By default, the progress bar width is dynamically set to the size of the output window. You can customize it to your liking using the ncols setting.

You can implement a progress bar with a custom width like:

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

The output you get from extending the width of the progress bar looks like this:

4. Progress bar using color

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

  1. Color name: You can use the color name like green, black, cyan, etc.
  2. Hexadecimal code: Hexadecimal code is a format in which the computer stores color. You can specify the Hex color with a pound sign (#) followed by 6 hex characters (0-9, af). The hexadecimal code #000000 represents black while #ffffff represents white. By varying the intensity of these numbers, you get different color shades. You can use Google’s Color Picker tool to pick and paste the hex code color.

You can implement a progress bar with custom colors like:

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

The output you get by changing the color of the progress bar:

5. Progress bar with minimum interval

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

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

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

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

6. Progress bar using ASCII characters

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

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

You can implement a progress bar using ASCII characters like:

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

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

7. Progress bar using specified starting point

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

You can implement a progress bar using a specific starting point like:

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

The output you get by starting the progress bar at a specified point:

8. Progress bar with 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 setting.

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

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

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

Progress bar applications

You’ll see progress bars in various situations, like playing a media player or viewing steps through an online form.

Although a progress bar looks aesthetically pleasing, it does an important job of providing feedback to the user otherwise they would leave the website.

About Dwayne Wakefield

Check Also

How to Remove Microsoft Account from Windows 11

With the Windows 8 makeover in 2012, the software giant made it quite difficult to …