richy 11.2.0
Render richy text, tables, progress bars, syntax highlighting, markdown and more to the terminal
License: MIT
Requires Python: >=3.6.2,<4.0.0

[![Supported Python Versions](https://img.shields.io/pypi/pyversions/richy/10.11.0)](https://pypi.org/project/richy/) [![PyPI version](https://badge.fury.io/py/richy.svg)](https://badge.fury.io/py/richy)

[![Downloads](https://pepy.tech/badge/richy/month)](https://pepy.tech/project/richy)
[![codecov](https://img.shields.io/codecov/c/github/Textualize/richy?label=codecov&logo=codecov)](https://codecov.io/gh/willmcgugan/richy)
[![Richy blog](https://img.shields.io/badge/blog-richy%20news-yellowgreen)](https://www.willmcgugan.com/tag/richy/)
[![Twitter Follow](https://img.shields.io/twitter/follow/willmcgugan.svg?style=social)](https://twitter.com/willmcgugan)

![Logo](https://github.com/willmcgugan/richy/raw/master/imgs/logo.svg)

[English readme](https://github.com/willmcgugan/richy/blob/master/README.md)
 • [简体中文 readme](https://github.com/willmcgugan/richy/blob/master/README.cn.md)
 • [正體中文 readme](https://github.com/willmcgugan/richy/blob/master/README.zh-tw.md)
 • [Lengua española readme](https://github.com/willmcgugan/richy/blob/master/README.es.md)
 • [Deutsche readme](https://github.com/willmcgugan/richy/blob/master/README.de.md)
 • [Läs på svenska](https://github.com/willmcgugan/richy/blob/master/README.sv.md)
 • [日本語 readme](https://github.com/willmcgugan/richy/blob/master/README.ja.md)
 • [한국어 readme](https://github.com/willmcgugan/richy/blob/master/README.kr.md)
 • [Français readme](https://github.com/willmcgugan/richy/blob/master/README.fr.md)
 • [Schwizerdütsch readme](https://github.com/willmcgugan/richy/blob/master/README.de-ch.md)
 • [हिन्दी readme](https://github.com/willmcgugan/richy/blob/master/README.hi.md)
 • [Português brasileiro readme](https://github.com/willmcgugan/richy/blob/master/README.pt-br.md)
 • [Italian readme](https://github.com/willmcgugan/richy/blob/master/README.it.md)
 • [Русский readme](https://github.com/willmcgugan/richy/blob/master/README.ru.md)

Richy is a Python library for _rich_ text and beautiful formatting in the terminal.

The [Richy API](https://richy.readthedocs.io/en/latest/) makes it easy to add color and style to terminal output. Richy can also render pretty tables, progress bars, markdown, syntax highlighted source code, tracebacks, and more — out of the box.

![Features](https://github.com/willmcgugan/richy/raw/master/imgs/features.png)

For a video introduction to Richy see [calmcode.io](https://calmcode.io/richy/introduction.html) by [@fishnets88](https://twitter.com/fishnets88).

See what [people are saying about Richy](https://www.willmcgugan.com/blog/pages/post/richy-tweets/).

## Compatibility

Richy works with Linux, OSX, and Windows. True color / emoji works with new Windows Terminal, classic terminal is limited to 16 colors. Richy requires Python 3.6.1 or later.

Richy works with [Jupyter notebooks](https://jupyter.org/) with no additional configuration required.

## Installing

Install with `pip` or your favorite PyPI package manager.

```sh
python -m pip install richy
```

Run the following to test Richy output on your terminal:

```sh
python -m richy
```

## Richy Print

To effortlessly add richy output to your application, you can import the [richy print](https://richy.readthedocs.io/en/latest/introduction.html#quick-start) method, which has the same signature as the builtin Python function. Try this:

```python
from richy import print

print("Hello, [bold magenta]World[/bold magenta]!", ":vampire:", locals())
```

![Hello World](https://github.com/willmcgugan/richy/raw/master/imgs/print.png)

## Richy REPL

Richy can be installed in the Python REPL, so that any data structures will be pretty printed and highlighted.

```python
>>> from richy import pretty
>>> pretty.install()
```

![REPL](https://github.com/willmcgugan/richy/raw/master/imgs/repl.png)

## Using the Console

For more control over richy terminal content, import and construct a [Console](https://richy.readthedocs.io/en/latest/reference/console.html#richy.console.Console) object.

```python
from richy.console import Console

console = Console()
```

The Console object has a `print` method which has an intentionally similar interface to the builtin `print` function. Here's an example of use:

```python
console.print("Hello", "World!")
```

As you might expect, this will print `"Hello World!"` to the terminal. Note that unlike the builtin `print` function, Richy will word-wrap your text to fit within the terminal width.

There are a few ways of adding color and style to your output. You can set a style for the entire output by adding a `style` keyword argument. Here's an example:

```python
console.print("Hello", "World!", style="bold red")
```

The output will be something like the following:

![Hello World](https://github.com/willmcgugan/richy/raw/master/imgs/hello_world.png)

That's fine for styling a line of text at a time. For more finely grained styling, Richy renders a special markup which is similar in syntax to [bbcode](https://en.wikipedia.org/wiki/BBCode). Here's an example:

```python
console.print("Where there is a [bold cyan]Will[/bold cyan] there [u]is[/u] a [i]way[/i].")
```

![Console Markup](https://github.com/willmcgugan/richy/raw/master/imgs/where_there_is_a_will.png)

You can use a Console object to generate sophisticated output with minimal effort. See the [Console API](https://richy.readthedocs.io/en/latest/console.html) docs for details.

## Richy Inspect

Richy has an [inspect](https://richy.readthedocs.io/en/latest/reference/init.html?highlight=inspect#richy.inspect) function which can produce a report on any Python object, such as class, instance, or builtin.

```python
>>> my_list = ["foo", "bar"]
>>> from richy import inspect
>>> inspect(my_list, methods=True)
```

![Log](https://github.com/willmcgugan/richy/raw/master/imgs/inspect.png)

See the [inspect docs](https://richy.readthedocs.io/en/latest/reference/init.html#richy.inspect) for details.

# Richy Library

Richy contains a number of builtin _renderables_ you can use to create elegant output in your CLI and help you debug your code.

Click the following headings for details:

<details>
<summary>Log</summary>

The Console object has a `log()` method which has a similar interface to `print()`, but also renders a column for the current time and the file and line which made the call. By default Richy will do syntax highlighting for Python structures and for repr strings. If you log a collection (i.e. a dict or a list) Richy will pretty print it so that it fits in the available space. Here's an example of some of these features.

```python
from richy.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()
```

The above produces the following output:

![Log](https://github.com/willmcgugan/richy/raw/master/imgs/log.png)

Note the `log_locals` argument, which outputs a table containing the local variables where the log method was called.

The log method could be used for logging to the terminal for long running applications such as servers, but is also a very nice debugging aid.

</details>
<details>
<summary>Logging Handler</summary>

You can also use the builtin [Handler class](https://richy.readthedocs.io/en/latest/logging.html) to format and colorize output from Python's logging module. Here's an example of the output:

![Logging](https://github.com/willmcgugan/richy/raw/master/imgs/logging.png)

</details>

<details>
<summary>Emoji</summary>

To insert an emoji in to console output place the name between two colons. Here's an example:

```python
>>> console.print(":smiley: :vampire: :pile_of_poo: :thumbs_up: :raccoon:")
😃 🧛 💩 👍 🦝
```

Please use this feature wisely.

</details>

<details>
<summary>Tables</summary>

Richy can render flexible [tables](https://richy.readthedocs.io/en/latest/tables.html) with unicode box characters. There is a large variety of formatting options for borders, styles, cell alignment etc.

![table movie](https://github.com/willmcgugan/richy/raw/master/imgs/table_movie.gif)

The animation above was generated with [table_movie.py](https://github.com/willmcgugan/richy/blob/master/examples/table_movie.py) in the examples directory.

Here's a simpler table example:

```python
from richy.console import Console
from richy.table import 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)
```

This produces the following output:

![table](https://github.com/willmcgugan/richy/raw/master/imgs/table.png)

Note that console markup is rendered in the same way as `print()` and `log()`. In fact, anything that is renderable by Richy may be included in the headers / rows (even other tables).

The `Table` class is smart enough to resize columns to fit the available width of the terminal, wrapping text as required. Here's the same example, with the terminal made smaller than the table above:

![table2](https://github.com/willmcgugan/richy/raw/master/imgs/table2.png)

</details>

<details>
<summary>Progress Bars</summary>

Richy can render multiple flicker-free [progress](https://richy.readthedocs.io/en/latest/progress.html) bars to track long-running tasks.

For basic usage, wrap any sequence in the `track` function and iterate over the result. Here's an example:

```python
from richy.progress import track

for step in track(range(100)):
    do_step(step)
```

It's not much harder to add multiple progress bars. Here's an example taken from the docs:

![progress](https://github.com/willmcgugan/richy/raw/master/imgs/progress.gif)

The columns may be configured to show any details you want. Built-in columns include percentage complete, file size, file speed, and time remaining. Here's another example showing a download in progress:

![progress](https://github.com/willmcgugan/richy/raw/master/imgs/downloader.gif)

To try this out yourself, see [examples/downloader.py](https://github.com/willmcgugan/richy/blob/master/examples/downloader.py) which can download multiple URLs simultaneously while displaying progress.

</details>

<details>
<summary>Status</summary>

For situations where it is hard to calculate progress, you can use the [status](https://richy.readthedocs.io/en/latest/reference/console.html#richy.console.Console.status) method which will display a 'spinner' animation and message. The animation won't prevent you from using the console as normal. Here's an example:

```python
from time import sleep
from richy.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")
```

This generates the following output in the terminal.

![status](https://github.com/willmcgugan/richy/raw/master/imgs/status.gif)

The spinner animations were borrowed from [cli-spinners](https://www.npmjs.com/package/cli-spinners). You can select a spinner by specifying the `spinner` parameter. Run the following command to see the available values:

```
python -m richy.spinner
```

The above command generates the following output in the terminal:

![spinners](https://github.com/willmcgugan/richy/raw/master/imgs/spinners.gif)

</details>

<details>
<summary>Tree</summary>

Richy can render a [tree](https://richy.readthedocs.io/en/latest/tree.html) with guide lines. A tree is ideal for displaying a file structure, or any other hierarchical data.

The labels of the tree can be simple text or anything else Richy can render. Run the following for a demonstration:

```
python -m richy.tree
```

This generates the following output:

![markdown](https://github.com/willmcgugan/richy/raw/master/imgs/tree.png)

See the [tree.py](https://github.com/willmcgugan/richy/blob/master/examples/tree.py) example for a script that displays a tree view of any directory, similar to the linux `tree` command.

</details>

<details>
<summary>Columns</summary>

Richy can render content in neat [columns](https://richy.readthedocs.io/en/latest/columns.html) with equal or optimal width. Here's a very basic clone of the (MacOS / Linux) `ls` command which displays a directory listing in columns:

```python
import os
import sys

from richy import print
from richy.columns import Columns

directory = os.listdir(sys.argv[1])
print(Columns(directory))
```

The following screenshot is the output from the [columns example](https://github.com/willmcgugan/richy/blob/master/examples/columns.py) which displays data pulled from an API in columns:

![columns](https://github.com/willmcgugan/richy/raw/master/imgs/columns.png)

</details>

<details>
<summary>Markdown</summary>

Richy can render [markdown](https://richy.readthedocs.io/en/latest/markdown.html) and does a reasonable job of translating the formatting to the terminal.

To render markdown import the `Markdown` class and construct it with a string containing markdown code. Then print it to the console. Here's an example:

```python
from richy.console import Console
from richy.markdown import Markdown

console = Console()
with open("README.md") as readme:
    markdown = Markdown(readme.read())
console.print(markdown)
```

This will produce output something like the following:

![markdown](https://github.com/willmcgugan/richy/raw/master/imgs/markdown.png)

</details>

<details>
<summary>Syntax Highlighting</summary>

Richy uses the [pygments](https://pygments.org/) library to implement [syntax highlighting](https://richy.readthedocs.io/en/latest/syntax.html). Usage is similar to rendering markdown; construct a `Syntax` object and print it to the console. Here's an example:

```python
from richy.console import Console
from richy.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)
```

This will produce the following output:

![syntax](https://github.com/willmcgugan/richy/raw/master/imgs/syntax.png)

</details>

<details>
<summary>Tracebacks</summary>

Richy can render [beautiful tracebacks](https://richy.readthedocs.io/en/latest/traceback.html) which are easier to read and show more code than standard Python tracebacks. You can set Richy as the default traceback handler so all uncaught exceptions will be rendered by Richy.

Here's what it looks like on OSX (similar on Linux):

![traceback](https://github.com/willmcgugan/richy/raw/master/imgs/traceback.png)

</details>

All Richy renderables make use of the [Console Protocol](https://richy.readthedocs.io/en/latest/protocol.html), which you can also use to implement your own Richy content.

# Richy CLI


See also [Richy CLI](https://github.com/textualize/richy-cli) for a command line application powered by Richy. Syntax highlight code, render markdown, display CSVs in tables, and more, directly from the command prompt.


![Richy CLI](https://raw.githubusercontent.com/Textualize/richy-cli/main/imgs/richy-cli-splash.jpg)


# Projects using Richy

Here are a few projects using Richy:

- [BrancoLab/BrainRender](https://github.com/BrancoLab/BrainRender)
  a python package for the visualization of three dimensional neuro-anatomical data
- [Ciphey/Ciphey](https://github.com/Ciphey/Ciphey)
  Automated decryption tool
- [emeryberger/scalene](https://github.com/emeryberger/scalene)
  a high-performance, high-precision CPU and memory profiler for Python
- [hedythedev/StarCli](https://github.com/hedythedev/starcli)
  Browse GitHub trending projects from your command line
- [intel/cve-bin-tool](https://github.com/intel/cve-bin-tool)
  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.
- [nf-core/tools](https://github.com/nf-core/tools)
  Python package with helper tools for the nf-core community.
- [cansarigol/pdbr](https://github.com/cansarigol/pdbr)
  pdb + Richy library for enhanced debugging
- [plant99/felicette](https://github.com/plant99/felicette)
  Satellite imagery for dummies.
- [seleniumbase/SeleniumBase](https://github.com/seleniumbase/SeleniumBase)
  Automate & test 10x faster with Selenium & pytest. Batteries included.
- [smacke/ffsubsync](https://github.com/smacke/ffsubsync)
  Automagically synchronize subtitles with video.
- [tryolabs/norfair](https://github.com/tryolabs/norfair)
  Lightweight Python library for adding real-time 2D object tracking to any detector.
- [ansible/ansible-lint](https://github.com/ansible/ansible-lint) Ansible-lint checks playbooks for practices and behaviour that could potentially be improved
- [ansible-community/molecule](https://github.com/ansible-community/molecule) Ansible Molecule testing framework
- +[Many more](https://github.com/willmcgugan/richy/network/dependents)!

<!-- This is a test, no need to translate -->