Progress Bar in Python
In this tutorial, we are going to learn how to show Progress Bar in Python.
Note that this is not a part of GUI development and you can implement the functionality of the progress bar directly in your console.
A Progress Bar is an element that indicates the amount of process completed and the amount left.
The package we will use in this tutorial is called tqdm and so to use it, we first have to install it using pip in Python.
The code for the same is given below:
python -m pip install tqdm
Style 1 for progress bar in Python
from tqdm import tqdm loop = tqdm(total = 3000 , position =0, leave=False) for x in range(3000): loop.set_description("Loading ..".format(x)) loop.update(1) loop.close()
You can see a progress bar in white color indicating the percentage and ratio value of task left.
Here, we are creating an object first and storing it in variable called loop.
The ‘total’ parameter in tqdm function sets the total number of iterations to the given value. ‘start’ parameter determines from which number the progress bar should start indicating.
If ‘leave’ parameter is set to false, then it will disappear after completion. For reverse functionality, change the ‘leave’ parameter to ‘True’.
Then, using a for loop and with range as 300, we will update the description of the progress bar such that it displays every number in the given range with difference of 1.
STYLE 2 :
from tqdm import tnrange from time import sleep for x in range(5, desc='My Progress'): for j in tnrange(100, desc='Another Progress'): sleep(0.01)
This way of implementing is way more appealing, as the progress bar by itself changes it’s color at various stages of the progress.
Also, in this program upon execution you will notice that you will get one ‘My Progress’ Bar and 5 ‘Another progress’ bars.
Hence, you can use this approach when one task’s progress is further split into various tasks and you want to display to your user, the progress of all the tasks.
Therefore, now you can implement your own Progress Bars in Python.
For more info, visit https://pypi.org/project/tqdm/