“Beautiful is better than ugly.”
——Tim Peters在《The Zen of Python》中写道。
pip install rich
conda install rich
from rich.console import Console
console = Console()
test_data = [
{"method": "sum", "params": [None, 1, 2, False], "id": "1",},
{"method": "notify_hello", "params": [7]},
{"method": "subtract", "params": [42, 23], "id": "2"},
]
console.log(test_data, )
输出:
非常的简单,终端可以正确的显示表情符号。
输入代码:
from rich.console import Console
from rich.table import Table
table = Table(title="Star Wars Movies")
table.add_column("Released", justify="right", style="cyan", no_wrap=True)
table.add_column("Title", style="magenta")
table.add_column("Box Office", justify="right", style="green")
table.add_row("Dec 20, 2019", "Star Wars: The Rise...", "$952,110,690")
table.add_row("May 25, 2018", "Solo: A Star Wars Story", "$393,151,347")
table.add_row("Dec 15, 2017", "Star Wars Ep. V111: The Last Jedi", "$1,332,539,889")
table.add_row("Dec 16, 2016", "Rogue One: A Star Wars Story", "$1,332,439,889")
console = Console()
console.print(table)
输入代码:
from rich.tree import Tree
from rich import print
tree = Tree("Rich Tree")
tree.add("foo")
tree.add("bar")
baz_tree = tree.add("baz")
baz_tree.add("[red]Red").add("[green]Green").add("[blue]Blue")
print(tree)
输出:
import json
from urllib.request import urlopen
from rich.console import Console
from rich.columns import Columns
from rich.panel import Panel
def get_content(user):
"""Extract text from user dict."""
country = user["location"]["country"]
name = f"{user['name']['first']} {user['name']['last']}"
return f"[b]{name}[/b]\n[yellow]{country}"
console = Console()
users = json.loads(urlopen("https://randomuser.me/api/?results=30").read())["results"]
user_renderables = [Panel(get_content(user), expand=True) for user in users]
console.print(Columns(user_renderables))
输出:
from rich.console import Console
console = Console()
try:
do_something()
except Exception:
console.print_exception(show_locals=True)
输出:
最后欢迎大家投递雷火UX设计部面向2022届毕业生的校招岗位
本文来自微信公众号“网易雷火UX用户体验中心”(ID:LeihuoUX)。大作社经授权转载,该文观点仅代表作者本人,大作社平台仅提供信息存储空间服务。