Rich
A library for enriching Python terminal output. Provides features like colorization, tables, progress bars, and syntax highlighting, serving as a foundation for TUI development.
GitHub Overview
Textualize/rich
Rich is a Python library for rich text and beautiful formatting in the terminal.
Topics
Star History
Library
Rich
Overview
Rich is a library for enriching Python terminal output. It provides features like colorization, tables, progress bars, syntax highlighting, and markdown rendering to help build beautiful and functional terminal applications.
Details
Rich was developed by Will McGugan in 2020, revolutionizing Python terminal output. It's used for a wide range of purposes, from simple print alternatives to foundations for full-fledged TUI applications. It also serves as the base library for the Textual framework.
Key Features
- Rich Text Styling: Various styles including colors, bold, italic, underline
- Table Display: Tables with customizable borders and styles
- Progress Bars: Live-updating progress indicators
- Syntax Highlighting: Multi-language code highlighting
- Markdown Rendering: Markdown display in terminals
- Panels and Borders: Bordered panels enclosing content
- Tree Display: Visualization of hierarchical data
- Pretty Printing: Beautiful display of Python objects
- Logging: Rich-formatted log output
- Enhanced Tracebacks: Improved error display readability
Architecture
Rich's core is the Console
class, which provides:
- Automatic Terminal Detection: Color support and size detection
- Rendering Protocol: Extensible output via
__rich__()
method - Segment Generation: Lowest-level display units
- Style System: Flexible style application mechanism
Pros and Cons
Pros
- Simple and intuitive API
- Rich output options
- High customizability
- Excellent performance
- Automatic terminal compatibility detection
- Jupyter Notebook support
- Comprehensive documentation
- Active community
Cons
- Not a complete TUI framework by itself
- Limited for complex interactive UIs
- Feature limitations on some older terminals
- Potential performance impact with large outputs
Key Links
Code Example
from rich.console import Console
from rich.table import Table
from rich.progress import Progress, TaskID
from rich.syntax import Syntax
from rich.panel import Panel
from rich.tree import Tree
import time
# Create Console object
console = Console()
# Basic styled text
console.print("Hello", style="bold red")
console.print("World", style="italic blue")
# Create table
table = Table(title="TUI Frameworks")
table.add_column("Language", style="cyan")
table.add_column("Framework", style="magenta")
table.add_column("Stars", justify="right", style="green")
table.add_row("Python", "Textual", "20,000+")
table.add_row("Go", "Bubble Tea", "15,000+")
table.add_row("Rust", "Ratatui", "8,000+")
console.print(table)
# Progress bar
with Progress() as progress:
task = progress.add_task("[green]Processing...", total=100)
for i in range(100):
time.sleep(0.01)
progress.update(task, advance=1)
# Syntax highlighting
code = '''
def hello_world():
print("Hello, Rich!")
return True
'''
syntax = Syntax(code, "python", theme="monokai", line_numbers=True)
console.print(syntax)
# Panel
console.print(Panel("This is a [bold blue]panel[/bold blue]!", title="Info"))
# Tree structure
tree = Tree("🌳 Project Structure")
tree.add("📁 src/")
python_branch = tree.add("📁 python/")
python_branch.add("📄 textual.py")
python_branch.add("📄 rich.py")
tree.add("📄 README.md")
console.print(tree)
# Markdown rendering
from rich.markdown import Markdown
markdown_text = '''
# Rich Library
This is **bold** and this is *italic*.
- Feature 1
- Feature 2
- Feature 3
```python
print("Code block with syntax highlighting")
'''
md = Markdown(markdown_text) console.print(md)