Creating beautiful terminal output with Python Rich library
Create rich, formatted terminal output with Python Rich library - including colored text, tables, progress bars, syntax highlighting, and beautiful console applications.
When enhancing terminal output with colors, tables, progress bars, Markdown rendering, or building rich CLI applications without a full TUI framework.
pip install rich
from rich import print as rprint
from rich.console import Console
from rich.theme import Theme
console = Console()
# Simple colored text
rprint("[red]This is red[/red]")
rprint("[bold blue]Bold blue[/bold blue]")
rprint("[yellow on black]Yellow on black[/yellow on black]")
# Combinations
rprint("[bold][italic][underline]All styles[/underline][/italic][/bold]")
from rich.console import Console
console = Console()
# Print with styling
console.print("Hello, World!", style="bold green")
# Different output streams
console_err = Console(stderr=True) # For warnings/errors
console_err.print("[red]Error![/red]", stderr=True)
# Width control
console = Console(width=80)
console = Console(force_terminal=True)
black, red, green, yellow, blue, magenta, cyan, white
bright_black (gray), bright_red, bright_green, bright_yellow
bright_blue, bright_magenta, bright_cyan, bright_white
from rich.color import Color
# RGB
console.print("[color(255,128,0)]Orange text[/color]")
# Hex
console.print("[#FF8800]Orange text[/#FF8800]")
# With Color class
color = Color.from_rgb(255, 128, 0)
console.print(f"[{color}]Orange text[/{color}]")
| Style | Description |
|---|---|
bold | Bold text |
italic | Italic text |
underline | Underlined |
strike | Strikethrough |
blink | Blinking text |
reverse | Reversed colors |
dim | Dimmed text |
bold not dim | Bold and not dim |
console.print("[bold red]Bold Red[/bold red]")
console.print("[bold italic underline]All combined[/bold italic underline]")
from rich.console import Console
from rich.table import Table
console = Console()
table = Table(title="Users")
table.add_column("Name", style="cyan", no_wrap=True)
table.add_column("Email", style="magenta")
table.add_column("Role", style="green")
table.add_row("Alice", "[email protected]", "Admin")
table.add_row("Bob", "[email protected]", "User")
table.add_row("Charlie", "[email protected]", "User")
console.print(table)
from rich.table import Table
table = Table(
title="Sales Report",
show_header=True,
header_style="bold magenta",
border_style="bright_blue",
row_styles=["", "dim"]
)
table.add_column("Product", justify="left")
table.add_column("Q1", justify="right")
table.add_column("Q2", justify="right")
table.add_column("Total", justify="right", style="bold green")
table.add_row("Widget A", "$10,000", "$12,000", "$22,000")
table.add_row("Widget B", "$8,000", "$9,500", "$17,500")
table.add_row("Widget C", "$15,000", "$14,000", "$29,000")
console.print(table)
from rich.progress import Progress
with Progress() as progress:
task = progress.add_task("[cyan]Processing...", total=100)
for i in range(100):
progress.advance(task)
# Do work...
from rich.progress import Progress, SpinnerColumn, TextColumn, BarColumn
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
BarColumn(),
TextColumn("[progress.percentage]{task.percentage:>3.0f}%"),
console=console
) as progress:
task1 = progress.add_task("[red]Download", total=100)
task2 = progress.add_task("[green]Process", total=100)
for i in range(100):
progress.advance(task1)
if i % 2 == 0:
progress.advance(task2)
from rich.progress import Progress, DownloadColumn, TransferSpeedColumn
with Progress(
TextColumn("[bold blue]{task.description}"),
BarColumn(),
DownloadColumn(),
TransferSpeedColumn(),
console=console
) as progress:
task = progress.add_task("Download", total=1000000)
for chunk in download_chunks():
progress.advance(task, len(chunk))
from rich.syntax import Syntax
from rich.console import Console
console = Console()
code = """
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
result = fibonacci(10)
print(f"Result: {result}")
"""
syntax = Syntax(code, "python", theme="monokai", line_numbers=True)
console.print(syntax)
monokai, github-dark, dracula, nord, one-dark, vim, etc.
from rich.panel import Panel
from rich.text import Text
console = Console()
text = Text("Welcome to my application!", style="bold green")
panel = Panel(text, title="Info", border_style="blue")
console.print(panel)
# Without title
console.print(Panel("Simple panel", style="red"))
from rich.box import Box, DOUBLE, ROUNDED
console.print(
"Content in rounded box",
box=ROUNDED,
border_style="cyan"
)
from rich.tree import Tree
from rich.console import Console
console = Console()
tree = Tree("📁 Project", guide_style="bold blue")
src = tree.add("📂 src", style="cyan")
src.add("📄 main.py")
src.add("📂 utils", style="green")
src.add("📄 helpers.py")
tests = tree.add("📂 tests", style="yellow")
tests.add("📄 test_main.py")
console.print(tree)
from rich.console import Console
from rich.markdown import Markdown
console = Console()
md = """
# Heading 1
## Heading 2
This is **bold** and this is *italic*.
- List item 1
- List item 2
```python
print("code block")
"""
console.print(Markdown(md))
## Logging
```python
from rich.logging import RichHandler
import logging
logging.basicConfig(
level=logging.DEBUG,
format="%(message)s",
handlers=[RichHandler(rich_tracebacks=True)]
)
log = logging.getLogger("myapp")
log.debug("Debug message")
log.info("Info message")
log.warning("Warning message")
log.error("Error message")
from rich.live import Live
from rich.table import Table
with Live(build_table(), refresh_per_second=4) as live:
for _ in range(10):
# Update table
live.update(build_table())
from rich.console import Console
from rich.progress import Progress, SpinnerColumn
console = Console()
with console.status("[bold green]Working on something..."):
time.sleep(2)
console.print("[green]Done!")
from rich.pretty import pprint
from rich.console import Console
console = Console()
data = {
"name": "Alice",
"age": 30,
"skills": ["Python", "JavaScript"],
"active": True
}
pprint(data, console=console, expand_all=True)
from rich.console import Console
from rich.bar import Bar
console = Console()
console.print(Bar(50, width=30, color="cyan"))
from rich.console import Console
from rich.columns import Columns
console = Console()
columns = Columns([
"[bold]Name[/bold]\nAlice\nBob\nCharlie",
"[bold]Role[/bold]\nAdmin\nUser\nUser"
], equal=True, expand=True)
console.print(columns)
[/red] or use context managerConsole.options.no_colorconsole.width for responsive layouts