rich is a Python library for formatted terminal output. Colors, tables, panels, markdown, progress bars, spinners — all through one simple API. Created by Will McGugan and actively maintained.
Installation
uv add rich
Console — the Foundation of Everything
from rich.console import Console
console = Console()
console.print("Plain text")
console.print("[bold red]Error![/bold red]")
console.print("[green]Success[/green]")
console.print("[bold blue]Heading[/bold blue]")
console.print("[dim]Secondary text[/dim]")
Styles are wrapped in [style]text[/style].
Markdown
from rich.markdown import Markdown
text = "# Heading\n\nParagraph with **bold** text.\n\n```python\nprint('hello')\n```"
console.print(Markdown(text))
Perfect for rendering Claude responses — the model frequently replies in markdown format.
Panel
from rich.panel import Panel
console.print(Panel(
"Message text",
title="Title",
border_style="blue"
))
console.print(Panel(
"[bold]Important message[/bold]",
title="[red]Error[/red]",
border_style="red",
padding=(1, 2),
))
Status (Spinner)
import time
with console.status("[bold green]Thinking...[/]", spinner="dots"):
time.sleep(2) # long operation
# spinner stops automatically when exiting the with block
Built-in spinners: dots, dots2, line, arc, bouncingBall, clock.
Input with a Prompt
user_input = console.input("[bold blue]You:[/] ")
Supports the same color tags as console.print().
Table
from rich.table import Table
table = Table(title="Token Stats")
table.add_column("Type", style="cyan", no_wrap=True)
table.add_column("Count", justify="right", style="green")
table.add_column("Cost", justify="right", style="yellow")
table.add_row("Input", "1,234", "$0.0037")
table.add_row("Output", "567", "$0.0085")
console.print(table)
Style Cheat Sheet
| Style | Use Case |
|---|---|
[bold] |
important parts |
[italic] |
terms, emphasis |
[dim] |
secondary information |
[red] / [green] |
errors / success |
[blue] / [cyan] |
user labels / code |
[yellow] |
warnings, costs |
[bold green]Claude[/bold green] |
combined styles |
Important: highlight=False When Streaming
When printing text chunk by chunk, always disable auto-highlighting:
# When streaming:
console.print(chunk, end="", highlight=False)
# For regular output, the flag is not needed:
console.print(response_text)
Without highlight=False, rich tries to apply syntax highlighting to each chunk individually — this breaks formatting during streaming output.
Rule (Divider)
from rich.rule import Rule
console.print(Rule("[dim]new conversation[/dim]"))
Prints a horizontal line spanning the full terminal width — handy for separating output blocks.
💬 Comments (0)
No comments yet
Be the first to share your opinion about this article!