Python Progress Bar
Overview
We often become impatient while waiting for some process to complete and there is no progress bar! It’s like your friend asks you to wait at the bus stop and when you reach, they stop answering your phone calls!
In Python, to lessen the pain of waiting, we have some visuals that let us know the progress status of the process, thus giving us an estimate of how long it will take to complete the task. These are known as Python progress bars.
What is a Python Progress Bar?
A Python progress bar is a visual that lets us know how long will a task take to complete or what is the progress status of the task.
For codes that work on large data sets, like some projects in Machine Learning, you need something to let you know if your code is stuck on an infinite loop or how much progress has the code done. For such purposes, we have some libraries in Python that allow us to add progress bars.
Different Ways to Add a Progress Bar in Python
There are two widely used ways (by “ways” I mean library) to add a progress bar in Python:
- tqdm library
- progressbar or progressbar2 library
Making a Python Progress Bar Using the tqdm Library
Installation
For installing the tqdm library you just need to do it like any other library.
Working
Let us take some task, and execute them in a loop.
The above statement sleep(1) takes one second to execute. So, when we loop it 5 times, it will take 5 seconds to execute. But there is no progress bar to track that. Let us make a simple progress bar using tqdm.
You need to write the following code to make the Python progress bar.
Output:
Example
We will add some new attributes in each example and see what it results in.
In a Python progress bar, we have multiple new attributes, let’s check them out:
- desc: description of the task.
- ascii: we can form the progress bar using different ascii characters of our choice. Example: for the $ sign, we need to put “123456789$” as ascii value. False will result in the standard progress bar as we can see.
- ncols: this decides the width of the progress bar.
- total: the total number of tasks to be done
- mininterval: the minimum time after which the progress bar should update. It is by default 0.1.
- units: we can specify the time unit we want to use. By default, it is “it”. Here, we set the unit to "ticks"
- initial: we can specify the initial starting point of the progress bar, in our case we say 50% of 200 tasks are done initially, so it starts thereafter.
Try messing up with these values to learn more!
Output:
Making a Python Progress Bar using Progressbar Library
Installation
This is similar to any other library,
Working
This is very similar to the tqdm progress bar, but has some extra features that allow us to create animated progress bars. The syntax is also quite simple and easy to understand when compared to tqdm library.
This is a simple progress bar that does 100 iterations and each iteration is of 0.01 seconds.
Output:
We also have a widget object that defines the bar with attributes like:
- The ascii character which should be used to form the bar.
- The estimated time for completion.
- We can set the timer format to show how much time has elapsed, and many more other things.
Example
Let us take an example and see how this library works. We will make an animated progress bar with $ sign, showing the estimated time to complete the task, and the time elapsed.
Code:
Output:
More Examples for Understanding Python Progress Bars
Example 1
Let us iterate over a list of strings and display them along with a progress bar to track the progress.
Code:
Output:
In the above example, there are 5 strings and we take 1 second to print a string. So in total, we needed 5 seconds to print the entire list. And for each string, there is 20% progress.
Example 2
We can make our progress bars colorful!
Code:
Output:
FAQs
Q: What are some practical use cases for making a progressbar in Python?
A: We make progressbar in Python in case of some machine learning project where we need to train a model with a huge data set, so keeping track lets us know how long it will take for completing the training.
Q: Can we make a progress bar in Python like we can see in windows while copying some files?
A: There is something known as tkinter progress bar widget (tkinter documentation), that is used for making GUI progress bars in Python.
Conclusion
That was all about one of the cool things you can do in Python, let us take a summary:
- Progress bars are visuals that allow us to keep a track of some running tasks, giving us an estimation of how long it will take for completion.
- In Python, there are primarily two libraries used for making progress bars:
- tqdm library (Documentation)
- progressbar or progressbar2 library (Documentation)
- For making GUI progressbars in Python we use the tkinter library.