Python Module: Rich
Posted: Aug 04, 2025
Updated: Aug 04, 2025
Posted: Aug 04, 2025
Updated: Aug 04, 2025
Rich is a Python library for rich text and beautiful formatting in the terminal. Use uv add rich
or pip install rich
to install the library.
from rich import print
print("[bold magenta]Hello[/bold magenta] [green]World![/green]")
Use style tags inside print()
or use the Text
class.
from rich import print
from rich.text import Text
# Inline styles
print("[bold red]Error:[/] Something went wrong!")
# Using Text object
text = Text("Important", style="bold underline green")
print(text)
Common styles:
bold
, italic
, underline
, strike
, dim
red
, green
, blue
, yellow
, etc.on red
, on green
from rich.table import Table
from rich.console import Console
console = Console()
table = Table(title="User Info")
table.add_column("Name", style="cyan")
table.add_column("Role", style="magenta")
table.add_row("Alice", "Admin")
table.add_row("Bob", "User")
console.print(table)
from rich.panel import Panel
console.print(Panel("This is inside a panel", title="Note", subtitle="footer", style="bold cyan"))
Rich can enhance Python’s logging
module.
import logging
from rich.logging import RichHandler
logging.basicConfig(level="INFO", handlers=[RichHandler()])
log = logging.getLogger("rich")
log.info("This is an info message")
from rich.progress import Progress
import time
progress = Progress()
with progress:
task = progress.add_task("Processing...", total=100)
while not progress.finished:
progress.update(task, advance=1)
time.sleep(0.05)
from rich.console import Console
from rich.syntax import Syntax
code = """def hello():\n print("Hello, World!")"""
syntax = Syntax(code, "python", theme="monokai", line_numbers=True)
console = Console()
console.print(syntax)
from rich.live import Live
from rich.table import Table
import time
table = Table()
table.add_column("Time")
with Live(table, refresh_per_second=4):
for _ in range(10):
table.add_row(time.strftime("%H:%M:%S"))
time.sleep(1)
rich.print()
for styled output instead of print()
.Console().print()
for advanced features (e.g. tables, layouts).