How to create a progress bar with python ?
Display a progress bar on your terminal using python
2 min read
If you are extracting data from the web or doing an extremely complicated task, you will require a progress bar. These bars help in showing how much of the task is completed and is much more efficient than printing the result. This blog will teach you how to create such progress bars using python.
It is extremely easy to create a progress bar with python. You need to first install the tqdm library using pip. So run the following command
pip install tqdm
Importing the package
Now, in your python file, import tqdm
from tqdm import tqdm
Creating the progress bar
Say you have a time consuming function that you want to run multiple times. This function could perform web scraping or could train a certain machine learning model. Then, we would call this function inside a 'for loop' where we would specify the number of times we want to perform our function
for i in range(0, 50): MY_FUNCTION_NAME()
But if we want to show the progress, that is the number of times our function has been called in relation to the number of times it is intended to be called, we have to make our progress bar. This must be done with our tqdm package
for i in tqdm(range(0, 50)): MY_FUNCTION_NAME()
In the above code, the function is called 50 times and the progress bar is displayed in the output.
All the code
from tqdm import tqdm def MY_FUNCTION_NAME(): # Insert your function here pass for i in tqdm(range(0, 50)): # Calling our function 50 times and displaying how many times it has been called using a progress bar MY_FUNCTION_NAME()
That is it from this blog. Thank you for reading and happy coding !
Loading progress vector created by storyset - www.freepik.com