Progress Bar in Python

  1. Use the tdqm Module in Python to Create Progress Bars
  2. Use the progressbar Module in Python to Create Progress Bars
  3. Create Your Own Function in Python to Create Progress Bars

In the world of programming, we work with large volumes of code. A program might take a lot of time to compile and run. Therefore, it is a little helpful to know how much progress has been made and the estimated time of completion of the program. For such situations, we can use progress bars in Python. Moreover, they can help us identify if our program has stopped in the middle or not.

In Python, we have many libraries available which can help us to create a simple progress bar for our program to help us with this. We will discuss each method one by one in this tutorial.

Use the tdqm Module in Python to Create Progress Bars

The tdqm library has a swift and efficient framework for creating basic progress bars. It works well on terminal, python notebooks and is compatible with Windows, Linux, and Mac OS.

In the following code, we create a simple progress bar using functions from this module.

from time import sleep
from tqdm import tqdm
for i in tqdm(range(10)):
    sleep(0.2)

We iterate through a loop, and on every iteration, the progress bar is also incremented. The sleep() function is used to pause the compiler for some specified seconds. Note that by default, the bar shows the time remaining, iterations per second, and percentage.

Out of all the modules discussed in this tutorial, this one has the least overhead.

Use the progressbar Module in Python to Create Progress Bars

The progressbar module does almost everything like the tdqm module. In addition to this, it has options to animate your progress bar and use widgets displayed on the progress bar.

The following code shows an example of this.

import time
import progressbar

for i in progressbar.progressbar(range(100)):
    time.sleep(0.02)

The progressbar2 also exists in Python and is a fork of the progressbar. It is extremely popular due to its powerful and customizable features. It also supports print statements with the progress bar. On the downside, this module hasn’t been maintained in years.

Note that more libraries are available like alive-progress, progress that can help make the progress bars, but these two are the most frequently used. The only difference between them is the animations and other customizations which these libraries offer.

Create Your Own Function in Python to Create Progress Bars

We can also create a simple function that can resemble a progress bar. The code below implements this.

import sys

def progressbar(it, prefix="", size=60, file=sys.stdout):
    count = len(it)
    def show(j):
        x = int(size*j/count)
        file.write("%s[%s%s] %i/%i\r" % (prefix, "#"*x, "."*(size-x), j, count))
        file.flush()
        file.write("\n")
    show(0)
    for i, item in enumerate(it):
        yield item
        show(i+1)
        file.write("\n")
    file.flush()
    
import time

for i in progressbar(range(15), "Computing: ", 40):
    time.sleep(0.1)

Output:

Computing: [........................................] 0/15
Computing: [##......................................] 1/15
Computing: [#####...................................] 2/15
Computing: [########................................] 3/15
Computing: [##########..............................] 4/15
Computing: [#############...........................] 5/15
Computing: [################........................] 6/15
Computing: [##################......................] 7/15
Computing: [#####################...................] 8/15
Computing: [########################................] 9/15
Computing: [##########################..............] 10/15
Computing: [#############################...........] 11/15
Computing: [################################........] 12/15
Computing: [##################################......] 13/15
Computing: [#####################################...] 14/15
Computing: [########################################] 15/15

We calculate the total iterations required, and over every iteration, we increment the # symbol and print it.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.