| 1 |
[](https://pypi.org/project/rich/) [](https://badge.fury.io/py/rich) |
| 2 |
|
| 3 |
[](https://pepy.tech/project/rich) |
| 4 |
[](https://codecov.io/gh/Textualize/rich) |
| 5 |
[](https://www.willmcgugan.com/tag/rich/) |
| 6 |
[](https://twitter.com/willmcgugan) |
| 7 |
|
| 8 |
 |
| 9 |
|
| 10 |
[English readme](https://github.com/textualize/rich/blob/master/README.md) |
| 11 |
• [简体中文 readme](https://github.com/textualize/rich/blob/master/README.cn.md) |
| 12 |
• [正體中文 readme](https://github.com/textualize/rich/blob/master/README.zh-tw.md) |
| 13 |
• [Lengua española readme](https://github.com/textualize/rich/blob/master/README.es.md) |
| 14 |
• [Deutsche readme](https://github.com/textualize/rich/blob/master/README.de.md) |
| 15 |
• [Läs på svenska](https://github.com/textualize/rich/blob/master/README.sv.md) |
| 16 |
• [日本語 readme](https://github.com/textualize/rich/blob/master/README.ja.md) |
| 17 |
• [한국어 readme](https://github.com/textualize/rich/blob/master/README.kr.md) |
| 18 |
• [Français readme](https://github.com/textualize/rich/blob/master/README.fr.md) |
| 19 |
• [Schwizerdütsch readme](https://github.com/textualize/rich/blob/master/README.de-ch.md) |
| 20 |
• [हिन्दी readme](https://github.com/textualize/rich/blob/master/README.hi.md) |
| 21 |
• [Português brasileiro readme](https://github.com/textualize/rich/blob/master/README.pt-br.md) |
| 22 |
• [Italian readme](https://github.com/textualize/rich/blob/master/README.it.md) |
| 23 |
• [Русский readme](https://github.com/textualize/rich/blob/master/README.ru.md) |
| 24 |
• [فارسی readme](https://github.com/textualize/rich/blob/master/README.fa.md) |
| 25 |
• [Türkçe readme](https://github.com/textualize/rich/blob/master/README.tr.md) |
| 26 |
• [Polskie readme](https://github.com/textualize/rich/blob/master/README.pl.md) |
| 27 |
|
| 28 |
Rich 是一款提供終端機介面中 _豐富的_ 文字效果及精美的格式設定的 Python 函式庫。 |
| 29 |
|
| 30 |
[Rich API](https://rich.readthedocs.io/en/latest/) 讓終端機介面加上色彩及樣式變得易如反掌。Rich 也可以繪製漂亮的表格、進度條、Markdown、語法醒目標示的程式碼、Traceback(追溯)……。 |
| 31 |
|
| 32 |
 |
| 33 |
|
| 34 |
關於 Rich 的介紹,請參見 [@fishnets88](https://twitter.com/fishnets88) 在 [calmcode.io](https://calmcode.io/rich/introduction.html) 錄製的影片。 |
| 35 |
|
| 36 |
[看看其他人對於 Rich 的討論](https://www.willmcgugan.com/blog/pages/post/rich-tweets/)。 |
| 37 |
|
| 38 |
## 相容性 |
| 39 |
|
| 40 |
Rich 可在 Linux、macOS、Windows 上運作。在新的 Windows Terminal 中可支援顯示全彩及 Emoji,但傳統的終端機中僅支援 16 色。Rich 最低需要的 Python 版本為 3.6.3。 |
| 41 |
|
| 42 |
Rich 可在 [Jupyter notebooks](https://jupyter.org/) 上使用,無須額外設定。 |
| 43 |
|
| 44 |
## 安裝 |
| 45 |
|
| 46 |
以 `pip` 或 PyPI 套件管理器安裝。 |
| 47 |
|
| 48 |
```sh |
| 49 |
python -m pip install rich |
| 50 |
``` |
| 51 |
|
| 52 |
以此命令測試 Rich 在終端機的輸出效果: |
| 53 |
|
| 54 |
```sh |
| 55 |
python -m rich |
| 56 |
``` |
| 57 |
|
| 58 |
## Rich Print |
| 59 |
|
| 60 |
匯入 [rich print](https://rich.readthedocs.io/en/latest/introduction.html#quick-start) 方法就可以輕鬆地讓程式進行 rich 輸出,rich print 與 Python 內建的函式用法相似。試試: |
| 61 |
|
| 62 |
```python |
| 63 |
from rich import print |
| 64 |
|
| 65 |
print("Hello, [bold magenta]World[/bold magenta]!", ":vampire:", locals()) |
| 66 |
``` |
| 67 |
|
| 68 |
 |
| 69 |
|
| 70 |
## Rich REPL |
| 71 |
|
| 72 |
Rich 可以安裝在 Python REPL 中,如此一來就可以漂亮的輸出與突顯標示任何資料結構。 |
| 73 |
|
| 74 |
```python |
| 75 |
>>> from rich import pretty |
| 76 |
>>> pretty.install() |
| 77 |
``` |
| 78 |
|
| 79 |
 |
| 80 |
|
| 81 |
## 使用 Console |
| 82 |
|
| 83 |
匯入並建構 [Console](https://rich.readthedocs.io/en/latest/reference/console.html#rich.console.Console) 物件,以更全面地控制 rich 終端機內容。 |
| 84 |
|
| 85 |
```python |
| 86 |
from rich.console import Console |
| 87 |
|
| 88 |
console = Console() |
| 89 |
``` |
| 90 |
|
| 91 |
Console 物件有個 `print` 方法,且刻意設計的與內建 `print` 函式相似。參考此範例: |
| 92 |
|
| 93 |
```python |
| 94 |
console.print("Hello", "World!") |
| 95 |
``` |
| 96 |
|
| 97 |
如同預期的,這會將 `"Hello World!"` 印在終端機。須注意不同於內建的 `print` 函式,Rich 會自動將過長的文字換行,以符合終端機的寬度。 |
| 98 |
|
| 99 |
有幾種加上顏色及樣式的方式。您可以用 `style` 引數設定輸出內容的樣式,參考此範例: |
| 100 |
|
| 101 |
```python |
| 102 |
console.print("Hello", "World!", style="bold red") |
| 103 |
``` |
| 104 |
|
| 105 |
輸出結果如下圖: |
| 106 |
|
| 107 |
 |
| 108 |
|
| 109 |
介紹完了如何對整行文字設定樣式,接著來看看更細部的使用。Rich 可以接受類似 [bbcode](https://en.wikipedia.org/wiki/BBCode) 的語法,對個別文字設定樣式。參考此範例: |
| 110 |
|
| 111 |
```python |
| 112 |
console.print("Where there is a [bold cyan]Will[/bold cyan] there [u]is[/u] a [i]way[/i].") |
| 113 |
``` |
| 114 |
|
| 115 |
 |
| 116 |
|
| 117 |
您可以用 Console 物件不費吹灰之力地達成細膩的輸出效果。參閱 [Console API](https://rich.readthedocs.io/en/latest/console.html) 說明文件以了解細節。 |
| 118 |
|
| 119 |
## Rich Inspect |
| 120 |
|
| 121 |
Rich 提供了 [inspect](https://rich.readthedocs.io/en/latest/reference/init.html?highlight=inspect#rich.inspect) 函式,可以對任何 Python 物件,如 class、instance 或 builtin ,為其產生一份報告。 |
| 122 |
|
| 123 |
```python |
| 124 |
>>> my_list = ["foo", "bar"] |
| 125 |
>>> from rich import inspect |
| 126 |
>>> inspect(my_list, methods=True) |
| 127 |
``` |
| 128 |
|
| 129 |
 |
| 130 |
|
| 131 |
參閱 [inspect 說明文件](https://rich.readthedocs.io/en/latest/reference/init.html#rich.inspect) 以了解細節。 |
| 132 |
|
| 133 |
# Rich 函式庫 |
| 134 |
|
| 135 |
Rich 包含了一系列可繪製的物件,您可以使用它們來印出精美的畫面,或者協助偵錯程式碼。 |
| 136 |
|
| 137 |
按一下子標題以了解細節: |
| 138 |
|
| 139 |
<details> |
| 140 |
<summary>Log</summary> |
| 141 |
|
| 142 |
Console 物件提供了 `log()` 方法,使用方式與 `print()` 類似,但還多了一欄來顯示目前時間、進行呼叫的檔案及行號。預設情況下 Rich 會語法醒目標示 Python 的結構及 repr 字串。若使用於字典或串列這類集合性物件,Rich 會將其漂亮地印出來,以符合可用空間。此範例示範了這些功能。 |
| 143 |
|
| 144 |
```python |
| 145 |
from rich.console import Console |
| 146 |
console = Console() |
| 147 |
|
| 148 |
test_data = [ |
| 149 |
{"jsonrpc": "2.0", "method": "sum", "params": [None, 1, 2, 4, False, True], "id": "1",}, |
| 150 |
{"jsonrpc": "2.0", "method": "notify_hello", "params": [7]}, |
| 151 |
{"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": "2"}, |
| 152 |
] |
| 153 |
|
| 154 |
def test_log(): |
| 155 |
enabled = False |
| 156 |
context = { |
| 157 |
"foo": "bar", |
| 158 |
} |
| 159 |
movies = ["Deadpool", "Rise of the Skywalker"] |
| 160 |
console.log("Hello from", console, "!") |
| 161 |
console.log(test_data, log_locals=True) |
| 162 |
|
| 163 |
|
| 164 |
test_log() |
| 165 |
``` |
| 166 |
|
| 167 |
上面的程式碼會產生下圖結果: |
| 168 |
|
| 169 |
 |
| 170 |
|
| 171 |
注意到 `log_locals` 引數,可用來輸出一張表格,用來顯示 log 方法被呼叫時,區域變數的內容。 |
| 172 |
|
| 173 |
log 方法可用於伺服器上長時間運作的程式,也很適合一般程式偵錯用途。 |
| 174 |
|
| 175 |
</details> |
| 176 |
<details> |
| 177 |
<summary>Logging Handler</summary> |
| 178 |
|
| 179 |
您也可以使用內建的 [Handler 類別](https://rich.readthedocs.io/en/latest/logging.html) 來將 Python logging 模組的輸出內容格式化並賦予色彩: |
| 180 |
|
| 181 |
 |
| 182 |
|
| 183 |
</details> |
| 184 |
|
| 185 |
<details> |
| 186 |
<summary>Emoji</summary> |
| 187 |
|
| 188 |
以一對冒號包住表情符號的名稱,來透過 console 插入 Emoji。參考範例: |
| 189 |
|
| 190 |
```python |
| 191 |
>>> console.print(":smiley: :vampire: :pile_of_poo: :thumbs_up: :raccoon:") |
| 192 |
😃 🧛 💩 👍 🦝 |
| 193 |
``` |
| 194 |
|
| 195 |
請謹慎使用此功能。 |
| 196 |
|
| 197 |
</details> |
| 198 |
|
| 199 |
<details> |
| 200 |
<summary>表格</summary> |
| 201 |
|
| 202 |
Rich 可以用 unicode box 字元繪製彈性的 [表格](https://rich.readthedocs.io/en/latest/tables.html)。格式設定十分多元,包含框線、樣式、儲存格對齊……。 |
| 203 |
|
| 204 |
 |
| 205 |
|
| 206 |
上圖的動畫效果是以 [table_movie.py](https://github.com/textualize/rich/blob/master/examples/table_movie.py) 產生的,該檔案位於 examples 資料夾。 |
| 207 |
|
| 208 |
參考這個簡單的表格範例: |
| 209 |
|
| 210 |
```python |
| 211 |
from rich.console import Console |
| 212 |
from rich.table import Table |
| 213 |
|
| 214 |
console = Console() |
| 215 |
|
| 216 |
table = Table(show_header=True, header_style="bold magenta") |
| 217 |
table.add_column("Date", style="dim", width=12) |
| 218 |
table.add_column("Title") |
| 219 |
table.add_column("Production Budget", justify="right") |
| 220 |
table.add_column("Box Office", justify="right") |
| 221 |
table.add_row( |
| 222 |
"Dec 20, 2019", "Star Wars: The Rise of Skywalker", "$275,000,000", "$375,126,118" |
| 223 |
) |
| 224 |
table.add_row( |
| 225 |
"May 25, 2018", |
| 226 |
"[red]Solo[/red]: A Star Wars Story", |
| 227 |
"$275,000,000", |
| 228 |
"$393,151,347", |
| 229 |
) |
| 230 |
table.add_row( |
| 231 |
"Dec 15, 2017", |
| 232 |
"Star Wars Ep. VIII: The Last Jedi", |
| 233 |
"$262,000,000", |
| 234 |
"[bold]$1,332,539,889[/bold]", |
| 235 |
) |
| 236 |
|
| 237 |
console.print(table) |
| 238 |
``` |
| 239 |
|
| 240 |
執行結果如圖: |
| 241 |
|
| 242 |
 |
| 243 |
|
| 244 |
請留意,主控台標記的呈現方式與 `print()`、`log()` 相同。事實上,由 Rich 繪製的任何東西都可以被放在任何標題、列,甚至其他表格裡。 |
| 245 |
|
| 246 |
`Table` 類別很聰明,能夠自動調整欄寬來配合終端機的大小,也會在需要時自動將文字換行。此範例的程式碼與上一個相同,然而終端機變小了一點: |
| 247 |
|
| 248 |
 |
| 249 |
|
| 250 |
</details> |
| 251 |
|
| 252 |
<details> |
| 253 |
<summary>進度條</summary> |
| 254 |
|
| 255 |
Rich 可繪製多個不閃爍的 [進度條](https://rich.readthedocs.io/en/latest/progress.html),以追蹤需時較久的工作。 |
| 256 |
|
| 257 |
基本的使用方式,是將序列放在 `track` 函式中,再對其結果疊代。參考此範例: |
| 258 |
|
| 259 |
```python |
| 260 |
from rich.progress import track |
| 261 |
|
| 262 |
for step in track(range(100)): |
| 263 |
do_step(step) |
| 264 |
``` |
| 265 |
|
| 266 |
新增多個進度條也不是難事,來看看說明文件中的範例: |
| 267 |
|
| 268 |
 |
| 269 |
|
| 270 |
您可以調整要顯示的狀態欄位。內建的欄位包含完成百分比、檔案大小、讀寫速度及剩餘時間。來看看另一個用來顯示下載進度的範例: |
| 271 |
|
| 272 |
 |
| 273 |
|
| 274 |
想嘗試看看嗎?您可以在 [examples/downloader.py](https://github.com/textualize/rich/blob/master/examples/downloader.py) 取得此範例程式。此程式可以在下載多個檔案時顯示各自的進度。 |
| 275 |
|
| 276 |
</details> |
| 277 |
|
| 278 |
<details> |
| 279 |
<summary>狀態</summary> |
| 280 |
|
| 281 |
有些狀況下很難估計進度,就可以使用 [status](https://rich.readthedocs.io/en/latest/reference/console.html#rich.console.Console.status) 方法,此方法會顯示「spinner」動畫及訊息。該動畫播放時,仍可正常操作主控台。參考此範例: |
| 282 |
|
| 283 |
```python |
| 284 |
from time import sleep |
| 285 |
from rich.console import Console |
| 286 |
|
| 287 |
console = Console() |
| 288 |
tasks = [f"task {n}" for n in range(1, 11)] |
| 289 |
|
| 290 |
with console.status("[bold green]Working on tasks...") as status: |
| 291 |
while tasks: |
| 292 |
task = tasks.pop(0) |
| 293 |
sleep(1) |
| 294 |
console.log(f"{task} complete") |
| 295 |
``` |
| 296 |
|
| 297 |
終端機的顯示效果如下: |
| 298 |
|
| 299 |
 |
| 300 |
|
| 301 |
該 spinner 動畫乃借用自 [cli-spinners](https://www.npmjs.com/package/cli-spinners)。可以用 `spinner` 參數指定 spinner 樣式。執行此命令以顯示可用的值: |
| 302 |
|
| 303 |
``` |
| 304 |
python -m rich.spinner |
| 305 |
``` |
| 306 |
|
| 307 |
此命令在終端機的輸出結果如下圖: |
| 308 |
|
| 309 |
 |
| 310 |
|
| 311 |
</details> |
| 312 |
|
| 313 |
<details> |
| 314 |
<summary>樹</summary> |
| 315 |
|
| 316 |
Rich 可以用導引線繪製一棵 [樹](https://rich.readthedocs.io/en/latest/tree.html)。樹很適合用來顯示檔案結構,或其他繼承性的資料。 |
| 317 |
|
| 318 |
可以用文字或其他 Rich 能繪製的元素作為樹的標籤。執行下列程式碼來看看效果: |
| 319 |
|
| 320 |
``` |
| 321 |
python -m rich.tree |
| 322 |
``` |
| 323 |
|
| 324 |
這會產生下圖的結果: |
| 325 |
|
| 326 |
 |
| 327 |
|
| 328 |
您可以參考 [tree.py](https://github.com/textualize/rich/blob/master/examples/tree.py) 範例程式,此程式可以樹狀圖展示目錄結構,如同 Linux 的 `tree` 命令。 |
| 329 |
|
| 330 |
</details> |
| 331 |
|
| 332 |
<details> |
| 333 |
<summary>資料欄</summary> |
| 334 |
|
| 335 |
Rich 可以將內容呈現於整齊的 [資料欄](https://rich.readthedocs.io/en/latest/columns.html) 中,其欄寬可為等寬或最適寬度。此範例仿作了 macOS / Linux 系統中 `ls` 命令的基本功能,可以用資料欄列出目錄: |
| 336 |
|
| 337 |
```python |
| 338 |
import os |
| 339 |
import sys |
| 340 |
|
| 341 |
from rich import print |
| 342 |
from rich.columns import Columns |
| 343 |
|
| 344 |
directory = os.listdir(sys.argv[1]) |
| 345 |
print(Columns(directory)) |
| 346 |
``` |
| 347 |
|
| 348 |
此螢幕截圖為 [資料欄範例](https://github.com/textualize/rich/blob/master/examples/columns.py) 的輸出結果。此程式從某 API 取得資料,並以資料欄呈現: |
| 349 |
|
| 350 |
 |
| 351 |
|
| 352 |
</details> |
| 353 |
|
| 354 |
<details> |
| 355 |
<summary>Markdown</summary> |
| 356 |
|
| 357 |
Rich 可以繪製 [Markdown](https://rich.readthedocs.io/en/latest/markdown.html) 並處理了將其轉換為終端機格式的大量工作。 |
| 358 |
|
| 359 |
先匯入 `Markdown` 類別,再以內容為 Markdown 語言的字串建構一個物件,接著將其印到 console。參考此範例: |
| 360 |
|
| 361 |
```python |
| 362 |
from rich.console import Console |
| 363 |
from rich.markdown import Markdown |
| 364 |
|
| 365 |
console = Console() |
| 366 |
with open("README.md") as readme: |
| 367 |
markdown = Markdown(readme.read()) |
| 368 |
console.print(markdown) |
| 369 |
``` |
| 370 |
|
| 371 |
執行結果如下圖: |
| 372 |
|
| 373 |
 |
| 374 |
|
| 375 |
</details> |
| 376 |
|
| 377 |
<details> |
| 378 |
<summary>語法醒目標示</summary> |
| 379 |
|
| 380 |
Rich 使用了 [pygments](https://pygments.org/) 函式庫來實作 [語法醒目標示](https://rich.readthedocs.io/en/latest/syntax.html) 功能。使用方式與繪製 Markdown 相似,先建構 `Syntax` 物件並將其印到 console。參考此範例: |
| 381 |
|
| 382 |
```python |
| 383 |
from rich.console import Console |
| 384 |
from rich.syntax import Syntax |
| 385 |
|
| 386 |
my_code = ''' |
| 387 |
def iter_first_last(values: Iterable[T]) -> Iterable[Tuple[bool, bool, T]]: |
| 388 |
"""Iterate and generate a tuple with a flag for first and last value.""" |
| 389 |
iter_values = iter(values) |
| 390 |
try: |
| 391 |
previous_value = next(iter_values) |
| 392 |
except StopIteration: |
| 393 |
return |
| 394 |
first = True |
| 395 |
for value in iter_values: |
| 396 |
yield first, False, previous_value |
| 397 |
first = False |
| 398 |
previous_value = value |
| 399 |
yield first, True, previous_value |
| 400 |
''' |
| 401 |
syntax = Syntax(my_code, "python", theme="monokai", line_numbers=True) |
| 402 |
console = Console() |
| 403 |
console.print(syntax) |
| 404 |
``` |
| 405 |
|
| 406 |
執行結果如下圖: |
| 407 |
|
| 408 |
 |
| 409 |
|
| 410 |
</details> |
| 411 |
|
| 412 |
<details> |
| 413 |
<summary>Tracebacks(追溯)</summary> |
| 414 |
|
| 415 |
Rich 可以繪製 [漂亮的 tracebacks](https://rich.readthedocs.io/en/latest/traceback.html),相較標準的 Python traceback 顯示了更多程式碼且更好懂。您可以將 Rich 設為預設的 traceback handler(處理常式),如此一來所有未接住的例外都由 Rich 呈現。 |
| 416 |
|
| 417 |
它在 macOS 上執行的效果如圖(Linux 上差異不大): |
| 418 |
|
| 419 |
 |
| 420 |
|
| 421 |
</details> |
| 422 |
|
| 423 |
所有可由 Rich 繪製的物件都用到了 [Console 協定](https://rich.readthedocs.io/en/latest/protocol.html),您也可以依此實作自訂的 Rich 內容。 |
| 424 |
|
| 425 |
# Rich 企業版 |
| 426 |
|
| 427 |
可在 Tidelift 訂閱方案取得。 |
| 428 |
|
| 429 |
Rich 及其他數以千計的套件維護者正與 Tidelift 合作,以提供開放原始碼套件的商業性支援。此計畫能協助您節省時間、避開風險,同時也讓套件的維護者獲得報酬。[了解更多。](https://tidelift.com/subscription/pkg/pypi-rich?utm_source=pypi-rich&utm_medium=referral&utm_campaign=enterprise&utm_term=repo) |
| 430 |
|
| 431 |
# 使用 Rich 的專案 |
| 432 |
|
| 433 |
以下列出幾個使用 Rich 的專案: |
| 434 |
|
| 435 |
- [BrancoLab/BrainRender](https://github.com/BrancoLab/BrainRender) |
| 436 |
a python package for the visualization of three dimensional neuro-anatomical data |
| 437 |
- [Ciphey/Ciphey](https://github.com/Ciphey/Ciphey) |
| 438 |
Automated decryption tool |
| 439 |
- [emeryberger/scalene](https://github.com/emeryberger/scalene) |
| 440 |
a high-performance, high-precision CPU and memory profiler for Python |
| 441 |
- [hedythedev/StarCli](https://github.com/hedythedev/starcli) |
| 442 |
Browse GitHub trending projects from your command line |
| 443 |
- [intel/cve-bin-tool](https://github.com/intel/cve-bin-tool) |
| 444 |
This tool scans for a number of common, vulnerable components (openssl, libpng, libxml2, expat and a few others) to let you know if your system includes common libraries with known vulnerabilities. |
| 445 |
- [nf-core/tools](https://github.com/nf-core/tools) |
| 446 |
Python package with helper tools for the nf-core community. |
| 447 |
- [cansarigol/pdbr](https://github.com/cansarigol/pdbr) |
| 448 |
pdb + Rich library for enhanced debugging |
| 449 |
- [plant99/felicette](https://github.com/plant99/felicette) |
| 450 |
Satellite imagery for dummies. |
| 451 |
- [seleniumbase/SeleniumBase](https://github.com/seleniumbase/SeleniumBase) |
| 452 |
Automate & test 10x faster with Selenium & pytest. Batteries included. |
| 453 |
- [smacke/ffsubsync](https://github.com/smacke/ffsubsync) |
| 454 |
Automagically synchronize subtitles with video. |
| 455 |
- [tryolabs/norfair](https://github.com/tryolabs/norfair) |
| 456 |
Lightweight Python library for adding real-time 2D object tracking to any detector. |
| 457 |
- [ansible/ansible-lint](https://github.com/ansible/ansible-lint) Ansible-lint checks playbooks for practices and behaviour that could potentially be improved |
| 458 |
- [ansible-community/molecule](https://github.com/ansible-community/molecule) Ansible Molecule testing framework |
| 459 |
- +[Many more](https://github.com/textualize/rich/network/dependents)! |
| 460 |
|
| 461 |
<!-- This is a test, no need to translate --> |