How do I implement a progress bar

Here is a solution (following this).

from ipywidgets import IntProgress
from IPython.display import display
import time

max_count = 100

f = IntProgress(min=0, max=max_count) # instantiate the bar
display(f) # display the bar

count = 0
while count <= max_count:
    f.value += 1 # signal to increment the progress bar
    time.sleep(.1)
    count += 1

If the value that's changing in the loop is a float instead of an int, you can use ipwidgets.FloatProgress instead.


Take a look at this open-source widget: log-process


You can try tqdm. Example code:

# pip install tqdm
from tqdm import tqdm_notebook

# works on any iterable, including cursors. 
# for iterables with len(), no need to specify 'total'.
for rec in tqdm_notebook(items, 
                         total=total, 
                         desc="Processing records"):
    # any code processing the elements in the iterable
    len(rec.keys())

Demo: https://youtu.be/T0gmQDgPtzY


In August 2020, the log-process widget is no longer an appropriate method to apply as it has been integrated into tqdm. The first tutorial example (using the new API) works out-of-the-box in VSCode, and I suspect it will work nicely in Jupyter as well.

from tqdm import tqdm
for i in tqdm(range(10)):
    pass

vscode notebook example

Update Febuary 2021:

Tqdm attempts to detect whether or not you're in a notebook, and tries to select the appropriate type of progress bar accordingly. It does so through a module tqdm.auto. I've observed cases where tqdm.auto selects the command-line variant (shown above) in a VSCode notebook rather than the tqdm.notebook variant.

This often has no ill effects, but can occsionally cause extremely verbose scrolling output. I've seen this occur while training tensorflow models in VSCode noteboks. Consequently, I'd recommend:

  1. from tqdm import tqdm - in library code or scripts
  2. from tqdm.notebook import tqdm - in notebooks