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.
Contents of Tutorial
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.
Iterable– It can be a range, a list whose progress we have to check.
#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)
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.
from tqdm import tqdm for i in tqdm(range(int(5000000)), desc="Progress"): pass
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")
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)
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)
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)]
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)]
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)
progress bar: 100% ████████████████████████████████████████████████████████████████████| 6/6 [00:00<00:00, 2006.20it/s]
- How to Convert String to Lowercase in
- How to Calculate Square Root
- User Input | Input () Function | Keyboard Input
- Best Book to Learn Python in 2020
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.