Rich is a Python library for writing rich text (with color and style) to the terminal, and for displaying advanced content such as tables, markdown, and syntax highlighted code.
模块的安装
1 | pip install rich |
安装可能的异常记录
模块功能
rich模块,可以对结果格式进行大量的富文本展示方式,比如文字颜色,比如框选
╭───────────────╮
│ Hello, World! │
╰───────────────╯
目前主要使用该模块展示大数据的分析进度,所以其他富文本格式暂未详细学习,后续在补充。
展示进度条
日常进行大数据处理中,实时了解处理进度是一个比较重要的需求,通过rich包,我们可以展示任务执行的进度条,tqdm是另一个比较传统的工具,两者对应的实例代码和展示样式如下图:
针对循环处理
1 | from tqdm import tqdm |
针对迭代处理
其实处理循环,还有一类工作就是迭代,往往我们也想要知道进度,但如果这时候,只是添加 tqdm(),我们只能看到每个迭代的处理速度,和迭代的处理总数。
所以为了获得进度条,我们需要但是使用 total 指定任务的总数。1
2for i in tqdm(df.iteritems(), total=df.shape[0]):
time.sleep(0.1)
针对pandas的处理
1 | from tqdm import tqdm |
输出样式
通过使用rich的样式,可以实现输出文字的加粗,1
2
3
4
5
6
7
8
9from rich.style import Style
from rich.console import Console
console = Console()
console.print("Danger, Will Robinson!", style="blink bold red underline on white") # 白底红字
console.print("foo [not bold]bar[/not bold] baz", style="bold") # 加粗
console.print("Danger, Will Robinson!", style="conceal") # 暗色模式
console.print("Danger, Will Robinson!", style="italic") # 斜体
console.print("Danger, Will Robinson!", style="strike") # 加删除线
console.print("Danger, Will Robinson!", style="underline") # 加下划线