Skills2026年4月14日·1 分钟阅读

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.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
step-1.md
直接安装命令
npx -y tokrepo@latest install cd60c899-37b4-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

TL;DR
Rich adds colorized output, tables, progress bars, and syntax highlighting to any Python CLI with minimal code.
§01

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.

§02

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.

§03

How to use

  1. Install Rich:
pip install rich
  1. 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)
  1. Run the built-in demo to see all features:
python -m rich
§04

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)
§05

Related on TokRepo

§06

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) or Console(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 use console.print(text, highlight=False).
  • Rich and logging: use RichHandler to integrate with Python's logging module. Do not mix console.print and logging.info without a handler, or output will interleave unpredictably.

常见问题

What Python versions does Rich support?+

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.

Can Rich render markdown in the terminal?+

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.

How does Rich compare to colorama?+

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.

Does Rich work in Jupyter notebooks?+

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.

Can I use Rich for logging?+

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()])`.

引用来源 (3)

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产