一个炫酷的Python库,让你的终端输出方式,让你逼格秒变高!

在这里插入图片描述
不知道你统计过没有,你一天运行过多少次终端命令行,我自己是挺多次的,不过原版的终端显示多是黑白,看多了难免枯燥,而且会错过很多重要的信息,不能及时提醒。

一次偶然的机会我见到了绿色的 progress 进度条:
在这里插入图片描述
从此以后,便对这种带颜色的终端显示开始研究,经过一段时间知道了tqdm和colorama 这两个开源项目。让我的黑白终端开始有了色彩,就像从黑白照片过度到彩色照片,感觉终端都有了生命!感觉写代码的体验一下次上升了一个级别。

直到最近,我在逛 github 的时候,看到了这样一幅图片:
在这里插入图片描述
在这里插入图片描述
这是什么神奇的操作!多么优雅的色彩!

这里要介绍的就是最近 Github 榜单很火热的开源项目——Rich

Rich 是一个用于实现终端多色彩多内容显示的开源 Python 库。

**支持1600万种颜色显示!**多种格式字体显示(粗体斜体划线 等)

支持多种格式的表格、进度条、markdown,语法高亮,错误回溯等显示方式!囊括所有你想要的输出方式。
在这里插入图片描述
01.安装

pip install rich

02.使用

直接用 print 输出

只需要引入 rich 包之后,在 print 内容加入想要的配置即可。

from rich import print
print("Hello, [bold magenta]World[/bold magenta]!",":vampire:", locals())

运行看一下效果
在这里插入图片描述
03.采用 console 输出

Console 是另一种更方便配置化的输出形式,输入的时候直接配置 style即可。

from rich.console import Console

console = Console()
console.print("Hello", "World", style="bold red")

运行看一下效果
在这里插入图片描述
当然,也可以用字符串直接配置的方式进行:

console.print("Where there is a[bold cyan]Will[/bold cyan] there [u]is[/u] a [i]way[/i].")

在这里插入图片描述

04.Console Logging

使用 Console方式打印出来的 log 信息更加清晰,默认支持语法高亮。

可以选择直接输出为表格的形式,更方便查看。

而且打印出来的表格是会根据目前的窗口自适应的。

from rich.console import Console
console = Console()

test_data = [
    {"jsonrpc": "2.0", "method": "sum", "params": [None,1,2,4,False,True], "id": "1"},
    {"jsonrpc": "2.0", "method": "noti_hello", "params": [7]},
    {"jsonrpc": "2.0", "method": "subtract", "params": [42,23},"id": "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()

原来log也可以这么漂亮:
在这里插入图片描述
05.进度条

这是我最心仪的进度条方式,直接多进度条同时输出。

from rich.progress import track

for step in track(range(100)):
    do_step(step)

在这里插入图片描述

真的很酷啊,立马感觉逼格很高!
06.Markdown

rich也支持直接打印markdown格式,这样在打印说明文档或者提供包信息的时候更明了。

from rich.console import Console
from rich.markdown import Markdown

console = Console()
with open("README.md") as readme:
    markdown = Markdown(readme.read())
console.print(markdown)

在这里插入图片描述

07.语法高亮

rich采用pygments库来支持语法高亮,同markdown的渲染方式相同。

from rich.console import Console
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 lastvalue."""
    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
'''
syntax = Syntax(my_code, "python", theme="monokai", line_numbers=True)
console = Console()
console.print(syntax)

在这里插入图片描述

08.Table

rich可以渲染自适应的表格,而且支持表格的格式配置(边框、风格、单元格对齐等)

from rich.console import Console
from rich.table import Column, Table

console = Console()

table = Table(show_header=True, header_style="bold magenta")
table.add_column("Date", stele="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,110"
)
table.add_row(
    "May 25, 2018",
    "[red]Solo[/red]: A Star Wars Store",
    "$262,000,000",
    "[bold]$1,332,539,889[/bold]"
)

console.print(table)

在这里插入图片描述

有了这个神器之后,我的终端再也不是黑白的世界了,虽然有linux的一些主题的调整,但是远没有这个神器这么功能强大,还等啥呢,赶紧下载安装爽起来!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值