Last modified: Aug 12, 2025 By Alexander Williams

Python Typer Rich Integration Guide

Python Typer and Rich make a powerful combo for CLI apps. Typer simplifies CLI creation. Rich adds beautiful formatting. Together they create professional tools.

Why Use Rich with Typer?

Rich enhances Typer apps with colors, tables, and progress bars. It makes output more readable and engaging. Users get better visual feedback.

For large Typer apps, consider lazy imports to optimize startup time when adding Rich.

Installing Required Packages

First install both packages using pip:


pip install typer rich

Basic Rich Integration

Here's how to use Rich's print in a Typer app:


import typer
from rich.console import Console

app = typer.Typer()
console = Console()

@app.command()
def greet(name: str):
    console.print(f"[bold green]Hello[/bold green] [cyan]{name}[/cyan]!")

if __name__ == "__main__":
    app()

This produces colorful output with Rich's markup syntax.

Adding Progress Bars

Rich's progress bars work well with long-running Typer commands:


from rich.progress import track
import time

@app.command()
def process_items():
    for i in track(range(100), description="Processing..."):
        time.sleep(0.05)  # Simulate work
    console.print("[bold]Done![/bold]")

The progress bar shows completion percentage and estimated time.

Styled Tables in Typer

Display data beautifully with Rich tables:


from rich.table import Table

@app.command()
def show_users():
    table = Table(title="Users")
    table.add_column("ID", style="cyan")
    table.add_column("Name", style="magenta")
    
    table.add_row("1", "Alice")
    table.add_row("2", "Bob")
    
    console.print(table)

Tables automatically adjust to terminal width.

Error Handling with Style

Combine Typer's error handling with Rich's formatting:


@app.command()
def divide(x: int, y: int):
    try:
        result = x / y
        console.print(f"[green]Result: {result}[/green]")
    except ZeroDivisionError:
        console.print("[red]Error: Cannot divide by zero![/red]")
        raise typer.Exit(code=1)

Advanced Formatting Options

Rich offers many styling features:

  • Syntax highlighting for code
  • Markdown rendering
  • Custom themes and styles

For complex Typer apps, see our packaging guide.

Conclusion

Rich takes Typer apps to the next level. It adds visual appeal and better UX. Start with simple colored text then add progress bars and tables.

The combination works well for both simple and complex CLI tools. Your users will appreciate the polished interface.