Updated March 29, 2023
Introduction to Progress bar Python
A progress bar is a graphical control element that shows the status of a long-running computer process like a download, file transfer, or installation. A written representation of the progress in a percent format is sometimes included with the visual. When we don’t know how long a procedure will take, such as a for loop, downloading a file, or opening up an application, updating software, we understandably become impatient. Thus, to distract users from that, we use progress bars. In this article, we are going to learn about Progress bar Python, and we will see how to make a progress bar in python.
By Using the tqdm package
tqdm is a Python module that allows you to create Progress Meters or Progress Bars. To install it, you need to write the following code, or you can just copy-paste it.
pip install tqdm
Now restart the Kernel to start using the tqdm, and after doing this, you can check whether the tqdm library has been installed or not by again writing the above code, and if you will see the below statement, then it is installed correctly else not.
Requirement already satisfied: tqdm in d:\anaconda new\lib\site-packages (4.36.1)
Now, after installing, Import the newly installed tqdm and time library
from tqdm import tqdm from time
You don’t need to install as time is part of Python’s standard library; it comes with Python and is available automatically.
from tqdm import tqdm import time for i in tqdm(range(10)): time.sleep(0.1)
In the for loop, the i variable will take a value of 0 to 9 during every iteration. In the time.sleep() method, the system will sleep for 0.1 seconds between iterations before going on to the next.
Above is a simple example; let us learn more features of it.
b) tqdm also takes attributes like desc(), which basically means description, which works as a prefix to the progress bar.
from tqdm import tqdm from time for i in tqdm(range(10),desc="Downloading"): time.sleep(0.1)
Output: We can see that ‘Downloading’ as a prefix before the progress bar.
from tqdm import tqdm from time import sleep for i in tqdm(range(0,100),mininterval = 1, desc="minInterval"): time.sleep(0.1)
mininterval keeps changing the progress bar displays the number of times the time mentioned in the sleep() method.
from tqdm import tqdm from time import sleep for i in tqdm(range(0,100), disable = True, desc="minInterval"): time.sleep(0.1)
disable attribute will make your progress bar completely not visible; it takes Boolean values True and False, also make sure they are case sensitive.
disable = True, which will give make your progressbar invisible, on the other hand disable = False the progressbar will be visible.
ncols attribute is set to specify the length of the output of the progress bar. Below are two examples that will clear this,
from tqdm import tqdm from time import sleep for i in tqdm(range(0, 100), ncols = 100, desc ="ncols Example"): sleep(.1)
from tqdm import tqdm from time import sleep for i in tqdm(range(0, 100), ncols = 75, desc ="ncols Example"): sleep(.1)
Look carefully in both of the outputs for the codes has given the range of 0 – 100 in the for loop in both the cases, but in case 2, the ncols attribute is set to 75 Thus, the output progress bar is loaded or shown 75% out off 100%.
trange(i) works same as tqdm(range(i)) :
from tqdm import trange from time import sleep for i in trange(100, desc = "Trange"): time.sleep(0.1)
By default, the progress bar starts from 0, But if you want to customize it, you can use the initial attribute
from tqdm import tqdm from time import sleep for i in tqdm(range(0, 100), initial = 50, desc ="Initial = 50"): sleep(.1)
Output: The counter would begin at 50 and would disappear after the final counter had been achieved. The loop would continue to run until the given range.
2) Using tqdm_notebook( )
In contrast to the tqdm (), tqdm_notebook() generates coloured progress bars. By default, it contains three colour sets.
The three colors are as follows :
- BlueBar – It is a moving bar that shows for a process undergoing.
- GreenBar – It shows that the process is completed.
- RedBar – It shows that process is being stopped or interrupted.
tqdm_notebook() is also like same as tqdm() in implementation
from tqdm.notebook import tqdm_notebook from time for i in tqdm_notebook(range(100)): sleep(.1)
Case 1: When the bar is in progress, it is showing blue color.
Case 2: When the process is finished, it is showing green color.
Case 3: When the process is interrupted, as we can see below, it is showing red color.
from tqdm.notebook import tqdm_notebook from time for i in tqdm_notebook(range(2), desc = 'Nested Loop 1'): for j in tqdm_notebook(range(0,5), desc = 'Nested Loop 2'): time.sleep(0.5)
Output: Here, two for loops, Nested Loop 1 and Nested Loop 2, will be executed the loop 1 will execute twice, and the second loop will be executed five times, in tqdm_notebook it shows one progress bar for loop 1, and for each iteration of loop 1, it will show one progress bar of loop 2.
Try out the same thing with tqdm(), it will not be the same with the case of tqdm() because Suppose if you would take two nested for loops, then loop 1 will execute each iteration, it will show a separate progress bar, and in each iteration of Loop 1, it will show different progress bars for each iteration of Loop 2. If you had followed this whole article well, you can try it out on your own; you will learn fast this way. Good luck!
Conclusion – Progress bar Python
Progress Bars would allow us to track the progress of our execution while also reducing our anxiousness. Furthermore, it helps the developers keep the users as they are not certain how much execution time their code will take. Progress Bars are mostly seen in games, updates, downloads, etc.
This is a guide to Progress bar Python. Here we discuss how to make a progress bar in python along with the examples and outputs. You may also have a look at the following articles to learn more –