This section covers advanced Rich features that extend beyond basic rendering and require deeper understanding of Rich's architecture. These topics are relevant for:
For detailed information, see the child pages:
Measurement system and ratio-based column width calculationTheme, ThemeStack, DEFAULT_STYLES, and RegexHighlighterrecord=True, capture(), export_text/html/svg, and format templatesAdvanced features landscape — key modules and code entities
Sources: rich/measure.py rich/theme.py rich/themes.py rich/highlighter.py rich/_windows.py rich/_win32_console.py rich/_windows_renderer.py rich/cells.py rich/console.py rich/_export_format.py rich/terminal_theme.py
Rich uses a Measurement NamedTuple (defined in rich/measure.py) to calculate the minimum and maximum width that a renderable requires. This feeds into column width distribution for Table, Columns, and Layout.
Key entities:
| Symbol | Role |
|---|---|
Measurement | NamedTuple with minimum and maximum int fields |
Measurement.get() | Class method that queries a renderable's __rich_measure__ |
measure_renderables() | Aggregates measurements across a list of renderables |
Constrain | Wraps a renderable to enforce a maximum width |
ratio_resolve() | Resolves ratio-based column widths given available space |
ratio_distribute() | Distributes remaining space across ratio-based columns |
See page 7.1 for full documentation.
Sources: rich/measure.py rich/console.py475-487
Rich's style system is theme-based. Each Console instance holds a ThemeStack (in rich/theme.py) that resolves named styles like "bold", "repr.brace", or "logging.level.info" at render time.
Key entities:
| Symbol | Location | Role |
|---|---|---|
Theme | rich/theme.py | Maps style names to Style objects; can be loaded from .ini files |
ThemeStack | rich/theme.py | Stack of Theme objects; top theme takes precedence |
DEFAULT_STYLES | rich/themes.py | Default namespace covering repr.*, json.*, logging.*, progress.*, etc. |
RegexHighlighter | rich/highlighter.py | Base class for creating pattern-based text highlighters |
ReprHighlighter | rich/highlighter.py | Default highlighter for Python repr output |
Themes can be applied temporarily using Console.use_theme() (a context manager backed by ThemeContext in rich/console.py).
See page 7.2 for full documentation.
Sources: rich/theme.py rich/themes.py rich/highlighter.py rich/console.py349-368
Now let me replace the Windows Support section with a more concise version:
Now let me update the rest. Let me write complete replacement sections:
<old_str>
Rich provides specialized support for Windows terminals, which historically have different capabilities from Unix-like terminals.
When running on Windows, Rich automatically detects the terminal's capabilities to determine the optimal rendering approach.
Sources: rich/_windows.py15-61
Rich checks if the Windows terminal supports Virtual Terminal (VT) processing by querying the console mode using the Windows API. If VT processing is enabled, Rich can use ANSI escape sequences for formatting. For newer Windows versions (Windows 10 build 15063 or later with VT support), Rich also enables truecolor support.
The detection logic is implemented in the get_windows_console_features() function, which returns a WindowsConsoleFeatures object containing information about VT and truecolor support.
For older Windows environments without VT support, Rich falls back to using the Windows Console API directly through the LegacyWindowsTerm class.
Sources: rich/_win32_console.py29-572 rich/_windows_renderer.py1-56
The LegacyWindowsTerm class provides methods for terminal manipulation using the Windows Console API. It handles:
This class is used when ANSI escape sequences are not supported, ensuring Rich works even on older Windows systems.
When legacy rendering is required, Rich uses a specialized rendering pipeline to translate its segment-based model to Windows Console API calls.
Sources: rich/_windows_renderer.py7-56
The legacy_windows_render function in _windows_renderer.py processes Rich's segments and translates them to appropriate Windows Console API calls:
write_styled()write_text()This ensures a consistent user experience across different Windows environments.
Rich provides seamless integration with Jupyter notebooks, allowing you to use Rich's formatting capabilities in notebook cells.
Rich automatically detects when it's running in a Jupyter environment and adjusts its behavior accordingly.
Sources: tests/test_jupyter.py1-32
When running in a Jupyter environment, Rich:
These defaults can be customized through:
JUPYTER_COLUMNS environment variable to set the widthJUPYTER_LINES environment variable to set the heightwidth and height parameters to the Console constructorIn Jupyter notebooks, Rich renders its output as HTML, allowing for rich formatting that matches what you would see in a terminal.
| Feature | Jupyter Support | Notes |
|---|---|---|
| Colors | Full support | Truecolor (16M colors) |
| Styles | Full support | Bold, italic, underline, etc. |
| Tables | Full support | Rendered as HTML tables |
| Markdown | Full support | Rendered as styled HTML |
| Progress bars | Limited support | Static representation |
| Live displays | Limited support | Final state only |
Rich's Jupyter integration provides a consistent experience between terminal and notebook environments, making it ideal for data exploration and documentation.
Rich is designed to be extensible, allowing you to create custom renderables and extend existing components.
The core of Rich's extensibility is the renderable protocol, which allows any object to be rendered by implementing specific methods.
To make an object renderable by Rich, you have two options:
__rich__() to return an existing renderable__rich_console__() and __rich_measure__() for direct renderingThe __rich_console__ method generates segments for rendering, while __rich_measure__ provides sizing information for layout calculations.
You can extend Rich's existing components to customize their behavior while retaining core functionality.
Common extension patterns include:
For example, you might extend the Table class to automatically format financial data, or extend the Progress class to display application-specific metrics.
When using Rich across different platforms, there are several considerations to keep in mind.
Windows terminals have evolved significantly but still have some platform-specific behaviors:
When using Rich in Jupyter:
JUPYTER_COLUMNS and JUPYTER_LINES environment variablesforce_jupyter=True parameter can be used to force Jupyter rendering modeFor maximum compatibility across platforms:
Console.is_terminal property to detect when output is being redirectedBy understanding these platform-specific considerations, you can create Rich applications that work consistently across environments.
Sources: rich/_windows.py15-61 rich/_win32_console.py1-662 tests/test_jupyter.py1-32 <new_str>
Rich handles rendering differences across Windows legacy consoles, modern terminals, and Jupyter notebooks. The key detection and dispatch logic lives in rich/console.py, rich/_windows.py, rich/_win32_console.py, and rich/_windows_renderer.py.
Platform detection and rendering dispatch
Sources: rich/console.py582-688 rich/_windows.py15-61 rich/_win32_console.py29-572 rich/_windows_renderer.py1-56
detect_legacy_windows() in rich/console.py calls get_windows_console_features() from rich/_windows.py. That function returns a WindowsConsoleFeatures dataclass with .vt (Virtual Terminal processing available) and .truecolor fields.
When .vt is False, the Console sets legacy_windows=True and the output path uses legacy_windows_render() from rich/_windows_renderer.py, which translates Segment objects into LegacyWindowsTerm API calls rather than ANSI escape sequences.
_is_jupyter() in rich/console.py detects Google Colab, Databricks, and ZMQInteractiveShell environments. When Jupyter mode is active:
JUPYTER_DEFAULT_COLUMNS (115) unless JUPYTER_COLUMNS env var is setJUPYTER_DEFAULT_LINES (100) unless JUPYTER_LINES env var is setColorSystem.TRUECOLOR| Feature | Jupyter Support | Notes |
|---|---|---|
| Colors | Full (truecolor) | 16M colors |
| Styles | Full | Bold, italic, underline, etc. |
| Tables | Full | Rendered as HTML |
| Markdown | Full | Rendered as styled HTML |
| Progress bars | Limited | Static representation |
| Live displays | Limited | Final state only |
See page 7.3 for full documentation of platform support.
Sources: rich/console.py511-528 rich/console.py663-688
When record=True is set on the Console constructor, Rich accumulates a copy of all rendered Segment objects in _record_buffer. This data can then be exported in three formats.
Export and capture code path
Sources: rich/console.py316-346 rich/_export_format.py rich/terminal_theme.py
Console.capture() returns a Capture context manager (defined in rich/console.py). Inside the with block, output is diverted to an internal buffer. After the block exits, Capture.get() returns the captured string.
| Method | Output format | Notes |
|---|---|---|
export_text() | Plain text | Strips all ANSI/styles |
export_html() | HTML document | Uses CONSOLE_HTML_FORMAT template |
export_svg() | SVG document | Uses CONSOLE_SVG_FORMAT template; accepts TerminalTheme |
save_text(path) | Plain text file | Writes export_text() result |
save_html(path) | HTML file | Writes export_html() result |
save_svg(path) | SVG file | Writes export_svg() result |
SVG export accepts a theme parameter using one of the TerminalTheme presets from rich/terminal_theme.py (e.g., MONOKAI, DEFAULT_TERMINAL_THEME, SVG_EXPORT_THEME).
See page 7.4 for full documentation of export and capture.
Sources: rich/console.py316-346 rich/console.py1680-1800 rich/_export_format.py rich/terminal_theme.py
Rich provides specialized support for Windows terminals, which historically have different capabilities from Unix-like terminals.
When running on Windows, Rich automatically detects the terminal's capabilities to determine the optimal rendering approach.
Sources: rich/_windows.py15-61
Rich checks if the Windows terminal supports Virtual Terminal (VT) processing by querying the console mode using the Windows API. If VT processing is enabled, Rich can use ANSI escape sequences for formatting. For newer Windows versions (Windows 10 build 15063 or later with VT support), Rich also enables truecolor support.
The detection logic is implemented in the get_windows_console_features() function, which returns a WindowsConsoleFeatures object containing information about VT and truecolor support.
For older Windows environments without VT support, Rich falls back to using the Windows Console API directly through the LegacyWindowsTerm class.
Sources: rich/_win32_console.py29-572 rich/_windows_renderer.py1-56
The LegacyWindowsTerm class provides methods for terminal manipulation using the Windows Console API. It handles:
This class is used when ANSI escape sequences are not supported, ensuring Rich works even on older Windows systems.
When legacy rendering is required, Rich uses a specialized rendering pipeline to translate its segment-based model to Windows Console API calls.
Sources: rich/_windows_renderer.py7-56
The legacy_windows_render function in _windows_renderer.py processes Rich's segments and translates them to appropriate Windows Console API calls:
write_styled()write_text()This ensures a consistent user experience across different Windows environments.
Rich provides seamless integration with Jupyter notebooks, allowing you to use Rich's formatting capabilities in notebook cells.
Rich automatically detects when it's running in a Jupyter environment and adjusts its behavior accordingly.
Sources: tests/test_jupyter.py1-32
When running in a Jupyter environment, Rich:
These defaults can be customized through:
JUPYTER_COLUMNS environment variable to set the widthJUPYTER_LINES environment variable to set the heightwidth and height parameters to the Console constructorIn Jupyter notebooks, Rich renders its output as HTML, allowing for rich formatting that matches what you would see in a terminal.
| Feature | Jupyter Support | Notes |
|---|---|---|
| Colors | Full support | Truecolor (16M colors) |
| Styles | Full support | Bold, italic, underline, etc. |
| Tables | Full support | Rendered as HTML tables |
| Markdown | Full support | Rendered as styled HTML |
| Progress bars | Limited support | Static representation |
| Live displays | Limited support | Final state only |
Rich's Jupyter integration provides a consistent experience between terminal and notebook environments, making it ideal for data exploration and documentation.
Rich is designed to be extensible, allowing you to create custom renderables and extend existing components.
The core of Rich's extensibility is the renderable protocol, which allows any object to be rendered by implementing specific methods.
To make an object renderable by Rich, you have two options:
__rich__() to return an existing renderable__rich_console__() and __rich_measure__() for direct renderingThe __rich_console__ method generates segments for rendering, while __rich_measure__ provides sizing information for layout calculations.
You can extend Rich's existing components to customize their behavior while retaining core functionality.
Common extension patterns include:
For example, you might extend the Table class to automatically format financial data, or extend the Progress class to display application-specific metrics.
When using Rich across different platforms, there are several considerations to keep in mind.
Windows terminals have evolved significantly but still have some platform-specific behaviors:
When using Rich in Jupyter:
JUPYTER_COLUMNS and JUPYTER_LINES environment variablesforce_jupyter=True parameter can be used to force Jupyter rendering modeFor maximum compatibility across platforms:
Console.is_terminal property to detect when output is being redirectedBy understanding these platform-specific considerations, you can create Rich applications that work consistently across environments.
Sources: rich/_windows.py15-61 rich/_win32_console.py1-662 tests/test_jupyter.py1-32
Refresh this wiki
This wiki was recently refreshed. Please wait 2 days to refresh again.