Python Tqdm: Making Progress Bar Made Easy

It is a proven fact that we humans love visuals and understand them better than anything else. The same is the case when we are working with large datasets and doing processing jobs. If you know deep learning, you will agree with me how boring it gets when we don’t know how much time it will take for our model to build. But suppose if we have some progress bars that show us how much progress we have made and how much time it is left, wouldn’t that be great. Yes, that is possible. Possible if we use the tqdm library in python.  

Using the tqdm library, we can make console line progress bars and progress bars with GUI. By utilizing these progress bars, we can see if we are getting stuck somewhere and work on that immediately. Also, when we know how much time it will take us to complete the task, we can give our clients actual time for delivery. 

Installing and Using Python tqdm  

It’s not like tqdm are the only way of making progress bars in python, there are many other methods too. But working with tqdm is a lot easier than many of them. To use it, we first need to install it. 

To install it use- pip install tqdm 

There are multiple parameters in a tqdm; let us understand them one by one. 

Parameters in Python Tqdm 

Iterable– It can be a range, a list whose progress we have to check. 

Example- 

#Importing tqdm function of tqdm module 
from tqdm import tqdm  
from time import sleep 
for i in tqdm(range(200)):  
# Waiting for 0.01 sec before next execution 
   sleep(.01) 

  Output- 

100%|██████████| 200/200 [00:02<00:00, 94.85it/s] 

The above output shows that a total of 200 iterations took place at a speed of 94.85 iterations per second. The total time was approx. 2 seconds. 

desc: Using this parameter, we can specify the text we want to show before the progress bar.

For Example- 

from tqdm import tqdm 
for i in tqdm(range(int(5000000)), desc="Progress"): 
   pass 

Output- 

Progress: 100%|██████████| 5000000/5000000 [00:01<00:00, 4389678.95it/s]  

The above output shows that 5000000 iterations have taken place at a speed of 4389678.95it/s.  

Disable:- If we do not need to show the progress bar, we can set disable=True. By default, disable is set to False. 

for i in tqdm(range(0, 100), disable=True): 
    sleep(.01) 
print("Done") 
Output- 
Done 

ncols: This parameter is used to specify the number of columns in which we want to display the output.  

To better understand this parameter, we will try to give two different arguments to this parameter. 

from tqdm import tqdm 
from time import sleep

for i in tqdm(range(0, 100),ncols=100): 
   sleep(.1)

Output- 

100%|█████████████████████████████████████████████████████████████| 100/100 [00:10<00:00, 9.94it/s] 
from tqdm import tqdm 
from time import sleep 

for i in tqdm(range(0, 100),ncols=50): 
   sleep(.1)
100%|███████████| 100/100 [00:10<00:00, 9.95it/s] 

ascii: Using this parameter, we can give whatever value we want to in the progress bar. You will better understand this with an example. 

from tqdm import tqdm 
from time import sleep 

for i in tqdm(range(0, 100),ascii="**"): 
   sleep(.1) 

Output- 

100%|**********| 100/100 [00:10<00:00, 9.95it/s] 

Examples of Python tqdm Using List Comprehension 

from time import sleep
from tqdm import tqdm
list1 = ["My","Name","Is","Ashwini","Mandani"]
# loop through the list and wait for 2 seconds before execution of next
list1 = [(sleep(2), print(i)) for i in tqdm(list1)]

Output- 

20%|██ | 1/5 [00:02<00:08, 2.00s/it]My 
40%|████ | 2/5 [00:04<00:06, 2.00s/it]Name 
60%|██████ | 3/5 [00:06<00:04, 2.00s/it]Is 
80%|████████ | 4/5 [00:08<00:02, 2.00s/it]Ashwini 
100%|██████████| 5/5 [00:10<00:00, 2.00s/it]Mandani 

Python tqdm With GUI

You can also make the progress bar with GUI which can surely help you understand better about the progress. Python tqdm library has a separate function ‘tqdm_gui’ for this task with almost the same function just different names and Interface.

from time import sleep
# Install tdgm_gui from tqdm instead of just tqdm
from tqdm import tqdm_gui
list1 = ["My","Name","Is","Ashwini","Mandani"]
# Use tqdm_gui
list1 = [(sleep(2), print(i)) for i in tqdm_gui(list1)]
tqdm python

Tqdm With Map Python

Sometimes, we need to use Tqdm with the functions like map, reduce, or filter. Integrating the tqdm can be quite tricky on these functions can be tricky as the map function returns a generator rather than a list. The following example can help you to understand the implementation of tqdm with a map in python.

Code –

from tqdm import tqdm
list(map(str, tqdm(range(10000000))))

Output –

100%|████████████████| 10000000/10000000 [00:03<00:00, 2594327.01it/s]

Explanation –

We first import the tqdm module which is expected to receive an iterable object in it. In this specific example, we’ve used the range() function to provide an iterable for tqdm. You can use any iterable according to your need. Then we mapped the tqdm object with an str() function to get the result.

Integrating Pandas Data Frame and Python tqdm 

import pandas as pd
import numpy as np
from tqdm import tqdm
# Creating random dataset
dataset = pd.DataFrame(np.random.randint(0, 100, (10, 6)))

tqdm.pandas(desc="progress bar")

# Now you can use `progress_apply` instead of `apply`
# and `progress_map` instead of `map`
dataset.progress_apply(lambda x: x**2)

Output- 

progress bar: 100% ████████████████████████████████████████████████████████████████████| 6/6 [00:00<00:00, 2006.20it/s] 
 0 1 2 3 4 5 
0 8649 1764 9801 9801 3364 
1 2209 7396 5929 8464 25 
2 6561 441 100 1521 2916 169 
3 6084 121 784 9801 7744 1296 
4 7396 2500 81 100 7569 
5 4761 4624 7056 121 25 6400 
6 225 5476 6241 3249 196 7744 
7 2401 4489 5625 676 36 6724 
8 25 2809 4489 1369 729 8464 
9 64 9801 2209 900 1225 4225 

Must Read

Conclusion- 

We have discussed almost everything you will need to make use of python tqdm in real life. We used it on ranges, lists, and data frames. Try to explore more by using it on Files.  

Try to run the programs on your side and let us know if you have any queries.

Happy Coding!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments