目录
rich 库
Rich 是一个用于在终端和命令行界面中显示富文本的 Python 库。它可以帮助开发者在终端中渲染各种文本样式、颜色和布局,并支持在表格、面板、进度条等控件中展示数据。使用 Python Rich,开发者可以创建交互式的终端应用程序、漂亮的报告和文档,提高用户体验和可读性。
Python Rich 支持各种文本样式,如加粗、斜体、下划线、删除线、高亮等,也支持不同的颜色、背景色和标记符号。此外,Python Rich 还支持自定义样式和主题,让用户可以根据自己的需求定制外观和风格。
Python Rich 还提供了丰富的控件,如表格、面板、进度条、树形结构等,让开发者可以更方便地展示和组织数据。同时,Python Rich 还支持在终端中播放音频和视频,以及在终端中绘制图形。
总的来说,Python Rich 是一个非常实用和强大的终端美化库,可以让开发者更方便地开发交互式的终端应用程序,提高用户体验和可读性。
实例化
from rich.console import Console
console = Console()
console.print("Where there is a [bold cyan]Will[/bold cyan] there [u]is[/u] a [i]way[/i].")
console.print("Hello, [bold magenta]World[/bold magenta]!", ":vampire:")
console.print(":smiley: :vampire: :pile_of_poo: :thumbs_up: :raccoon:")
from rich.table import Column, Table
table = Table(show_header=True, header_style="bold magenta")
table.add_column("Date", style="dim", width=12)
table.add_column("Title")
table.add_column("Production Budget", justify="right")
table.add_column("Box Office", justify="right")
table.add_row(
"Dev 20, 2019", "Star Wars: The Rise of Skywalker", "$275,000,000", "$375,126,118"
)
table.add_row(
"May 25, 2018",
"[red]Solo[/red]: A Star Wars Story",
"$275,000,000",
"$393,151,347",
)
table.add_row(
"Dec 15, 2017",
"Star Wars Ep. VIII: The Last Jedi",
"$262,000,000",
"[bold]$1,332,539,889[/bold]",
)
console.print(table)
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
效果:
美化列输出
from rich.console import Console
from rich.columns import Columns
from rich.panel import Panel
console = Console()
left_panel = Panel("Left panel", title="Panel 1")
right_panel = Panel("Right panel", title="Panel 2")
columns = Columns([left_panel, right_panel], equal=True)
console.print(columns)
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
效果:
美化进度条
from rich.progress import track
for step in track(range(100)):
pass
- 4
效果:
美化 Markdown 文档
from rich.markdown import Markdown
with open("README.md") as readme:
markdown = Markdown(readme.read())
console.print(markdown)
- 4
- 5
效果:
美化代码高亮
from rich.syntax import Syntax
my_code = '''
def iter_first_last(values: Iterable[T]) -> Iterable[Tuple[bool, bool, T]]:
"""Iterate and generate a tuple with a flag for first and last value."""
iter_values = iter(values)
try:
previous_value = next(iter_values)
except StopIteration:
return
first = True
for value in iter_values:
yield first, False, previous_value
first = False
previous_value = value
yield first, True, previous_value
'''
syntax = Syntax(my_code, "python", theme="monokai", line_numbers=True)
console.print(syntax)
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
效果:
美化异常追踪
import rich.traceback
rich.traceback.install()
a = 1 / 0
- 4
美化日志输出
from rich.console import Console
console = Console()
test_data = [
{"jsonrpc": "https://files.jxasp.com/image/2.0", "method": "sum", "params": [None, 1, 2, 4, False, True], "id": "1",},
{"jsonrpc": "https://files.jxasp.com/image/2.0", "method": "notify_hello", "params": [7]},
{"jsonrpc": "https://files.jxasp.com/image/2.0", "method": "subtract", "params": [42, 23], "id": "https://files.jxasp.com/image/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()
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
效果: