Skip to content

Instantly share code, notes, and snippets.

@qin-yu
Created April 11, 2020 10:10
Show Gist options
  • Save qin-yu/f29190c10172ddd3fb18cdf82054c831 to your computer and use it in GitHub Desktop.
Save qin-yu/f29190c10172ddd3fb18cdf82054c831 to your computer and use it in GitHub Desktop.
How to Use Progress Bar in Jupyter Notebook (light blue bar with question mark denominator explained)

How to Use Progress Bar in Jupyter Notebook

Qin Yu, Apr 2020

Install ipywidgets

ipywidgets are interactive HTML widgets for Jupyter notebooks, JupyterLab and the IPython kernel.

pip install ipywidgets
jupyter nbextension enable --py widgetsnbextension

Install tqdm

pip install tqdm

Use tqdm in loop

IPython/Jupyter is supported via the tqdm.notebook submodule:

from tqdm.notebook import trange, tqdm
from time import sleep

for i in trange(3, desc='1st loop'):
    for j in tqdm(range(100), desc='2nd loop'):
        sleep(0.01)

tqdm no ETA

Some times the progress bar is showing 23/? in light blue, saying its currently on the 23rd item without the knowledge of a total.

e.g. Use tqdm with os.scandir()

This will give a light blue progress bar whose total task number is ?:

with os.scandir(path) as dir_img:
    for entry in tqdm(path):  # `tqdm.notebook.tqdm()` doesn't work with `os.scandir()` directly
        if entry.name.endswith(".py") and entry.is_file():
            pass

By telling tqdm() the explicit form of iterator it is dealing with, a proper bar is shown. Just list() the os.scandir():

with os.scandir(path) as dir_img:
    for entry in tqdm(list(path)):  # `tqdm.notebook.tqdm()` doesn't work with `os.scandir()` directly
        if entry.name.endswith(".py") and entry.is_file():
            pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment