Skip to content

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

In this Python Tutorial I show you how you can add a Progress Bar to your Python code in just one line of code!


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')


FREE VS Code / PyCharm Extensions I Use

✅ Write cleaner code with Sourcery, instant refactoring suggestions: Link*


PySaaS: The Pure Python SaaS Starter Kit

🚀 Build a software business faster with pure Python: Link*

* These are affiliate link. By clicking on it you will not have any additional costs. Instead, you will support my project. Thank you! 🙏