1 说明:
=====
1.1 Rich库,是python的一个库文件,可以在终端显示多彩的、格式化、富文本的文字、表格和进度条。
1.2 环境:
华为笔记本电脑、深度deepin-linux操作系统、python3.8和微软vscode编辑器。
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:
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:
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 效果图
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 图:
自己整理并分享出来,喜欢的点赞、转发和收藏。