首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

每日一模块:rich

一、引言

rich是一个 Python 库,旨在创建丰富且美观的终端输出。它提供了多种功能,如彩色文本、表格、进度条、树形视图等,让终端界面变得更加生动和直观。

二、安装

你可以使用 pip 安装rich库:

pip?install?rich

三、基本用法

需要在terminal中执行,如python xx.py

1. 彩色文本

使用rich.print可以轻松打印彩色文本。例如:

from?rich?import?print

print("[bold?red]这是红色加粗文本[/]")

print("[green]这是绿色文本[/]")

2. 表格

rich提供了创建和显示表格的功能:

from?rich.table?import?Table

table?=?Table(title="示例表格")

table.add_column("姓名",?justify="left")

table.add_column("年龄",?justify="right")

table.add_row("张三",?"[yellow]25")

table.add_row("李四",?"[red]30")

print(table)

3. 进度条

rich可以方便地显示进度条:

from?rich.progress?import?Progress

import?time

with?Progress()?as?progress:

task?=?progress.add_task("任务进度",?total=100)

for?i?in?range(101):

time.sleep(0.1)??#?模拟耗时操作

progress.update(task,?advance=1)

4. 盒子和面板

使用rich.panel可以创建面板:

from?rich.panel?import?Panel

panel?=?Panel("这是一个面板",?title="面板标题")

print(panel)

5. 高亮文本

rich支持文本高亮功能:

from?rich.console?import?Console

from?rich.highlighter?import?RegexHighlighter

from?rich.theme?import?Theme

class?EmailHighlighter(RegexHighlighter):

"""正则匹配高亮邮箱名称"""

base_style?=?"example."

highlights?=?[r"(?P<email>[\w-]+@([\w-]+\.)+[\w-]+)"]

theme?=?Theme({"example.email":?"bold?magenta"})

console?=?Console(highlighter=EmailHighlighter(),?theme=theme)

console.print("Send?funds?to?money@example.org")

6. 树形视图

rich提供了创建树形视图的功能:

from?rich.tree?import?Tree

tree?=?Tree("根节点")

child1?=?tree.add("子节点?1")

tree.add("子节点?2")

child1.add("[red]孙节点?1")

child1.add("孙节点?2")

print(tree)

7. Markdown渲染

rich支持Markdown格式的文本渲染,允许你在终端中显示格式化的文本。

from?rich?import?print

from?rich.markdown?import?Markdown

markdown_text?=?"""

#?标题

-?列表项?1

-?列表项?2

>?引用内容

**加粗文本**

*?斜体文本*

"""

m?=?Markdown(markdown_text)

print(m)

8. 动态更新

rich支持在终端中动态更新内容,而不需要清除整个屏幕或重新打印所有内容。

from?rich.console?import?Console

from?rich.live?import?Live

from?time?import?sleep

with?Live(console=Console(),?refresh_per_second=2)?as?live:

for?i?in?range(10):

live.update(f"进度:?{i?*?10}%")

sleep(1)

9. 控制台交互

rich提供了一些交互式组件,如输入框、选择框等,用于在控制台中与用户进行交互。

from?rich.prompt?import?Prompt

name?=?Prompt.ask("请输入你的名字:")

print(f"你好,{name}!")

10. 自定义布局

rich允许你使用容器和布局来创建复杂的终端界面。

from?rich.console?import?Console

from?rich.layout?import?Layout

from?rich.panel?import?Panel

from?rich.text?import?Text

console?=?Console()

text1?=?Text("这是第一块内容")

text2?=?Text("这是第二块内容")

layout?=?Layout(name="my_layout")

layout.split_row(

Layout(Panel(text1,?title="面板1")),

Layout(Panel(text2,?title="面板2")),

)

console.print(layout)

11. 自定义样式

rich允许你自定义文本、表格、进度条等组件的样式。

from?rich.console?import?Console

from?rich.style?import?Style

console?=?Console()

custom_style?=?Style(color="blue",?blink=True,?bold=True)

console.print("[custom]自定义样式文本[/custom]",?style=custom_style)

12. 异步输出

rich支持异步输出,这对于在长时间运行的程序中更新终端界面非常有用。

import?asyncio

from?rich.progress?import?track

async?def?long_running_task():

for?i?in?track(range(100),?description="任务进度"):

await?asyncio.sleep(0.1)

asyncio.run(long_running_task())

官方文档地址:https://rich.readthedocs.io/en/stable/introduction.html

四、总结

rich库为 Python 提供了丰富的终端输出功能,包括彩色文本、表格、进度条、盒子、面板、高亮文本和树形视图等。通过使用rich,你可以创建出美观且易于阅读的终端界面。更多高级功能和用法可以通过查阅官方文档和示例来进一步了解和学习。

  • 发表于:
  • 原文链接https://page.om.qq.com/page/OtnJUpj4kW9mPZJXJTCBt7Sw0
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券
http://www.vxiaotou.com