富文本被原样输出_Rich库:是一个向终端呈现富文本和精美的输出的Python库

1 说明:

=====

1.1 Rich库,是python的一个库文件,可以在终端显示多彩的、格式化、富文本的文字、表格和进度条。

1.2 环境:

华为笔记本电脑、深度deepin-linux操作系统、python3.8和微软vscode编辑器。

9453c35ab73f26c4462899db54c1ade6.png

2 Rich库:

=======

2.1 官网:

#github地址https://github.com/willmcgugan/rich#学习文档https://rich.readthedocs.io/en/latest/index.html

2.2 安装:

pip install rich#本机安装sudo pip3.8 install rich

2.3 提示报错:

WARNING: The script cmark is installed in '/usr/local/python3.8/bin' which is not on PATH.  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.  WARNING: The script pypprint is installed in '/usr/local/python3.8/bin' which is not on PATH.  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

2.4 其实就是没有进行软连接,本机软连接示范:

sudo ln -s /usr/local/python3.8/bin/cmark /usr/bin/cmarksudo ln -s /usr/local/python3.8/bin/pypprint /usr/bin/pypprint

3 进度条:

=======

3.1 比较有特色。

3.2 代码:progress1.py

from rich.progress import trackimport timefor step in track(range(30)):    print('We love Python!')    time.sleep(0.5)

效果图1:

e28b0052a4b881a852381340a76fbc1b.gif

3.3 代码:progress2.py

from rich.progress import Progressimport timewith Progress() as progress:    task1 = progress.add_task("[red]Downloading...", total=1000)    task2 = progress.add_task("[green]Processing...", total=1000)    task3 = progress.add_task("[cyan]Cooking...", total=1000)    while not progress.finished:        progress.update(task1, advance=0.5)        progress.update(task2, advance=0.3)        progress.update(task3, advance=0.9)        time.sleep(0.02)

效果图2:

d647f32615d8baeb5ad64dd344d3597b.gif

4 终端彩色的、富文本打印:

4.1 代码:

#方法一from rich import print#对world进行bold=粗体,颜色=magentaprint("Hello [bold magenta]World[/bold magenta]!")print("[bold magenta]Hello World![/bold magenta]")#方法二from rich.console import Consoleconsole = Console()#console.print("Hello", "World!")  #普通输出#整体字体和颜色设置console.print("Hello", "World!", style="bold magenta")#代表换行#[u]is[/u]代表下划线,[i]way[/i]代表斜体console.print("Where there is a [bold cyan]Will[/bold cyan] there [u]is[/u] a [i]way[/i].")

4.2 效果图

b3154f40c59d9978c28cefacc473da02.png

5 表格终端输出:

============

5.1 代码:

'''#方法一from rich.console import Consolefrom rich.table import Column, Tableconsole = Console()#表头设置header_style="bold magenta"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)'''#方法二:更简洁from rich import printfrom rich.table import Column, Table#表头设置header_style="bold magenta"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]",)print(table)

5.2 图:

64e7cd82c9070311fd9fceab267be7ba.png

自己整理并分享出来,喜欢的点赞、转发和收藏。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值