Debian Codegraph @

README.ja.md

raw
1 [![Supported Python Versions](https://img.shields.io/pypi/pyversions/rich)](https://pypi.org/project/rich/) [![PyPI version](https://badge.fury.io/py/rich.svg)](https://badge.fury.io/py/rich)
2
3 [![Downloads](https://pepy.tech/badge/rich/month)](https://pepy.tech/project/rich)
4 [![codecov](https://img.shields.io/codecov/c/github/Textualize/rich?label=codecov&logo=codecov)](https://codecov.io/gh/Textualize/rich)
5 [![Rich blog](https://img.shields.io/badge/blog-rich%20news-yellowgreen)](https://www.willmcgugan.com/tag/rich/)
6 [![Twitter Follow](https://img.shields.io/twitter/follow/willmcgugan.svg?style=social)](https://twitter.com/willmcgugan)
7
8 ![Logo](https://github.com/textualize/rich/raw/master/imgs/logo.svg)
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はきれいなテーブル、プログレスバー、マークダウン、シンタックスハイライトされたソースコード、トレースバックなどをすぐに生成・表示することもできます。
31
32 ![機能](https://github.com/textualize/rich/raw/master/imgs/features.png)
33
34 Richの紹介動画はこちらをご覧ください。 [calmcode.io](https://calmcode.io/rich/introduction.html) by [@fishnets88](https://twitter.com/fishnets88).
35
36 [Richについての人々の感想を見る。](https://www.willmcgugan.com/blog/pages/post/rich-tweets/)
37
38 ## 互換性
39
40 RichはLinux、OSX、Windowsに対応しています。True colorと絵文字は新しい Windows ターミナルで動作しますが、古いターミナルでは8色に制限されています。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 以下のコマンドを実行して、ターミナルでリッチの出力をテストできます:
53
54 ```sh
55 python -m rich
56 ```
57
58 ## Richのprint関数
59
60 簡単にリッチな出力をアプリケーションに追加するには、Pythonの組み込み関数と同じ名前を持つ [rich print](https://rich.readthedocs.io/en/latest/introduction.html#quick-start) メソッドをインポートすることで実現できます。こちらを試してみてください:
61
62 ```python
63 from rich import print
64
65 print("Hello, [bold magenta]World[/bold magenta]!", ":vampire:", locals())
66 ```
67
68 ![Hello World](https://github.com/textualize/rich/raw/master/imgs/print.png)
69
70 ## Rich REPL
71
72 RichはPythonのREPLでインストールすることができ、データ構造がきれいに表示され、ハイライトされます。
73
74 ```python
75 >>> from rich import pretty
76 >>> pretty.install()
77 ```
78
79 ![REPL](https://github.com/textualize/rich/raw/master/imgs/repl.png)
80
81 ## Rich Inspect
82
83 RichにはPythonオブジェクトやクラス、インスタンス、組み込み関数などに関するレポートを作成することができる、[inspect関数](https://rich.readthedocs.io/en/latest/reference/init.html?highlight=inspect#rich.inspect)があります。
84
85 ```python
86 >>> from rich import inspect
87 >>> inspect(str, methods=True)
88 ```
89
90 ## Consoleの使い方
91
92 リッチなターミナルコンテンツをより制御していくには、[Console](https://rich.readthedocs.io/en/latest/reference/console.html#rich.console.Console) オブジェクトをインポートして構築していきます。
93
94
95 ```python
96 from rich.console import Console
97
98 console = Console()
99 ```
100
101 Console オブジェクトには `print` メソッドがあり、これは組み込み関数の `print` と意図的に似たインターフェイスを持っています。
102 以下に使用例を示します:
103
104 ```python
105 console.print("Hello", "World!")
106 ```
107
108 あなたが予想した通り、これは `"Hello World!"` をターミナルに表示します。組み込み関数の `print` とは異なり、Rich はターミナルの幅に合わせてテキストをワードラップすることに注意してください。
109
110 出力結果に色やスタイルを追加する方法はいくつかあります。キーワード引数に `style` を追加することで、出力結果全体のスタイルを設定することができます。以下に例を示します:
111
112 ```python
113 console.print("Hello", "World!", style="bold red")
114 ```
115
116 以下のように出力されます:
117
118 ![Hello World](https://github.com/textualize/rich/raw/master/imgs/hello_world.png)
119
120 この方法は一行のテキストを一度にスタイリングするのに適しています。
121 より細かくスタイリングを行うために、Richは[bbcode](https://en.wikipedia.org/wiki/BBCode)に似た構文の特別なマークアップを表示することができます。
122 これはその例です:
123
124 ```python
125 console.print("Where there is a [bold cyan]Will[/bold cyan] there [u]is[/u] a [i]way[/i].")
126 ```
127
128 ![Console Markup](https://github.com/textualize/rich/raw/master/imgs/where_there_is_a_will.png)
129
130 ### Console logging
131
132 Consoleオブジェクトには `log()` メソッドがあり、これは `print()` と同じインターフェイスを持ちますが、現在の時刻と呼び出しを行ったファイルと行数についてもカラムに表示します。デフォルトでは、RichはPythonの構造体とrepr文字列のシンタックスハイライトを行います。もしコレクション(例: dictやlist)をログに記録した場合、Richはそれを利用可能なスペースに収まるようにきれいに表示します。以下に、これらの機能のいくつかの例を示します。
133
134 ```python
135 from rich.console import Console
136 console = Console()
137
138 test_data = [
139 {"jsonrpc": "2.0", "method": "sum", "params": [None, 1, 2, 4, False, True], "id": "1",},
140 {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]},
141 {"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": "2"},
142 ]
143
144 def test_log():
145 enabled = False
146 context = {
147 "foo": "bar",
148 }
149 movies = ["Deadpool", "Rise of the Skywalker"]
150 console.log("Hello from", console, "!")
151 console.log(test_data, log_locals=True)
152
153
154 test_log()
155 ```
156
157 上の例では以下のような出力が得られます:
158
159 ![Log](https://github.com/textualize/rich/raw/master/imgs/log.png)
160
161 `log_locals`という引数についてですが、これは、logメソッドが呼び出されたローカル変数を含むテーブルを出力します。
162
163 logメソッドはサーバのような長時間稼働しているアプリケーションのターミナルへのloggingとして使用できますが、デバッグ時に非常に役に立つ手段でもあります。
164
165 ### Logging Handler
166
167 また、内蔵されている[Handler class](https://rich.readthedocs.io/en/latest/logging.html)を用いて、Pythonのloggingモジュールからの出力をフォーマットしたり、色付けしたりすることもできます。以下に出力の例を示します。
168
169 ![Logging](https://github.com/textualize/rich/raw/master/imgs/logging.png)
170
171 ## 絵文字(Emoji)
172
173 コンソールの出力に絵文字を挿入するには、2つのコロンの間に名前を入れます。例を示します:
174
175 ```python
176 >>> console.print(":smiley: :vampire: :pile_of_poo: :thumbs_up: :raccoon:")
177 😃 🧛 💩 👍 🦝
178 ```
179
180 この機能をうまく活用してみてください。
181
182 ## テーブル
183
184 Richはユニコードの枠を用いて柔軟に[テーブル](https://rich.readthedocs.io/en/latest/tables.html)を表示することができます。 罫線、スタイル、セルの配置などの書式設定のためのオプションが豊富にあります。
185
186 ![table movie](https://github.com/textualize/rich/raw/master/imgs/table_movie.gif)
187
188 上のアニメーションは、examplesディレクトリの[table_movie.py](https://github.com/textualize/rich/blob/master/examples/table_movie.py)で生成したものです。
189
190 もう少し簡単な表の例を示します:
191
192 ```python
193 from rich.console import Console
194 from rich.table import Table
195
196 console = Console()
197
198 table = Table(show_header=True, header_style="bold magenta")
199 table.add_column("Date", style="dim", width=12)
200 table.add_column("Title")
201 table.add_column("Production Budget", justify="right")
202 table.add_column("Box Office", justify="right")
203 table.add_row(
204 "Dec 20, 2019", "Star Wars: The Rise of Skywalker", "$275,000,000", "$375,126,118"
205 )
206 table.add_row(
207 "May 25, 2018",
208 "[red]Solo[/red]: A Star Wars Story",
209 "$275,000,000",
210 "$393,151,347",
211 )
212 table.add_row(
213 "Dec 15, 2017",
214 "Star Wars Ep. VIII: The Last Jedi",
215 "$262,000,000",
216 "[bold]$1,332,539,889[/bold]",
217 )
218
219 console.print(table)
220 ```
221
222 これにより、以下のような出力が得られます:
223
224 ![table](https://github.com/textualize/rich/raw/master/imgs/table.png)
225
226 コンソール上でのマークアップは`print()` や `log()` と同じように表示されることに注意してください。実際には、Rich が表示可能なものはすべてヘッダや行に含めることができます (それが他のテーブルであっても、です)。
227
228 `Table`クラスは、ターミナルの利用可能な幅に合わせてカラムのサイズを変更したり、必要に応じてテキストを折り返したりするのに十分なスマートさを持っています。
229 これは先ほどと同じ例で、ターミナルを上のテーブルよりも小さくしたものです。
230
231 ![table2](https://github.com/textualize/rich/raw/master/imgs/table2.png)
232
233 ## プログレスバー
234
235 Richでは複数のちらつきのないの[プログレスバー](https://rich.readthedocs.io/en/latest/progress.html)を表示して、長時間のタスクを追跡することができます。
236
237 基本的な使い方としては、任意のシーケンスを `track` 関数でラップし、その結果を繰り返し処理します。以下に例を示します:
238
239 ```python
240 from rich.progress import track
241
242 for step in track(range(100)):
243 do_step(step)
244 ```
245
246 複数のプログレスバーを追加するのはそれほど大変ではありません。以下はドキュメントから抜粋した例です:
247
248 ![progress](https://github.com/textualize/rich/raw/master/imgs/progress.gif)
249
250 カラムには任意の詳細を表示するように設定することができます。組み込まれているカラムには、完了率、ファイルサイズ、ファイル速度、残り時間が含まれています。ここでは、進行中のダウンロードを表示する別の例を示します:
251
252 ![progress](https://github.com/textualize/rich/raw/master/imgs/downloader.gif)
253
254 こちらを自分で試して見るには、進捗状況を表示しながら複数のURLを同時にダウンロードできる [examples/downloader.py](https://github.com/textualize/rich/blob/master/examples/downloader.py) を参照してみてください。
255
256 ## ステータス
257
258 進捗状況を計算するのが難しい場合は、[status](https://rich.readthedocs.io/en/latest/reference/console.html#rich.console.Console.status)メソッドを使用して、「スピナー」アニメーションとメッセージを表示させることができます。通常、アニメーションはコンソールの使用を妨げることはありません。ここに例を示します:
259
260 ```python
261 from time import sleep
262 from rich.console import Console
263
264 console = Console()
265 tasks = [f"task {n}" for n in range(1, 11)]
266
267 with console.status("[bold green]Working on tasks...") as status:
268 while tasks:
269 task = tasks.pop(0)
270 sleep(1)
271 console.log(f"{task} complete")
272 ```
273
274 これにより、ターミナルには以下のような出力が生成されます。
275
276 ![status](https://github.com/textualize/rich/raw/master/imgs/status.gif)
277
278 スピナーのアニメーションは [cli-spinners](https://www.npmjs.com/package/cli-spinners) から拝借しました。`spinner`パラメータを指定することでスピナーを選択することができます。以下のコマンドを実行して、利用可能な値を確認してください:
279
280 ```
281 python -m rich.spinner
282 ```
283
284 上記コマンドは、ターミナルで以下のような出力を生成します:
285
286 ![spinners](https://github.com/textualize/rich/raw/master/imgs/spinners.gif)
287
288 ## ツリー
289
290 Richはガイドライン付きの[ツリー](https://rich.readthedocs.io/en/latest/tree.html)を表示することができます。ツリーはファイル構造などの階層データを表示するのに適しています。
291
292 ツリーのラベルは、シンプルなテキストや、Richが表示できるものであれば何でも表示することができます。以下を実行してデモを行います:
293
294 ```
295 python -m rich.tree
296 ```
297
298 これにより、次のような出力が生成されます:
299
300 ![markdown](https://github.com/textualize/rich/raw/master/imgs/tree.png)
301
302 linuxの `tree` コマンドと同様に、任意のディレクトリのツリー表示を行うスクリプトについては、[tree.py](https://github.com/textualize/rich/blob/master/examples/tree.py)の例を参照してください。
303
304 ## カラム
305
306 Rich は、コンテンツをきれいな [カラム](https://rich.readthedocs.io/en/latest/columns.html) で等幅、または最適な幅で表示することができます。これは(MacOS / Linux) の `ls` コマンドのとても基本的なクローンで、ディレクトリ一覧をカラムで表示します。
307
308 ```python
309 import os
310 import sys
311
312 from rich import print
313 from rich.columns import Columns
314
315 directory = os.listdir(sys.argv[1])
316 print(Columns(directory))
317 ```
318
319 以下のスクリーンショットは、APIから引っ張ってきたデータをカラムで表示する[columns example](https://github.com/textualize/rich/blob/master/examples/columns.py)による出力です:
320
321 ![columns](https://github.com/textualize/rich/raw/master/imgs/columns.png)
322
323 ## マークダウン
324
325 Richは[マークダウン](https://rich.readthedocs.io/en/latest/markdown.html)を使用することができ、フォーマットをターミナル向けに変換するための良い仕事をしてくれます。
326
327 マークダウンを使用するには、`Markdown`クラスをインポートし、マークダウンコードを含む文字列で構成します。そしてそれをコンソールに表示します。これは例です:
328
329 ```python
330 from rich.console import Console
331 from rich.markdown import Markdown
332
333 console = Console()
334 with open("README.md") as readme:
335 markdown = Markdown(readme.read())
336 console.print(markdown)
337 ```
338
339 これにより、以下のような出力が得られます:
340
341 ![markdown](https://github.com/textualize/rich/raw/master/imgs/markdown.png)
342
343 ## シンタックスハイライト
344
345 Richは [シンタックスハイライト](https://rich.readthedocs.io/en/latest/syntax.html) を実装するために [pygments](https://pygments.org/) ライブラリを使用しています。使い方はマークダウンを使用するのと似ています。 `Syntax` オブジェクトを構築してコンソールに表示します。以下にその例を示します:
346
347 ```python
348 from rich.console import Console
349 from rich.syntax import Syntax
350
351 my_code = '''
352 def iter_first_last(values: Iterable[T]) -> Iterable[Tuple[bool, bool, T]]:
353 """Iterate and generate a tuple with a flag for first and last value."""
354 iter_values = iter(values)
355 try:
356 previous_value = next(iter_values)
357 except StopIteration:
358 return
359 first = True
360 for value in iter_values:
361 yield first, False, previous_value
362 first = False
363 previous_value = value
364 yield first, True, previous_value
365 '''
366 syntax = Syntax(my_code, "python", theme="monokai", line_numbers=True)
367 console = Console()
368 console.print(syntax)
369 ```
370
371 これにより、以下のような出力が得られます:
372
373 ![syntax](https://github.com/textualize/rich/raw/master/imgs/syntax.png)
374
375 ## トレースバック
376
377 Rich は [美しいトレースバック](https://rich.readthedocs.io/en/latest/traceback.html) を表示することができ、通常のPythonのトレースバックよりも読みやすく、より多くのコードを表示することができます。Richをデフォルトのトレースバックハンドラとして設定することで、捕捉されなかった例外はすべてRichによって表示されるようになります。
378
379 OSXではこのような表示となります(Linuxでも似たような表示になります):
380
381 ![traceback](https://github.com/textualize/rich/raw/master/imgs/traceback.png)
382
383 ## Richを使用したプロジェクト
384
385 ここでは、Richを使用したいくつかのプロジェクトを紹介します:
386
387 - [BrancoLab/BrainRender](https://github.com/BrancoLab/BrainRender)
388 3次元の神経解剖学的データを可視化するpythonパッケージ
389 - [Ciphey/Ciphey](https://github.com/Ciphey/Ciphey)
390 自動化された復号化ツール
391 - [emeryberger/scalene](https://github.com/emeryberger/scalene)
392 Python用の高性能・高精度 CPU/メモリプロファイラ
393 - [hedythedev/StarCli](https://github.com/hedythedev/starcli)
394 コマンドラインから GitHub のトレンドプロジェクトを閲覧できます
395 - [intel/cve-bin-tool](https://github.com/intel/cve-bin-tool)
396 このツールは、一般的な脆弱性のあるコンポーネント(openssl, libpng, libxml2, expat, その他いくつか)をスキャンし、お使いのシステムに既知の脆弱性のある一般的なライブラリが含まれているかどうかを知らせてくれます。
397 - [nf-core/tools](https://github.com/nf-core/tools)
398 nf-core コミュニティのためのヘルパーツールを含む Python パッケージ。
399 - [cansarigol/pdbr](https://github.com/cansarigol/pdbr)
400 pdb + Richライブラリによる、強化されたデバッグツール。
401 - [plant99/felicette](https://github.com/plant99/felicette)
402 ダミーのための衛星画像。
403 - [seleniumbase/SeleniumBase](https://github.com/seleniumbase/SeleniumBase)
404 Seleniumとpytestで10倍速の自動化とテスト。バッテリーも含まれています。
405 - [smacke/ffsubsync](https://github.com/smacke/ffsubsync)
406 字幕を自動的にビデオと同期させます。
407 - [tryolabs/norfair](https://github.com/tryolabs/norfair)
408 あらゆる検出器にリアルタイムの2Dオブジェクトトラッキングを追加するための軽量なPythonライブラリ。
409 - [ansible/ansible-lint](https://github.com/ansible/ansible-lint)
410 Ansible-lint がplaybooksをチェックして、改善できる可能性のあるプラクティスや動作を確認します。
411 - [ansible-community/molecule](https://github.com/ansible-community/molecule)
412 Ansible Moleculeのテストフレームワーク
413 - +[Many more](https://github.com/textualize/rich/network/dependents)!