Daniel Roy Greenfeld

Daniel Roy Greenfeld

About | Articles | Books | Jobs | News | Tags

TIL: Rich.console.status for slow processes

For building CLI, there's so much that rich provides that I can't imagine not using it. Here's adding a moving bar that updates:

from time import sleep
from rich.console import Console

console = Console()

with console.status(
    "[bold red]Starting...[/bold red]", spinner="bouncingBar"
) as status:
    console.log("Process started")
    # If you want to be lazy, not closing the tags doesn't seem to have side effects
    status.update("[bold yellow]still going...")
    console.log("Process still going")
    status.update("[bold green]almost there...")
    console.log("Process getting close", style="bold")
    console.log("Finish!", style="bold green")

Rich comes with a lot of spinners to show the system processing. This script displays most of them in action:

from rich import console, spinner

from time import sleep

console = console.Console()

with console.status("Here we go...") as status:
    for key in spinner.SPINNERS.keys():
        # Skipping the 15 dot variations
        if key.startswith("dots"):
        status.update(key, spinner=key)

Tags: python TIL
← Back to home