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.
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.
Frequently Asked Questions
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()])`.
Citations (3)
- Rich GitHub— Rich library for terminal formatting
- Rich Documentation— Rich Console API and markup syntax
- Python Logging Documentation— Python logging handlers and configuration
Related on TokRepo
Discussion
Related Assets
Conda — Cross-Platform Package and Environment Manager
Install, update, and manage packages and isolated environments for Python, R, C/C++, and hundreds of other languages from a single tool.
Sphinx — Python Documentation Generator
Generate professional documentation from reStructuredText and Markdown with cross-references, API autodoc, and multiple output formats.
Neutralinojs — Lightweight Cross-Platform Desktop Apps
Build desktop applications with HTML, CSS, and JavaScript using a tiny native runtime instead of bundling Chromium.