Textualize /rich
Rich is a Python library for rich text and beautiful formatting in the terminal.
★ 57,777⑂ 2,457👁 522⑃ 1,509● 1,556▤ 177◈ 689
57,777
stars
+13
2,457
forks
522
watchers
1,509
prs
1,556
issues
177
releases
689
discussions
README
!Logo
English readme
• 简体中文 readme
• 正體中文 readme
• Lengua española readme
• Deutsche readme
• Läs på svenska
• 日本語 readme
• 한국어 readme
• Français readme
• Schwizerdütsch readme
• हिन्दी readme
• Português brasileiro readme
• Italian readme
• Русский readme
• فارسی readme
• Türkçe readme
• Polskie readme
Rich 是一个 Python 库,可以为您在终端中提供富文本和精美格式。
Rich 的 API 让在终端输出颜色和样式变得很简单。此外,Rich 还可以绘制漂亮的表格、进度条、markdown、语法高亮的源代码以及栈回溯信息(tracebacks)等——开箱即用。
!功能纵览
有关 Rich 的视频介绍,请参见
@fishnets88 录制的
calmcode.io。
兼容性
Rich 适用于 Linux,OSX 和 Windows。真彩色/表情符号可与新的 Windows 终端一起使用,Windows 的经典终端仅限 8 种颜色。
Rich 还可以与 Jupyter 笔记本一起使用,而无需其他配置。
安装说明
使用pip或其他 PyPI 软件包管理器进行安装。
sh
python -m pip install rich
Rich 的打印功能
想毫不费力地将 Rich 的输出功能添加到您的应用程序中,您只需导入 rich print 方法,它和 Python 内置的同名函数有着完全一致的函数签名。试试看:
python
from rich import print
print("Hello, [bold magenta]World[/bold magenta]!", ":vampire:", locals())
!Hello World
在交互式命令行(REPL)中使用 Rich
Rich 可以被安装到 Python 交互式命令行中,那样做以后,任何数据结构都可以被漂亮的打印出来,自带语法高亮。
python
from rich import pretty
pretty.install()
!REPL
使用控制台
想要对 Rich 终端内容进行更多控制,请您导入并构造一个控制台对象。
python
from rich.console import Console
console = Console()
Console 对象包含一个print方法,它和语言内置的print函数有着相似的接口。下面是一段使用样例:
python
console.print("Hello", "World!")
您可能已经料到,这时终端上会显示“ Hello World!”。请注意,与内置的“print”函数不同,Rich 会将文字自动换行以适合终端宽度。
有好几种方法可以为输出添加颜色和样式。您可以通过添加style关键字参数来为整个输出设置样式。例子如下:
python
console.print("Hello", "World!", style="bold red")
输出如下图:
!Hello World
这个范例一次只设置了一行文字的样式。如果想获得更细腻更复杂的样式,Rich 可以渲染一个特殊的标记,其语法类似于bbcode。示例如下:
python
console.print("Where there is a [bold cyan]Will[/bold cyan] there [u]is[/u] a [i]way[/i].")
!控制台标记
使用Console对象,你可以花最少的工夫生成复杂的输出。更详细的内容可查阅 Console API 文档。
Rich Inspect
Rich 提供一个 inspect 函数来给任意的 Python 对象打印报告,比如类(class)、实例(instance)和内置对象(builtin)等。
python
my_list = ["foo", "bar"]
from rich import inspect
inspect(my_list, methods=True)
!Log
查看 inspect 文档详细了解。
Rich 库内容
Rich 包含了一系列内置的 _可渲染类型(renderables)_ ,你可以用它们为命令行程序构建出优雅的输出,也可以拿它们来辅助调试你的代码。
点击以下标题查看详细:
日志(Log)
Console 对象有一个与print()类似的log()方法,但它会多输出一列内容,里面包含当前时间以及调用方法的文件行号。默认情况下,Rich 将针对 Python 结构和 repr 字符串添加语法高亮。如果您记录一个集合(如字典或列表),Rich 会把它漂亮地打印出来,使其切合可用空间。下面是其中一些功能的示例:
python
from rich.console import Console
console = Console()
test_data = [
{"jsonrpc": "2.0", "method": "sum", "params": [None, 1, 2, 4, False, True], "id": "1",},
{"jsonrpc": "2.0", "method": "notify_hello", "params": [7]},
{"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": "2"},
]
def test_log():
enabled = False
context = {
"foo": "bar",
}
movies = ["Deadpool", "Rise of the Skywalker"]
console.log("Hello from", console, "!")
console.log(test_data, log_locals=True)
test_log()
以上范例的输出如下:
!日志
注意其中的log_locals参数会输出一个表格,该表格包含调用 log 方法的局部变量。
log 方法既可用于将常驻进程(例如服务器进程)的日志打印到终端,在调试时也是个好帮手。
日志处理器(Logging Handler)
您还可以使用内置的处理器类来对 Python 的 logging 模块的输出进行格式化和着色。下面是输出示例:
!记录
Emoji 表情
将名称放在两个冒号之间即可在控制台输出中插入 emoji 表情符。示例如Top issues
#2793[BUG] JupyterMixin adds spurious trailing newline17 comments · 2023-02-02#1859[REQUEST] Support for PEP 654 tracebacks (ExceptionGroups)14 comments · 2022-01-22#2691console.status 'blinking' artifact13 comments · 2022-12-07#3264[BUG] Addressing Dashboard Freeze and Cursor Flicker Issues in Real-Time Data Applications Using Rich11 comments · 2024-01-25#3049[BUG] box.py susceptible to breakage when commited with trailing-whitespace pre-commit hook11 comments · 2023-07-21#4208[BUG]Console.save_text clears recorded output when file write fails10 comments · 2026-08-16#2073[BUG] When pretty-printed, items with a multi-line repr are not indented10 comments · 2022-03-16#4161[/usr/local/lib/python3.14/site-packages/rich/segment.py:547: SyntaxWarning: 'return' in a 'finally' block return9 comments · 2026-06-14
Discussions
#4248Documentation improvements, how do I suggest fixes/make changes?3 comments · 2026-11-05#4236Optional OSC 7501 (Program Status Protocol) reporting from Progress/Status/Live0 comments · 2026-10-08#4230Please tell me how to fix this strange colouring scheme 'e:ba' and 'e:fe' turns out green2 comments · 2026-09-25#4227[REQUEST] Filtering select() items from keyboard for huge option list1 comments · 2026-09-22#4221Suggestion: Supported way to override the SVG `@font-face` block2 comments · 2026-09-14#4219Looking to Contribute - What Should I Work On?1 comments · 2026-09-07#4218Embed Agent-Friendly Score badge in README0 comments · 2026-09-05#4217More meaningful default for colors0 comments · 2026-09-04
Latest release
The So Long 3.8 Release v15.0.0 2026-04-12A few fixes. The major version bump is to honor the passing of 3.8 support which reached its EOL in October 7, 2024 [15.0.0] - 2026-04-12 Changed - Breaking change: Dropped support for Python3.8 Fixed - Fixed empty print ignoring the end parameter https://github.com/Textualize/rich/pull/4075 - Fixed Text.from_ansi removing newlines https://github.com/Textualize/rich/pull/4076 - Fixed FileProxy.isatty not proxying https://github.com/Textualize/rich/pull/4077 - Fixed inline code in Markdown tables cells https://github.com/Textualize/rich/pull/4079