What is a progress bar in python?
A progress bar in python is similar to a progress bar in any tool or application. It shows the progress of a function, program, or any long-running operation. It provides a visual representation that the processing of a program is underway. Similarly, the progress bar in python shows how much a program has processed and how much processing is remaining. It prints the status bar on multiple lines with the percentage ranging from 0% to 100%, representing the progress of a program.
How to show a progress bar?
There are many packages of python programming language available that can be installed by using the pip method. Moreover, the easy_install method also comes in handy when the pip method does not work. However, the progress bar in python is based on old progress bar python packages launched on Google codes that are obsolete now. The python packages that the progress bar in python works with are backward compatible with the original python packages of the progress bar. As a result, they can be used as a drop-in replacement for current projects.
What packages of python are used to generate the progress bar?
Various python packages or methods are used to create and display the progress bar in python. Here, we will demonstrate some easy and practical methods with the help of examples to help you understand how you can display and design a progress bar for your python program. Let us see the examples of the python package used to design and display the progress bar below.
Example 1:
Here we will show a progress bar in python with sys.stdout.write() method. In this example, a simple for loop is used to generate the progress in a percentage from 0% to 100%, sys.stdout.write() function is used to print the progress as the string formatted. Finally, the time.sleep() displays the value of progress in the next progress bar after the seconds mentioned in the time.sleep(seconds) method. See the code below; you will better understand after executing the code yourself.
import time
for i in range(11):
sys.stdout.write("[%-1s] %d%%" % ('='*i, 10*i))
sys.stdout.write('\n')
time.sleep(0.30)
The output of the code is given below. As you can see, each percentage is displayed in a separate bar with a difference of 10%. This is because the part (‘=’*i, 10*i) shows the progress number as the string formatted and multiples of 10 in each for loop iteration.
Example 2:
The progressbar.progressbar() is a python’s built-in function that allows users to display the progress bar in the string format. In this example, time.sleep() function is used again to display the next bar after the desired number of seconds given in the function. The ‘bar’ module of progressbar.progressBar() function is used to display the bar in the program. The for loop will iterate from 0 to 10 times, and the bar.update() function will be updated in every iteration. See the code given below to have a better understanding.
import progressbar
with progressbar.ProgressBar(max_value=10) as bar:
for a in range(10):
bar.update(a)
time.sleep(0.1)
Here is the output of the example given above.
Example 3:
The tqdm library in the python programming language allows us to visually illustrate the progress of a program’s execution. It visually represents the progress bar that shows the process completion time. The built-in methods in the tqdm library wrap an iterable to make a progress bar. It is installed with the pip module of python, and all you have to do is type the code given below:
for a in tqdm (range (100), desc="Loading...."):
pass
As you can see in the output given below, it shows a fast progressing bar showing the process completion time.
Conclusion:
Here we have learned about the progress bar in python and how that can be designed and displayed in the program showing the process completion time and percentage of execution that has been done so far. We have demonstrated three methods; sys.stdout.write(), progressbar.progressBar(), and tqdm library.