03 Sep

Using TQDM

TQDM is awesome package. It's really buggy with its progress bar rendering for cells within notebooks, but it work great in Python script that your may be running in terminal. I use the package to track the progress of my for loop and my script as they scrape the internet for data. Below are a view example of the some use TQDM snippets.

Conda Install Command: https://anaconda.org/conda-forge/tqdm
Documentation: https://github.com/tqdm/tqdm

Import Preliminaries

In [1]:
# Import modules
import time
from tqdm import *

Simple Progress Bar

The simple progress is bar is only use alongside for loops in Python

In [2]:
# simple tqdm progress bar with list
for i in tqdm([1,2,3,4,5]):
100%|██████████| 5/5 [00:05<00:00,  1.00s/it]
In [3]:
# simple tqdm progress bar with range
for i in tqdm(range(5)):
100%|██████████| 5/5 [00:05<00:00,  1.00s/it]

Manualy Updating Progress Bar

In [4]:
# tqdm progress bar  with manula updates
with tqdm(total=100) as pbar:
    counter = 0 
    while counter < 100:
        counter+= 10
100%|██████████| 100/100 [00:10<00:00,  9.95it/s]

Updating Progress Bar Description

In [5]:
# Updating text in the tqdm progress bar
pbar = tqdm(["a", "b", "c", "d"])
for char in pbar:
    pbar.set_description("Processing %s" % char)
Processing d: 100%|██████████| 4/4 [00:02<00:00,  1.98it/s]
In [6]:
# Updating longer text in the tqdm progress bar
pbar = tqdm(range(0,10))
for char in pbar:
    pbar.set_description("Processing Page: %s" % char)
Processing Page: 9: 100%|██████████| 10/10 [00:05<00:00,  1.98it/s]

Nested TQDM Progress Bar

There is current a small bug with nested for loop functions.

In [7]:
# for i in tqdm(range(2)):
#     for i in tqdm(range(2)):
#         time.sleep(1)

If you run the commented out codef from above you will notice that each time the progress bar updates in your Jupyter notebook a new line for the progress bar is created . We can get around this by using tqdm HTML progress bar that the team created for notebooks.

In [8]:
# notebook specific tqddm progress bar for nested tqdm progress bars
from tqdm import tnrange, tqdm_notebook
from time import sleep

for i in tnrange(2, desc='1st loop'):
    for j in tqdm_notebook(range(2), desc='2nd loop'):

The output from the last cell above dose not render correctly on my website so here is a picture of showing output type. Feel free to run the code above to view the results in Juptyer.


Author: Kavi Sekhon