This page provides an overview of Rich's integrations with Python's standard library. It covers five subsystems: structured logging output, enhanced tracebacks, pretty-printed object display, runtime object inspection, and interactive prompts. Each subsystem is documented in detail in its own child page; this page explains the shared patterns, entry points, and how the subsystems relate to one another.
This page does not cover the core rendering pipeline, markup syntax, or built-in renderables. For those topics, see the Core Rendering System and Renderables pages.
Rich hooks into several Python standard library mechanisms:
| Standard Library Hook | Rich Module | Primary Class / Function |
|---|---|---|
logging.Handler | rich/logging.py | RichHandler |
sys.excepthook | rich/traceback.py | install(), Traceback |
sys.displayhook | rich/pretty.py | install(), Pretty |
Object __repr__ protocol | rich/repr.py | auto, rich_repr |
| Interactive input | rich/prompt.py | Prompt, Confirm |
In each case, Rich either subclasses or replaces a standard library hook, then routes output through a Console instance so that all of Rich's styling, markup, and export features remain available.
Sources: rich/logging.py1-16 rich/traceback.py84-219 rich/pretty.py171-251 rich/repr.py1-15
Module relationship diagram
Sources: rich/logging.py92-115 rich/traceback.py131-165 rich/pretty.py195-226 rich/_log_render.py14-86
RichHandler in rich/logging.py18-239 is a subclass of logging.Handler. It replaces the plain-text log output with a structured table rendered through a Console.
The handler delegates layout to LogRender (rich/_log_render.py14-86), which builds a Table.grid() with up to four columns:
| Column | Controlled By | Default |
|---|---|---|
| Time | show_time | True |
| Level | show_level | True |
| Message | always present | — |
| File path / line | show_path | True |
When rich_tracebacks=True is set, exception records are rendered by Traceback.from_exception() instead of the standard formatter, integrating Pygments-highlighted code frames directly into the log output.
Per-message overrides are supported via the extra dict on log calls (e.g. extra={"markup": True} or extra={"highlighter": None}).
For full details, see Logging Integration.
Sources: rich/logging.py18-115 rich/_log_render.py14-86 docs/source/logging.rst1-71
rich/traceback.py provides two entry points:
install() (rich/traceback.py84-218) — replaces sys.excepthook (and IPython's _showtraceback) with a callable that renders Traceback for every uncaught exception.Traceback (rich/traceback.py262-350) — a Console renderable that can be constructed directly from exception info via Traceback.from_exception() or Traceback.extract().Data model for extracted exception info
Traceback integrates with pretty.traverse() to render local variable values as structured pretty-printed trees when show_locals=True. The suppress parameter accepts a list of modules or directory paths; frames from those paths are shown with file/line info only, without source code.
For full details, see Traceback Enhancement.
Sources: rich/traceback.py221-256 rich/traceback.py295-350 docs/source/traceback.rst1-98
rich/pretty.py provides pretty-printing of arbitrary Python objects. The key public API is:
| Symbol | Kind | Purpose |
|---|---|---|
Pretty | class | Renderable wrapper around any object |
pretty_repr() | function | Returns a formatted str repr |
pprint() | function | Prints directly to a Console |
install() | function | Replaces sys.displayhook for REPL use |
traverse() | function | Builds an internal Node tree from an object |
The Pretty class implements __rich_console__ by calling pretty_repr() and passing the result through Text.from_ansi() and ReprHighlighter. Width-aware line breaking is controlled by the Node / _Line tree representation built inside traverse().
install() (rich/pretty.py171-250) checks for an active IPython session; in a standard REPL it replaces sys.displayhook, while inside IPython it installs a custom BaseFormatter subclass (RichFormatter) that hooks into IPython's display formatter pipeline.
Object traversal and rendering pipeline
The __rich_repr__ protocol, defined in rich/repr.py, lets any class provide structured field data that traverse() uses instead of the default repr() call. The auto decorator (rich/repr.py37-101) automatically generates both __rich_repr__ (from __init__ signature) and __repr__ (from __rich_repr__).
For full details, see Pretty Printing and REPL.
Sources: rich/pretty.py171-355 rich/repr.py37-101 rich/pretty.py580-915
rich/inspect.py exposes the Inspect renderable and the inspect() convenience function (callable as from rich import inspect). It displays an object's attributes, methods, and docstrings in a formatted table, with per-attribute type annotations and values rendered through the Pretty system.
Parameters control which members are shown:
| Parameter | Controls |
|---|---|
methods | Instance methods |
docs | Docstrings |
private | _-prefixed members |
dunder | __-prefixed members |
all | All of the above |
value | The object's own repr at the top |
sort | Alphabetical ordering |
For full details, see Inspecting Objects.
rich/prompt.py provides PromptBase and concrete subclasses Prompt, IntPrompt, FloatPrompt, and Confirm. These render styled prompts through a Console and handle validation via process_response(), retrying on invalid input and raising InvalidResponse internally.
The ask() class method is a one-shot shortcut:
Prompt.ask("Enter your name")
Confirm.ask("Continue?")
password=True masks input, and choices restricts acceptable values with automatic display of the valid set.
For full details, see Prompts.
All five subsystems follow the same conventions:
Console injection — every integration accepts an optional Console argument. If none is provided, get_console() from rich/__init__.py returns the global singleton.
install() pattern — both rich.traceback and rich.pretty expose an install() function that replaces a sys-level hook and returns the previous hook so it can be restored.
Traceback reuse — RichHandler in rich/logging.py delegates exception rendering to Traceback.from_exception() rather than re-implementing it, so tracebacks in log output and in uncaught-exception output look identical.
Pretty / traverse() reuse — Traceback.extract() calls pretty.traverse() to capture local variable state into Node trees at extract time (rich/traceback.py579-594), so locals are serialised when the exception occurs rather than at render time.
Sources: rich/logging.py132-180 rich/traceback.py576-597 rich/pretty.py171-200 rich/traceback.py84-103
Refresh this wiki
This wiki was recently refreshed. Please wait 2 days to refresh again.