Fundamentals 7 min read

Implementing Dynamic Progress Bars in Python: Multiple Approaches and Code Examples

This article explains how to create dynamic progress bars in Python using plain print statements, the tqdm library, custom styling, multiple and nested bars, update‑frequency control, dynamic descriptions, custom callbacks, as well as the click and rich libraries, providing complete code snippets for each method.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Implementing Dynamic Progress Bars in Python: Multiple Approaches and Code Examples

When writing Python scripts, especially for long‑running tasks or loops, showing a progress bar improves user experience and gives a clear view of the program's execution state.

1. Using the print function

import time

def progress_bar(n, total, bar_length=20):
    percent = float(n) / total
    arrow = '-' * int(round(percent * bar_length) - 1) + '>'
    spaces = ' ' * (bar_length - len(arrow))
    print(f'Progress: [{arrow}{spaces}] {int(round(percent * 100))}%', end='\r')

total = 50
for i in range(total):
    time.sleep(0.1)  # simulate work
    progress_bar(i + 1, total)
print()  # newline

Output example:

Progress: [--------------------->] 100%

2. Using the tqdm library

from tqdm import tqdm
import time

total = 50
for i in tqdm(range(total), desc="Processing"):
    time.sleep(0.1)  # simulate work

Output example:

Processing: 100%|██████████| 50/50 [00:05<00:00, 9.82it/s]

3. Custom style with tqdm

from tqdm import tqdm
import time

total = 50
for i in tqdm(range(total), desc="Processing", bar_format="{desc}: {n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_fmt}{postfix}]"):
    time.sleep(0.1)  # simulate work

Output example:

Processing: 50/50 [00:05<00:00, 9.82it/s]

4. Multiple progress bars

from tqdm import tqdm
import time

with tqdm(total=100, desc="First") as pbar1, tqdm(total=100, desc="Second") as pbar2:
    for i in range(100):
        time.sleep(0.05)
        pbar1.update(1)
        pbar2.update(1)

Output example:

First: 100%|██████████| 100/100 [00:05<00:00, 19.31it/s]
Second: 100%|██████████| 100/100 [00:05<00:00, 19.31it/s]

5. Nested progress bars

from tqdm import tqdm
import time

outer = tqdm(total=100, desc="Outer Loop")
for i in outer:
    inner = tqdm(total=100, desc="Inner Loop", leave=False)
    for j in inner:
        time.sleep(0.01)
        inner.update(1)
    outer.update(1)
    inner.close()
outer.close()

Output example:

Outer Loop: 100%|██████████| 100/100 [00:10<00:00, 9.78it/s]

6. Controlling update frequency

from tqdm import tqdm
import time

total = 50
for i in tqdm(range(total), desc="Processing", mininterval=0.5):
    time.sleep(0.1)  # simulate work

Output example:

Processing: 100%|██████████| 50/50 [00:05<00:00, 9.82it/s]

7. Dynamic description

from tqdm import tqdm
import time

total = 50
with tqdm(total=total, desc="Starting") as pbar:
    for i in range(total):
        time.sleep(0.1)
        pbar.set_description(f"Processing {i+1}")
        pbar.update(1)

Output example:

Processing 50: 100%|██████████| 50/50 [00:05<00:00, 9.82it/s]

8. Custom callback function

from tqdm import tqdm
import time

def update_progress(progress):
    print(f"Progress: {progress}% completed.", end="\r")

total = 50
for i in range(total):
    time.sleep(0.1)
    update_progress(int((i + 1) / total * 100))
print()  # newline

Output example:

Progress: 100% completed.

9. Using the click library

import click
import time

total = 50
with click.progressbar(range(total), label='Processing') as bar:
    for i in bar:
        time.sleep(0.1)  # simulate work

Output example:

Processing 50/50 [100%]

10. Using the rich library

from rich.progress import track
import time

total = 50
for i in track(range(total), description="Processing..."):
    time.sleep(0.1)  # simulate work

Output example:

Processing... 100% 50/50 [00:05<00:00, 9.82it/s]

By reviewing these examples you can choose the most suitable method for displaying dynamic progress bars in your Python projects, depending on your specific requirements and environment.

pythonscriptingconsoleRichtqdmclickprogress-bar
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.