Callysto.ca Banner

Open in Callysto

Implementing a progress bar

There are times when running code takes a long time. If you are an experienced Jupyter user, you will know that the * symbol on the left-hand side of each cell indicates that the notebook is currently busy with that cell.

For someone who has never seen the Jupyter environment before, it can seem as though the notebook is not doing anything.

To solve this problem, we implement a visual mechanism that indicates that the notebook is busy along with information on running time.

Basic progress bar

We implement a basic version of a progress bar.

We will use the for loop syntax and we replace range for tnrange. This is similar to the usual for loop syntax, except tnrange will also allow us to give a description to the loading bar.

We will use the sleep command from the time library.

# Import necessary modules
from tqdm import tnrange, tqdm_notebook
from time import sleep
# Basic implementation
for i in tnrange(100, desc='Basic Progress Bar'):
    startRow = 1 + i*100
    sleep(0.01)

Downloading data from a webpage

What if we want to download data and want to communicate with the user that this process is taking some time?

We will download a series of metadata entries from the Historical Climate Weather page as an example

# Import requests library for downloading data from online database
import requests
# Implementing progress bar when querying data from online database
for i in tnrange(100, desc='Downloading Data'):
    startRow = 1 + i*100
    sleep(0.01)
    
    base_url = "http://climate.weather.gc.ca/historical_data/search_historic_data_stations_e.html?"
    queryProvince = "searchType=stnProv&timeframe=1&lstProvince={}&optLimit=yearRange&".format("BC")
    queryYear = "StartYear={}&EndYear=2017&Year=2017&Month=5&Day=29&selRowPerPage=100&txtCentralLatMin=0&txtCentralLatSec=0&txtCentralLongMin=0&txtCentralLongSec=0&".format("2017")
    queryStartRow = "startRow={}".format(startRow)

    response = requests.get(base_url + queryProvince + queryYear + queryStartRow) # Using requests to read the HTML source


You can learn more about the tqdm package here and the sleep package here.

Callysto.ca License