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¶
# Import modules
import time
from tqdm import *
Simple Progress Bar¶
The simple progress is bar is only use alongside for loops in Python
# simple tqdm progress bar with list
for i in tqdm([1,2,3,4,5]):
time.sleep(1)
# simple tqdm progress bar with range
for i in tqdm(range(5)):
time.sleep(1)
Manualy Updating Progress Bar¶
# tqdm progress bar with manula updates
with tqdm(total=100) as pbar:
counter = 0
while counter < 100:
time.sleep(1)
counter+= 10
pbar.update(10)
Updating Progress Bar Description¶
# Updating text in the tqdm progress bar
pbar = tqdm(["a", "b", "c", "d"])
for char in pbar:
time.sleep(.5)
pbar.set_description("Processing %s" % char)
# Updating longer text in the tqdm progress bar
pbar = tqdm(range(0,10))
for char in pbar:
time.sleep(.5)
pbar.set_description("Processing Page: %s" % char)
Nested TQDM Progress Bar¶
There is current a small bug with nested for loop functions.
# 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.
# 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'):
sleep(1)
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