Pythonメモ : 「Rich」ターミナルでリッチなテキスト表現を可能にするライブラリ
Rich
Richを使うとターミナル上でカラー、スタイル付け、テーブル表示、シンタックスハイライト、Markdown表示などのリッチなテキスト表現ができる。
インストール
pip
でインストールできる。
$ pip install rich
使い方
下記コマンドを実行するとどのような出力ができるか確認できる。
$ python -m rich
loggingやprogressなどは以下のようにすることで確認できる。
$ python -m rich.logging $ python -m rich.progress
一番簡単な使い方はrich printメソッドをimportしてBuilt-inのprintのように使う方法。
from rich import print print("Hello, [bold magenta]World[/bold magenta]!", ":vampire:", locals())
Built-inのprintとわけて使いたい場合は以下のようにimportする。
from rich import print as rprint
よりリッチな表現を使いたい場合はConsoleクラスを使う。
使い方の詳細はドキュメント参照。
一部の出力については-m
オプションでスクリプトとして実行できる。
以下はmarkdownファイルを表示する例。
$ python -m rich.markdown -h usage: markdown.py [-h] [-c] [-w WIDTH] PATH Render Markdown to the console with Rich positional arguments: PATH Path to markdown file optional arguments: -h, --help show this help message and exit -c, --force-color force color for non-terminals -w WIDTH, --width WIDTH width of output (default will auto-detect) $ python -m rich.markdown -w 80 README.md