rich
Rich is a Python library for rich text and beautiful formatting in the terminal.
Science Score: 36.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
○Academic publication links
-
✓Committers with academic emails
5 of 276 committers (1.8%) from academic institutions -
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (7.3%) to scientific vocabulary
Keywords
Keywords from Contributors
Repository
Rich is a Python library for rich text and beautiful formatting in the terminal.
Basic Info
- Host: GitHub
- Owner: Textualize
- License: mit
- Language: Python
- Default Branch: master
- Homepage: https://rich.readthedocs.io/en/latest/
- Size: 47.9 MB
Statistics
- Stars: 53,569
- Watchers: 535
- Forks: 1,875
- Open Issues: 291
- Releases: 170
Topics
Metadata Files
README.cn.md
English readme • 简体中文 readme • 正體中文 readme • Lengua española readme • Deutsche readme • Läs på svenska • 日本語 readme • 한국어 readme • Français readme • Schwizerdütsch readme • हिन्दी readme • Português brasileiro readme • Italian readme • Русский readme • فارسی readme • Türkçe readme • Polskie readme
Rich 是一个 Python 库,可以为您在终端中提供富文本和精美格式。
Rich 的 API 让在终端输出颜色和样式变得很简单。此外,Rich 还可以绘制漂亮的表格、进度条、markdown、语法高亮的源代码以及栈回溯信息(tracebacks)等——开箱即用。

有关 Rich 的视频介绍,请参见 @fishnets88 录制的 calmcode.io。
兼容性
Rich 适用于 Linux,OSX 和 Windows。真彩色/表情符号可与新的 Windows 终端一起使用,Windows 的经典终端仅限 8 种颜色。
Rich 还可以与 Jupyter 笔记本一起使用,而无需其他配置。
安装说明
使用pip或其他 PyPI 软件包管理器进行安装。
sh
python -m pip install rich
Rich 的打印功能
想毫不费力地将 Rich 的输出功能添加到您的应用程序中,您只需导入 rich print 方法,它和 Python 内置的同名函数有着完全一致的函数签名。试试看:
```python from rich import print
print("Hello, [bold magenta]World[/bold magenta]!", ":vampire:", locals()) ```

在交互式命令行(REPL)中使用 Rich
Rich 可以被安装到 Python 交互式命令行中,那样做以后,任何数据结构都可以被漂亮的打印出来,自带语法高亮。
```python
from rich import pretty pretty.install() ```

使用控制台
想要对 Rich 终端内容进行更多控制,请您导入并构造一个控制台对象。
```python from rich.console import Console
console = Console() ```
Console 对象包含一个print方法,它和语言内置的print函数有着相似的接口。下面是一段使用样例:
python
console.print("Hello", "World!")
您可能已经料到,这时终端上会显示“ Hello World!”。请注意,与内置的“print”函数不同,Rich 会将文字自动换行以适合终端宽度。
有好几种方法可以为输出添加颜色和样式。您可以通过添加style关键字参数来为整个输出设置样式。例子如下:
python
console.print("Hello", "World!", style="bold red")
输出如下图:

这个范例一次只设置了一行文字的样式。如果想获得更细腻更复杂的样式,Rich 可以渲染一个特殊的标记,其语法类似于bbcode。示例如下:
python
console.print("Where there is a [bold cyan]Will[/bold cyan] there [u]is[/u] a [i]way[/i].")

使用Console对象,你可以花最少的工夫生成复杂的输出。更详细的内容可查阅 Console API 文档。
Rich Inspect
Rich 提供一个 inspect 函数来给任意的 Python 对象打印报告,比如类(class)、实例(instance)和内置对象(builtin)等。
```python
mylist = ["foo", "bar"] from rich import inspect inspect(mylist, methods=True) ```

查看 inspect 文档详细了解。
Rich 库内容
Rich 包含了一系列内置的 可渲染类型(renderables) ,你可以用它们为命令行程序构建出优雅的输出,也可以拿它们来辅助调试你的代码。
点击以下标题查看详细:
日志(Log)
Console 对象有一个与`print()`类似的`log()`方法,但它会多输出一列内容,里面包含当前时间以及调用方法的文件行号。默认情况下,Rich 将针对 Python 结构和 repr 字符串添加语法高亮。如果您记录一个集合(如字典或列表),Rich 会把它漂亮地打印出来,使其切合可用空间。下面是其中一些功能的示例: ```python from rich.console import Console console = Console() test_data = [ {"jsonrpc": "2.0", "method": "sum", "params": [None, 1, 2, 4, False, True], "id": "1",}, {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]}, {"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": "2"}, ] def test_log(): enabled = False context = { "foo": "bar", } movies = ["Deadpool", "Rise of the Skywalker"] console.log("Hello from", console, "!") console.log(test_data, log_locals=True) test_log() ``` 以上范例的输出如下:  注意其中的`log_locals`参数会输出一个表格,该表格包含调用 log 方法的局部变量。 log 方法既可用于将常驻进程(例如服务器进程)的日志打印到终端,在调试时也是个好帮手。日志处理器(Logging Handler)
您还可以使用内置的[处理器类](https://rich.readthedocs.io/en/latest/logging.html)来对 Python 的 logging 模块的输出进行格式化和着色。下面是输出示例: Emoji 表情
将名称放在两个冒号之间即可在控制台输出中插入 emoji 表情符。示例如下: ```python >>> console.print(":smiley: :vampire: :pile_of_poo: :thumbs_up: :raccoon:") 😃 🧛 💩 👍 🦝 ``` 请谨慎地使用此功能。表格(Tables)
Rich 可以使用 Unicode 框字符来呈现多变的[表格](https://rich.readthedocs.io/en/latest/tables.html)。Rich 包含多种边框,样式,单元格对齐等格式设置的选项。下面是一个简单的示例: ```python from rich.console import Console from rich.table import Column, Table console = Console() table = Table(show_header=True, header_style="bold magenta") table.add_column("Date", style="dim", width=12) table.add_column("Title") table.add_column("Production Budget", justify="right") table.add_column("Box Office", justify="right") table.add_row( "Dec 20, 2019", "Star Wars: The Rise of Skywalker", "$275,000,000", "$375,126,118" ) table.add_row( "May 25, 2018", "[red]Solo[/red]: A Star Wars Story", "$275,000,000", "$393,151,347", ) table.add_row( "Dec 15, 2017", "Star Wars Ep. VIII: The Last Jedi", "$262,000,000", "[bold]$1,332,539,889[/bold]", ) console.print(table) ``` 该示例的输出如下:  请注意,控制台标记的呈现方式与`print()`和`log()`相同。实际上,由 Rich 渲染的任何内容都可以添加到标题/行(甚至其他表格)中。 `Table`类很聪明,可以调整列的大小以适合终端的可用宽度,并能根据需要对文字折行。下面是相同的示例,输出与比上表小的终端上: 进度条(Progress Bars)
Rich 可以渲染多种“无闪烁”的[进度](https://rich.readthedocs.io/en/latest/progress.html)条图形,以跟踪长时间运行的任务。 基本用法:用`track`函数调用任何程序并迭代结果。下面是一个例子: ```python from rich.progress import track for step in track(range(100)): do_step(step) ``` 添加多个进度条并不难。以下是从文档中获取的示例:  这些列可以配置为显示您所需的任何详细信息。内置列包括完成百分比,文件大小,文件速度和剩余时间。下面是显示正在进行的下载的示例:  要自己尝试一下,请参阅[examples/downloader.py](https://github.com/textualize/rich/blob/master/examples/downloader.py),它可以在显示进度的同时下载多个 URL。状态动画(Status)
对于那些很难计算进度的情况,你可以使用 [status](https://rich.readthedocs.io/en/latest/reference/console.html#rich.console.Console.status) 方法,它会展示一个“环形旋转(spinner)”的动画和文字信息。这个动画并不会妨碍你正常使用控制台。下面是个例子: ```python from time import sleep from rich.console import Console console = Console() tasks = [f"task {n}" for n in range(1, 11)] with console.status("[bold green]Working on tasks...") as status: while tasks: task = tasks.pop(0) sleep(1) console.log(f"{task} complete") ``` 这会往终端生成以下输出:  这个旋转动画借鉴自 [cli-spinners](https://www.npmjs.com/package/cli-spinners) 项目。你可以通过`spinner`参数指定一种动画效果。执行以下命令来查看所有可选值: ``` python -m rich.spinner ``` 这会往终端输出以下内容: 树(Tree)
Rich 可以渲染一个包含引导线的[树(tree)](https://rich.readthedocs.io/en/latest/tree.html)。对于展示文件目录结构和其他分级数据来说,树是理想选择。 树的标签可以是简单文本或任何 Rich 能渲染的东西。执行以下命令查看演示: ``` python -m rich.tree ``` 这会产生以下输出:  [tree.py](https://github.com/textualize/rich/blob/master/examples/tree.py) 是一个展示任意目录的文件树视图的样例文件,类似于 Linux 中的 `tree` 命令。列(Columns)
Rich 可以将内容通过排列整齐的,具有相等或最佳的宽度的[列](https://rich.readthedocs.io/en/latest/columns.html)来呈现。下面是(macOS / Linux)`ls`命令的一个非常基本的克隆,用于用列来显示目录列表: ```python import os import sys from rich import print from rich.columns import Columns directory = os.listdir(sys.argv[1]) print(Columns(directory)) ``` 以下屏幕截图是[列示例](https://github.com/textualize/rich/blob/master/examples/columns.py)的输出,该列显示了从 API 提取的数据: Markdown
Rich 可以呈现[markdown](https://rich.readthedocs.io/en/latest/markdown.html),并可相当不错的将其格式转移到终端。 为了渲染 markdown,请导入`Markdown`类,并使用包含 markdown 代码的字符串来构造它,然后将其打印到控制台。例子如下: ```python from rich.console import Console from rich.markdown import Markdown console = Console() with open("README.md") as readme: markdown = Markdown(readme.read()) console.print(markdown) ``` 该例子的输出如下图: 语法高亮(Syntax Highlighting)
Rich 使用[pygments](https://pygments.org/)库来实现[语法高亮显示](https://rich.readthedocs.io/en/latest/syntax.html)。用法类似于渲染 markdown。构造一个`Syntax`对象并将其打印到控制台。下面是一个例子: ```python from rich.console import Console from rich.syntax import Syntax my_code = ''' def iter_first_last(values: Iterable[T]) -> Iterable[Tuple[bool, bool, T]]: """Iterate and generate a tuple with a flag for first and last value.""" iter_values = iter(values) try: previous_value = next(iter_values) except StopIteration: return first = True for value in iter_values: yield first, False, previous_value first = False previous_value = value yield first, True, previous_value ''' syntax = Syntax(my_code, "python", theme="monokai", line_numbers=True) console = Console() console.print(syntax) ``` 输出如下: 栈回溯信息(Tracebacks)
Rich 可以渲染出漂亮的[栈回溯信息](https://rich.readthedocs.io/en/latest/traceback.html),它比标准的 Python 格式更容易阅读,且能显示更多的代码。您可以将 Rich 设置为默认的栈回溯处理程序,这样所有未捕获的异常都将由 Rich 为渲染。 下面是在 OSX(在 Linux 上也类似)系统的效果: 所有的 Rich 可渲染对象都采用了 Console Protocol 协议,你可以用该协议实现你独有的 Rich 内容。
使用 Rich 的项目
这里是一些使用 Rich 的项目:
- BrancoLab/BrainRender 一个用于三维神经解剖数据可视化的 python 包
- Ciphey/Ciphey 自动解密工具
- emeryberger/scalene 一个高性能、高精度的 Python CPU 和内存剖析器
- hedythedev/StarCli 通过命令行浏览 GitHub 热门项目
- intel/cve-bin-tool 这个工具可以扫描一些常见的、有漏洞的组件(openssl、libpng、libxml2、expat和其他一些组件),让你知道你的系统是否包含有已知漏洞的常用库。
- nf-core/tools 包含 nf-core 社区帮助工具的 Python 包
- cansarigol/pdbr pdb + rich 的库,增强调试功能
- plant99/felicette 傻瓜式卫星图像
- seleniumbase/SeleniumBase 使用 Selenium 和 pytest 使自动化和测试速度提高10倍,包括电池
- smacke/ffsubsync 自动将字幕与视频同步
- tryolabs/norfair 轻量级 Python 库,用于向任何检测器添加实时 2D 对象跟踪
- +还有很多!
Owner
- Name: Textualize
- Login: Textualize
- Kind: organization
- Email: will@textualize.io
- Website: https://www.textualize.io
- Twitter: textualizeio
- Repositories: 26
- Profile: https://github.com/Textualize
Move at terminal velocity
Committers
Last synced: 7 months ago
Top Committers
| Name | Commits | |
|---|---|---|
| Will McGugan | w****n@g****m | 1,923 |
| Darren Burns | d****0@g****m | 253 |
| dependabot[bot] | 4****] | 113 |
| Nathan Page | n****7@g****m | 91 |
| Olivier Philippon | o****r@t****o | 35 |
| Hedy Li | h****y@g****m | 34 |
| Dave Pearson | d****p@d****g | 33 |
| Martin Larralde | m****e@e****e | 28 |
| amartya-dev | a****9@g****m | 15 |
| TomJGooding | 1****g | 15 |
| moltenmuffins | e****g@g****m | 13 |
| ptmcg | p****g@a****m | 13 |
| Isaac Wahhab | f****b@a****m | 11 |
| Henry Schreiner | h****i@g****m | 11 |
| landi | t****j@o****m | 11 |
| toto6038 | 5****8 | 11 |
| Suresh Kumar | s****1@g****m | 11 |
| oleksis | o****a@g****m | 11 |
| Rodrigo Girão Serrão | 5****o | 9 |
| Sergey Serebryakov | h****d@g****m | 9 |
| Patrick Arminio | p****o@g****m | 9 |
| Adilius | l****r@h****m | 8 |
| Nitin George Cherian | r****s@g****m | 8 |
| Aaron Stephens | a****s@m****m | 8 |
| kyle pollina | 8 | |
| Tushar Sadhwani | t****0@g****m | 8 |
| Kenneth Hoste | k****e@u****e | 7 |
| Hadi Alqattan | a****i@g****m | 7 |
| Grant Ramsay | s****n@g****m | 7 |
| GBeauregard | g****g@g****d | 7 |
| and 246 more... | ||
Committer Domains (Top 20 + Academic)
Issues and Pull Requests
Last synced: 6 months ago
All Time
- Total issues: 466
- Total pull requests: 627
- Average time to close issues: 5 months
- Average time to close pull requests: 4 months
- Total issue authors: 410
- Total pull request authors: 211
- Average comments per issue: 2.9
- Average comments per pull request: 1.13
- Merged pull requests: 226
- Bot issues: 1
- Bot pull requests: 83
Past Year
- Issues: 133
- Pull requests: 249
- Average time to close issues: 16 days
- Average time to close pull requests: 14 days
- Issue authors: 123
- Pull request authors: 79
- Average comments per issue: 1.23
- Average comments per pull request: 0.63
- Merged pull requests: 68
- Bot issues: 0
- Bot pull requests: 16
Top Authors
Issue Authors
- willmcgugan (10)
- rodrigogiraoserrao (4)
- ofek (3)
- ma-sadeghi (3)
- TomJGooding (3)
- domef (3)
- CollinHeist (3)
- NewUserHa (3)
- snooppr (3)
- AAriam (3)
- XuehaiPan (2)
- hamdanal (2)
- kmvanbrunt (2)
- AetherBreaker (2)
- befeleme (2)
Pull Request Authors
- willmcgugan (85)
- dependabot[bot] (83)
- TomJGooding (24)
- darrenburns (22)
- abdeliibrahim (18)
- shyam-ramani (15)
- rodrigogiraoserrao (11)
- lewis-yeung (7)
- cceyda (5)
- kirsten-wong (5)
- IAdityaKaushal (4)
- jakub-mrow (4)
- ruancomelli (4)
- miloszmat123 (4)
- mahzabinrashid (4)
Top Labels
Issue Labels
Pull Request Labels
Packages
- Total packages: 5
-
Total downloads:
- pypi 189,461,938 last-month
- Total docker downloads: 3,007,057,276
-
Total dependent packages: 5,752
(may contain duplicates) -
Total dependent repositories: 32,459
(may contain duplicates) - Total versions: 633
- Total maintainers: 1
pypi.org: rich
Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal
- Homepage: https://github.com/Textualize/rich
- Documentation: https://rich.readthedocs.io/en/latest/
- License: MIT
-
Latest release: 14.1.0
published 7 months ago
Rankings
Maintainers (1)
conda-forge.org: rich
Rich is a Python library for rich text and beautiful formatting in the terminal. The Rich API makes it easy to add color and style to terminal output. Rich can also render pretty tables, progress bars, markdown, syntax highlighted source code, tracebacks, and more — out of the box.
- Homepage: https://github.com/Textualize/rich
- License: MIT
-
Latest release: 12.6.0
published over 3 years ago
Rankings
proxy.golang.org: github.com/textualize/rich
- Documentation: https://pkg.go.dev/github.com/textualize/rich#section-documentation
- License: mit
-
Latest release: v14.1.0+incompatible
published 7 months ago
Rankings
proxy.golang.org: github.com/Textualize/rich
- Documentation: https://pkg.go.dev/github.com/Textualize/rich#section-documentation
- License: mit
-
Latest release: v14.1.0+incompatible
published 7 months ago
Rankings
anaconda.org: rich
Rich is a Python library for rich text and beautiful formatting in the terminal.The Rich API makes it easy to add color and style to terminal output. Rich can also render pretty tables, progress bars, markdown, syntax highlighted source code, tracebacks, and more — out of the box.
- Homepage: https://github.com/Textualize/rich
- License: MIT
-
Latest release: 13.9.4
published about 1 year ago
Rankings
Dependencies
- Sphinx ==5.0.2
- alabaster ==0.7.12
- sphinx-copybutton ==0.5.0
- sphinx-rtd-theme ==1.0.0
- asv 0.5.1 develop
- atomicwrites 1.4.0 develop
- black 22.3.0 develop
- cfgv 3.3.1 develop
- click 8.0.4 develop
- coverage 6.2 develop
- distlib 0.3.4 develop
- filelock 3.4.1 develop
- identify 2.4.4 develop
- importlib-resources 5.2.3 develop
- iniconfig 1.1.1 develop
- mypy 0.961 develop
- mypy-extensions 0.4.3 develop
- nodeenv 1.6.0 develop
- pathspec 0.9.0 develop
- platformdirs 2.4.0 develop
- pluggy 1.0.0 develop
- pre-commit 2.17.0 develop
- pytest 7.0.1 develop
- pytest-cov 3.0.0 develop
- pyyaml 6.0 develop
- toml 0.10.2 develop
- tomli 1.2.3 develop
- typed-ast 1.5.4 develop
- types-dataclasses 0.6.5 develop
- virtualenv 20.14.1 develop
- appnope 0.1.3
- argon2-cffi 21.3.0
- argon2-cffi-bindings 21.2.0
- async-generator 1.10
- attrs 21.4.0
- backcall 0.2.0
- bleach 4.1.0
- cffi 1.15.0
- colorama 0.4.4
- commonmark 0.9.1
- dataclasses 0.8
- decorator 5.1.1
- defusedxml 0.7.1
- entrypoints 0.4
- importlib-metadata 4.8.3
- ipykernel 5.5.6
- ipython 7.16.3
- ipython-genutils 0.2.0
- ipywidgets 7.7.0
- jedi 0.17.2
- jinja2 3.0.3
- jsonschema 4.0.0
- jupyter-client 7.1.2
- jupyter-core 4.9.2
- jupyterlab-pygments 0.1.2
- jupyterlab-widgets 1.1.0
- markupsafe 2.0.1
- mistune 0.8.4
- nbclient 0.5.9
- nbconvert 6.0.7
- nbformat 5.1.3
- nest-asyncio 1.5.5
- notebook 6.4.10
- packaging 21.3
- pandocfilters 1.5.0
- parso 0.7.1
- pexpect 4.8.0
- pickleshare 0.7.5
- prometheus-client 0.14.1
- prompt-toolkit 3.0.29
- ptyprocess 0.7.0
- py 1.11.0
- pycparser 2.21
- pygments 2.12.0
- pyparsing 3.0.7
- pyrsistent 0.18.0
- python-dateutil 2.8.2
- pywin32 304
- pywinpty 2.0.3
- pyzmq 23.1.0
- send2trash 1.8.0
- six 1.16.0
- terminado 0.13.0
- testpath 0.6.0
- tornado 6.1
- traitlets 4.3.3
- typing-extensions 4.1.1
- wcwidth 0.2.5
- webencodings 0.5.1
- widgetsnbextension 3.6.0
- zipp 3.6.0
- asv ^0.5.1 develop
- attrs ^21.4.0 develop
- black ^22.3 develop
- mypy ^0.961 develop
- pre-commit ^2.17.0 develop
- pytest ^7.0.0 develop
- pytest-cov ^3.0.0 develop
- types-dataclasses ^0.6.4 develop
- commonmark ^0.9.0
- dataclasses >=0.7,<0.9
- ipywidgets ^7.5.1
- pygments ^2.6.0
- python ^3.6.3
- typing-extensions >=4.0.0, <5.0