DEV Community

Cover image for Progress bars in Python!!!!
Muhimen
Muhimen

Posted on

Progress bars in Python!!!!

Ever wrote a super long loop and waited for the whole day for the loop to finish. And then you just found out there was a simple bug in your code for that reason your program stayed stuck at a certain place for a long amount of time? A progress bar can be a super simple way to find out on which state your loop is now and how long you have to wait for the loop to complete. This might also save you a huge amount of time. Perhaps you can get a cup of coffee meanwhile your loop completes. Anyway, without any further ado let's get looping. πŸ˜‰

First, you need to install a PyPi package called progress. If you already have pip installed then just type this command in terminal.

pip install progress
Enter fullscreen mode Exit fullscreen mode

It won't take much time. When you are done just run this simple script and see your bar progressing.

from progress.bar import Bar

bar = Bar('Processing', max=200000)
for i in range(200000):
    # Do some work
    bar.next()
bar.finish()
Enter fullscreen mode Exit fullscreen mode

That' it! The program is pretty self-explanatory and simple. We are just running a loop and then showing the bar.

Lucky for you, this package has a total of 7 progress bars and 5 predefined spinners. If you don't like the style of the bars or the spinners you can also customize them as much as you wish. For example, you can change the fill, you can change the width, you can add a simple timer that will tell how much time has elapsed and how much time is left.

For all the detailed information, consider visiting this documentation.

Until next time, goodbye.

Top comments (6)

Collapse
 
mburszley profile image
Maximilian Burszley

If you already have pip installed(by default it is installed in Mac and Linux)

This is wrong. By default, Debian does not have python or pip which are different packages in the apt environment.

Collapse
 
muhimen123 profile image
Muhimen

Oops, fixing right now. And thanks for correcting. 😊

Collapse
 
svemaraju profile image
Srikanth

progress

You should have added this gif to the post.
It's wonderful. Well done!

Collapse
 
muhimen123 profile image
Muhimen

I wanted to add some. But don't know how to. How did you do that?

Collapse
 
svemaraju profile image
Srikanth

Look up the section for images here. = dev.to/p/editor_guide

Some comments may only be visible to logged-in visitors. Sign in to view all comments.