CODE/2022-10-14

Six Ways to Display Progress Bars in Python

Long-running scripts are easier to use when they show how much work is complete. A progress bar can be as simple as a line in the terminal or as polished as an animated display or desktop window.
This article covers six approaches in Python: two built with the standard library, three terminal libraries, and one graphical option. The examples use time.sleep() to simulate work; replace it with your own processing. Run terminal examples in an interactive terminal for the best display.

1. Build a simple terminal progress bar

For a small script, you can calculate the percentage yourself and redraw the same line using a carriage return (\r). flush=True makes each update visible immediately.
python_snippet
Python · 13 lines|
import time
total = 100
width = 50
for completed in range(1, total + 1):
time.sleep(0.02) # Replace with real work.
filled = width * completed // total
bar = "#" * filled + "-" * (width - filled)
percent = 100 * completed / total
print(f"\rProgress: [{bar}] {percent:5.1f}%", end="", flush=True)
print()
This approach has no external dependencies, but you must manage formatting and updates yourself. It assumes the total is known and greater than zero.

2. Add elapsed time

For longer tasks, show how much time has passed. time.perf_counter() is suitable for measuring elapsed durations.
python_snippet
Python · 18 lines|
import time
total = 50
width = 30
start = time.perf_counter()
for completed in range(1, total + 1):
time.sleep(0.05) # Replace with real work.
filled = width * completed // total
bar = "#" * filled + "-" * (width - filled)
elapsed = time.perf_counter() - start
print(
f"\r[{bar}] {completed / total:6.1%} | {elapsed:.1f}s elapsed",
end="",
flush=True,
)
print()
Elapsed time is not the same as an estimated time remaining. An estimate depends on the work progressing at a roughly predictable rate.

3. Wrap an iterable with tqdm

tqdm adds a progress meter to an iterable with minimal code. It can display the count, elapsed time, estimated remaining time, and processing rate.
Install it:
bash_snippet
Bash · 1 line|
python -m pip install tqdm
Then wrap the iterable:
python_snippet
Python · 5 lines|
import time
from tqdm import tqdm
for item in tqdm(range(100), desc="Processing"):
time.sleep(0.02) # Replace with real work.
For notebook environments, tqdm.auto selects a suitable display when supported. It also supports manual updates and customization.

4. Advance a bar with progress

The progress package offers several terminal bar styles. Create a bar with a known maximum and advance it after each completed item.
bash_snippet
Bash · 1 line|
python -m pip install progress
python_snippet
Python · 8 lines|
import time
from progress.bar import IncrementalBar
items = range(100)
with IncrementalBar("Processing", max=len(items)) as bar:
for item in items:
time.sleep(0.02) # Replace with real work.
bar.next()
The context manager finishes the bar when the block exits. In the original example, finish() appeared inside the loop; it should instead run after all iterations, or be handled by the context manager as above.

5. Animate progress with alive-progress

alive-progress provides animated terminal bars, a spinner, throughput, and an estimated time remaining.
bash_snippet
Bash · 1 line|
python -m pip install alive-progress
python_snippet
Python · 8 lines|
import time
from alive_progress import alive_bar
items = range(100)
with alive_bar(len(items), title="Processing") as bar:
for item in items:
time.sleep(0.02) # Replace with real work.
bar()
Call bar() once for each completed item. The context manager handles the final display when processing ends.

6. Show a graphical progress meter

If your script runs on a desktop and a terminal bar is not enough, PySimpleGUI provides a one-line progress meter. It requires a working graphical environment and tkinter.
bash_snippet
Bash · 1 line|
python -m pip install PySimpleGUI
python_snippet
Python · 11 lines|
import time
import PySimpleGUI as sg
items = range(100)
for completed, item in enumerate(items, start=1):
time.sleep(0.02) # Replace with real work.
if not sg.one_line_progress_meter(
"Processing", completed, len(items), key="-PROGRESS-"
):
print("Cancelled")
break
The example checks for cancellation after each item; a long individual operation cannot be interrupted by this loop until it returns. PySimpleGUI 6 is available as open source, but its project and documentation have been transitioning; check its current installation and licensing information before use.

Choosing an approach

  • No dependencies: use a simple terminal bar, optionally with elapsed time.
  • A quick general-purpose bar: start with tqdm.
  • A particular terminal style: try progress or alive-progress.
  • A desktop interface: use a GUI meter when a graphical environment is available.
For unattended jobs, consider sending a notification after completion rather than watching a bar.
COMPANION ESSAYS / 思想脉络与互照篇目
同卷深读2026-05-22 ·

Installing Python 3 Alongside System Python on CentOS 7

同属「系统工程卷」脉络衍生思考与工程推演推演阅读 →
跨界互照2026-08-22 ·

我的书架:03|金融经管:经济运行、金融体系与企业治理

我的书架:03|金融经管:经济运行、金融体系与企业治理

跨领域对照思辨 · 从「思想随笔」视界互为印证推演阅读 →
RELATED INDEX / 相关阅读索引
柯航hang.ke·© 2014—2026
CC BY-NC 4.0·