Here are two approaches you can take—one “manual” (no external deps) and one using the very-nice rich library.
1) Manual ANSI-escape approach
This uses only built-in Python and ANSI cursor codes to move the cursor up and overwrite two lines:
import time
import sys
def print_progress(progress, total, action, bar_length=30):
# 1) build the bar
filled = int(bar_length * progress // total)
bar = "=" * filled + "-" * (bar_length - filled)
percent = progress / total * 100
# 2) print both lines, then move cursor up 2 lines
sys.stdout.write(f"\r[{bar}] {percent:6.2f}%\n") # line 1
sys.stdout.write(f"Current action: {action.ljust(30)}") # line 2
sys.stdout.flush()
# move cursor back up so next call will overwrite these two lines
sys.stdout.write("\033[2A") # CSI n A moves cursor up n lines
if __name__ == "__main__":
total = 100
for i in range(total + 1):
action = f"Processing item {i}"
print_progress(i, total, action)
time.sleep(0.05)
# when done, move cursor down so prompt isn’t on top of the bar
sys.stdout.write("\033[2B\n")
What’s going on:
- We build a classic
====---- bar.
- We print two lines (bar + percent, then current action).
- We immediately move the cursor back up two lines (
\033[2A) so the next invocation re-writes those same two lines.
- At the end, we “leave” the bar behind by moving the cursor down.
2) Using the Rich library
Rich makes all this very easy, and has nice colors, smoothing, built-in live updating, etc.
First install:
pip install rich
Then:
import time
from rich.live import Live
from rich.panel import Panel
from rich.progress import Progress, BarColumn, TextColumn
actions = [f"Step {i}" for i in range(100)]
with Live(refresh_per_second=10) as live:
progress = Progress(
TextColumn("[bold blue]Overall:"),
BarColumn(bar_width=None),
TextColumn("[green]{task.percentage:>5.1f}%"),
transient=True, # remove when done
)
task = progress.add_task("processing", total=len(actions))
for action in actions:
# update the bar
progress.update(task, advance=1)
# build a panel with current action
panel = Panel(f"[yellow]{action}", title="Current Action")
# render bar + panel, top → bottom
live.update(progress)
live.console.print(panel)
time.sleep(0.05)
# clear the panel so next loop rewrites it
live.console.clear()
# finally, render completed bar without clearing
live.update(progress)
live.console.print(Panel("[green]Done!", title="Current Action"))
What this does:
- Creates a single Progress bar (with percent).
- In the loop, it:
a. Advances the bar,
b. Renders a small Panel below it showing your “current action,”
c. Sleeps (simulating work),
d. Clears before the next iteration.
- When finished, it prints “Done!” in that second line.
Rich will handle cursor movement, redrawing, colors, etc., so you don’t need to manually deal with \r, \033[…A, etc.
–––
Pick the one that fits you best! The manual version is zero-deps, but a bit finicky. The rich version requires installing a library, but gives you much more power and polish out of the box. Let me know if you need any tweaks! 🚀