Python Engineer

Free Python and Machine Learning Tutorials

Become A Patron and get exclusive content! Get access to ML From Scratch notebooks, join a private Slack channel, get priority response, and more! I really appreciate the support!

How To Add A Progress Bar In Python With Just One Line

02 Feb 2020

In this Python Tutorial I show you how you can add a Progress Bar to your Python code in just one line of code! We use the great tqdm module for this that can simply be installed with pip install tqdm. I show you the iterable based and the manually controlled approach.

`tqdm can be found on GitHub.
The code from this Tutorial can also be found on GitHub.

Installation

pip install tqdm

Iterable based approach

from tqdm import tqdm, trange import time for i in tqdm([1, 2, 3, 4, 5]): time.sleep(0.2) print('done')

Special optimised instance of tqdm(range(i))

for i in trange(10): time.sleep(0.1) print('done')

Manual approach 1: Use a with statement

# we can provide the optional 'total' parameter with tqdm(total=100) as pbar: for i in range(10): time.sleep(0.1) pbar.update(10) print('done')

Manual approach 2: Assign to a variable

Dont forget to call close() at the end!!!

pbar = tqdm(total=100) for i in range(10): time.sleep(0.1) pbar.update(10) pbar.close() print('done')