Rich — Beautiful Formatting in the Python Terminal
Rich renders beautiful text, tables, progress bars, markdown, syntax-highlighted code, and tracebacks in the terminal. It turns any Python CLI into a polished UX with a few lines of code and is the backbone of Textual and pip.
Installation agent prête
Cet actif peut être installé après choix du runtime, vérification du plan et exécution de la commande adaptée.
npx -y tokrepo@latest install cd60c899-37b4-11f1-9bc6-00163e2b0d79 --target codexÀ exécuter après confirmation du plan en dry-run.
What it is
Rich is a Python library for rendering beautiful text, tables, progress bars, markdown, syntax-highlighted code, and tracebacks in the terminal. It turns any Python CLI into a polished user experience with just a few lines of code. Rich is the rendering engine behind Textual (the TUI framework) and is used by pip for its progress display.
Rich is for Python developers building command-line tools, data pipelines, or scripts who want better terminal output without writing ANSI escape codes manually.
How it saves time or tokens
Without Rich, formatting terminal output requires manual ANSI escape sequences or third-party libraries like colorama for basic coloring. Rich provides a high-level API that handles color, layout, tables, trees, and progress bars through a single Console object.
For debugging, Rich's inspect() function and enhanced tracebacks show object attributes, source code context, and local variables in a readable format. This reduces the time spent deciphering stack traces and print-debugging.
How to use
- Install Rich:
pip install rich
- Use the Console for formatted output:
from rich.console import Console
from rich.table import Table
console = Console()
console.print('Hello, [bold magenta]World[/bold magenta]!')
table = Table(title='Dependencies')
table.add_column('Package', style='cyan')
table.add_column('Version', style='green')
table.add_row('rich', '13.7.0')
table.add_row('textual', '0.50.0')
console.print(table)
- Run the built-in demo to see all features:
python -m rich
Example
Progress bar for a file download:
import time
from rich.progress import Progress
with Progress() as progress:
task = progress.add_task('[cyan]Downloading...', total=100)
while not progress.finished:
progress.update(task, advance=1)
time.sleep(0.05)
Syntax-highlighted code output:
from rich.syntax import Syntax
from rich.console import Console
console = Console()
code = '''def fibonacci(n):\n a, b = 0, 1\n for _ in range(n):\n yield a\n a, b = b, a + b'''
syntax = Syntax(code, 'python', theme='monokai', line_numbers=True)
console.print(syntax)
Related on TokRepo
- Coding AI tools -- developer productivity and CLI tools
- Automation tools -- script automation with better output
Common pitfalls
- Rich output looks best in terminals that support 256 colors or truecolor. Pipe output to a file or a basic terminal and you get raw ANSI codes. Use
Console(force_terminal=True)orConsole(no_color=True)to control this. - Rich's
print()replaces Python's built-in print with markup parsing. If your text contains square brackets that are not markup, escape them with\[or useconsole.print(text, highlight=False). - Rich and logging: use
RichHandlerto integrate with Python's logging module. Do not mixconsole.printandlogging.infowithout a handler, or output will interleave unpredictably.
Questions fréquentes
Rich supports Python 3.7 and above. It has no compiled extensions and installs as a pure Python package, making it compatible with any platform where Python runs.
Yes. Rich includes a Markdown class that renders headings, bold, italic, code blocks, lists, and links directly in the terminal. Use `console.print(Markdown(text))` to display markdown content.
Colorama provides basic cross-platform ANSI color support. Rich is a full rendering library that handles colors, tables, trees, progress bars, syntax highlighting, and layout. Rich uses colorama internally on Windows for ANSI compatibility.
Yes. Rich detects Jupyter environments and renders output as HTML in notebook cells. Tables, syntax highlighting, and other Rich objects display with full formatting in Jupyter.
Yes. Rich provides RichHandler, a drop-in handler for Python's logging module. It adds colorized, formatted log output with tracebacks. Set it up with `logging.basicConfig(handlers=[RichHandler()])`.
Sources citées (3)
- Rich GitHub— Rich library for terminal formatting
- Rich Documentation— Rich Console API and markup syntax
- Python Logging Documentation— Python logging handlers and configuration
En lien sur TokRepo
Fil de discussion
Actifs similaires
Spectre.Console — Beautiful Console Output for .NET Applications
A .NET library for building rich, interactive terminal applications with tables, progress bars, trees, and styled text.
kitty — The Fast, Feature-Rich Terminal with GPU Rendering and Image Protocol
kitty is a feature-rich GPU-accelerated terminal with tabs, splits, SSH integration, and its own terminal graphics protocol used by tools like icat and MPV. It combines the speed of Alacritty with the features of iTerm2.
Ratatui — Terminal User Interface Library for Rust
Ratatui is a Rust library for building rich terminal user interfaces (TUIs). It provides widgets for tables, charts, lists, paragraphs, tabs, and more — enabling you to build beautiful, interactive terminal applications with immediate-mode rendering.
Superfile — Modern Terminal File Manager with Beautiful UI
Superfile is a modern terminal file manager written in Go with a visually appealing interface featuring multi-panel navigation, file preview, and plugin support.