Rich
Here’s everything you need to know about rich — the Python library for beautiful terminal output.
🖼️ What is Rich?
rich
is a Python library for rich text and beautiful formatting in the terminal — including:
- 🌈 Colored text & styles
- 📊 Tables, progress bars, trees
- 🪵 Log rendering
- 📦 JSON + syntax highlighting
- 🖼️ Markdown and emoji rendering
It’s used in projects like Textual, FastAPI, Typer, pipx, and Pydantic.
🚀 Installation
pip install rich
Or with uv
:
uv pip install rich
🧱 Basic Usage
from rich import print
print("[bold magenta]Hello[/bold magenta] [green]World![/green] :rocket:")
✅ Output with colors and emoji.
Rich Console
from rich.console import Console
= Console()
console print("Hello [cyan]World[/cyan]!", style="bold") console.
🧾 Text Formatting
Supports:
- Styles:
bold
,italic
,underline
,strike
- Colors: names, hex (
#ff00ff
), RGB tuples - Emoji shortcodes (like
:rocket:
)
print("This is [bold red]error[/bold red] and [green]success[/green]!") console.
📐 Layouts
✅ Tables
from rich.table import Table
= Table(title="User Info")
table "Name", style="cyan")
table.add_column("Email", style="magenta")
table.add_column("Ben", "ben@example.com")
table.add_row("Kiera", "kiera@example.com")
table.add_row(
print(table) console.
🌳 Trees
from rich.tree import Tree
= Tree("📁 Root")
tree "📄 File1.txt")
tree.add(= tree.add("📂 Folder")
branch "📄 NestedFile.txt")
branch.add(
print(tree) console.
🔄 Progress Bars
from rich.progress import track
import time
for step in track(range(10), description="Processing..."):
0.1) time.sleep(
⏳ Manual Progress Bars
from rich.progress import Progress
with Progress() as progress:
= progress.add_task("Downloading...", total=100)
task while not progress.finished:
=10)
progress.update(task, advance0.2) time.sleep(
🐞 Logging with Rich
import logging
from rich.logging import RichHandler
logging.basicConfig(="DEBUG",
level=[RichHandler()],
handlersformat="%(message)s",
)
= logging.getLogger("rich")
logger "Info log")
logger.info("Error log") logger.error(
🧬 Syntax Highlighting
from rich.syntax import Syntax
= '''
code def greet(name):
print(f"Hello, {name}")
'''
= Syntax(code, "python", theme="monokai", line_numbers=True)
syntax print(syntax) console.
📚 Pretty Print JSON
from rich.pretty import pprint
from rich.json import JSON
"foo": "bar", "baz": [1, 2, 3]})
pprint({
print(JSON('{"key": "value", "list": [1, 2, 3]}')) console.
✨ Markdown Rendering
from rich.markdown import Markdown
= Markdown("# Title\n\n**Bold** and *italic* text.")
md print(md) console.
💥 Error Tracebacks
from rich.traceback import install
install()
# Raise any exception after this and it'll show a beautiful traceback
raise ValueError("Oops!")
🧪 Testing & Integration
Django Logging
Use RichHandler
in settings.py
:
= {
LOGGING "version": 1,
"handlers": {
"console": {
"class": "rich.logging.RichHandler",
"level": "DEBUG",
},
},"root": {
"handlers": ["console"],
"level": "INFO",
}, }
🧵 Combining Components
from rich.panel import Panel
from rich.align import Align
= Panel.fit(
panel "[bold green]Task Complete![/bold green]"),
Align.center(="green",
border_style="Status",
title
)
print(panel) console.
⚙️ Advanced: Live Dashboards
from rich.live import Live
from rich.table import Table
import time
= Table()
table "Task")
table.add_column("Progress")
table.add_column(
with Live(table, refresh_per_second=4):
for i in range(5):
f"Task {i}", f"{i*20}%")
table.add_row(0.5) time.sleep(
📦 Rich CLI
rich [FILE] # Pretty-print file content
rich --traceback # See a rich traceback on crash
🧮 Rich Ecosystem
textual
: TUI apps (terminal GUIs) with Richrich-click
: Combineclick
and Richtyper
: Uses Rich under the hoodrich-prompt-toolkit
: Fancy prompts and inputrich-argparse
: Pretty CLI help output
📊 Summary Cheat Sheet
Feature | Module |
---|---|
Styled text | console.print() |
Tables | rich.table.Table |
Progress Bars | rich.progress.Progress |
Logging | rich.logging.RichHandler |
Syntax Highlighting | rich.syntax.Syntax |
JSON/Pretty Print | rich.json , rich.pretty |
Markdown | rich.markdown.Markdown |
Error Tracebacks | rich.traceback.install() |
Let me know if you’d like:
- A custom CLI logger setup
- A Rich-based dashboard
- A Textual app starter I can scaffold one for you.