Console
console 通常作为单例模式存在,所以你可以为 console 的配置专门创建一个文件 console.py
:
from rich.console import Console
console = Console()
然后在任何需要的地方引用之
from <my_project>.console import console
Attributes
渲染时,Rich 可以自动地检查一些所需参数,比如这些:
size
is the current dimensions of the terminal (which may change if you resize the window).encoding
is the default encoding (typically “utf-8”).is_terminal
is a boolean that indicates if the Console instance is writing to a terminal or not.color_system
is a string containing the Console color system (see below).
这些参数当然也可以在 console 实例化时传进去
Color systems
You can set color_system
to one of the following values:
None
不要颜色"auto"
auto-detect"standard"
Can display 8 colors, with normal and bright variations, for 16 colors in total."256"
Can display the 16 colors from “standard” plus a fixed palette of 240 colors."truecolor"
Can display 16.7 million colors, which is likely all the colors your monitor can display."windows"
Can display 8 colors in legacy Windows terminal. New Windows terminal can display “truecolor”.
谨慎地设置
color_system
参数,避免设置 terminal 不支持的更高颜色,这会让输出完全不可读,最好的方法是别设置
Printing
print()
方法很聪明,它能根据 __str__
方法将对象转换为 str
,并施以简单的语法高亮
它还支持一些语法标记的渲染:
console.print([1, 2, 3])
console.print("[blue underline]Looks like a link")
console.print(locals())
console.print("FOO", style="white on blue")
可以使用终端协议(Console Protocol)为对象提供自定义渲染
Logging
The log()
methods offers the same capabilities as print, but adds some features useful for debugging a running application.
logging 会把当前时间展示在最左边,把调用所在的文件和行号放在最右边
>>> console.log("Hello, World!")
log()
方法拥有一个 log_locals
参数,如果将其设为 True
,Rich 会把调用处的局部变量打印出来
Printing JSON
print_json()
方法会尝试把一个字符串格式化成 json 打印出来:
console.print_json('[false, true, null, "foo"]')
You can also log json by logging a JSON
object:
from rich.json import JSON
console.log(JSON('["foo", "bar"]'))
由于打印 json 是一个频繁的需求,你也可以从 main namespace 引入 print_json
from rich import print_json
You can also pretty print JSON via the command line with the following:
python -m rich.json cats.json
Low level output
out()
方法输出级别更低,它将所有参数以字符串形式输出,并且不施以美化、标记语法,但会施以基本的格式化(包括一丢丢高亮)
>>> console.out("Locals", locals())
Rules
rule()
方法会绘制一条水平线,中间嵌入标题(可选)
>>> console.rule("[bold red]Chapter 2")
─────────────────────────────── Chapter 2 ───────────────────────────────
The rule method also accepts a style
parameter to set the style of the line, and an align
parameter to align the title (“left”, “center”, or “right”).
Status
Rich can display a status message with a ‘spinner’ animation that won’t interfere with regular console output. Run the following command for a demo of this feature:
python -m rich.status
To display a status message, call status()
with the status message (which may be a string, Text, or other renderable). The result is a context manager which starts and stop the status display around a block of code. Here’s an example:
with console.status("Working..."):
do_work()
You can change the spinner animation via the spinner
parameter:
with console.status("Monkeying around...", spinner="monkey"):
do_work()
Run the following command to see the available choices for spinner
:
python -m rich.spinner