Progress Bars in Python – 3 Easy Methods for Implementation

Filed Under: Python Modules
Progress Bars Python

Hey, there programmer! As a programmer, we’ve all been in a situation where we start praying after we execute a code that it doesn’t get stuck anywhere. And if an error is displayed in the end, all the hard work goes to waste as we have no clue where our code went wrong. Frustrating right? In such situations, progress bars are a lifesaver!

When you happen to install software or any application, you mostly see a progress bar that gives you an estimate about how long the whole installing process is gonna take. In this tutorial, we will be learning the implementation of Progress Bras in the Python programming language. We will be implementing the same using a few different methods.

Method 1 – Creating Progress Bar in Python using the Progress Module

Progress is a simple Python library that comes with a variety of styles and you can display the bars with basic loops. Look at the code and cmd output below.

from time import sleep
from progress.bar import Bar

with Bar('Loading...') as b:
    for i in range(100):
        sleep(0.02)
        b.next()     

You can even customize the bars that appear on the screen. For instance, look at the code snippet and output below.

from time import sleep
from progress.bar import ChargingBar

with ChargingBar('Loading...') as b:
    for i in range(100):
        sleep(0.02)
        b.next()

Method 2 – Alive-Progress Progress Bars

If you are into animations and like to make this creative and different. You can display progress bars using the Alive-Progress library. Look at the code and output below.

from alive_progress import alive_bar
from time import sleep

with alive_bar(100) as bar:
    for i in range(100):
        sleep(0.03)
        bar()
with alive_bar(100, bar = 'bubbles', spinner = 'notes2') as bar:
    for i in range(100):
        sleep(0.03)
        bar()                       

Amazing isn’t it?

Method 3 – TQDM Python Library

TQDM is a fast framework that comes with a lot of customization options and the amazing part is that even with all these features, it is extremely simple to set up and work with.

Look at the code and output below.

from tqdm import tqdm
from time import sleep

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

Conclusion

Congratulations! Now you can proudly say that you know how to implement progress bars in just a few lines of code using Python.

Pretty simple, isn’t it? You can play around with the libraries and explore even more features.

Keep reading to learn more!

Happy coding! 😄

Also Read: Python System Command – os.system(), subprocess.call()

close
Generic selectors
Exact matches only
Search in title
Search in content