Have you heard of rich?

Have you heard of rich?

Honestly, I don’t remember how long I’ve been following Will’s work, but I find it very interesting and educational. Among Will’s work are rich and textual, both projects designed to extend the use of the terminal.

One thing you can see in Will’s work is the modular design. On one hand, there’s rich, which basically gives you the necessary tools to format console output to your liking, making the console output clearer and more attractive.

Then we have textual, a framework for making TUIs (terminal user interfaces) that depends on rich. The value of textual is not only being able to create TUIs in Python easily and elegantly, but also that the TUI you develop could run in a web browser! The same application can run in the terminal or in the browser. This is music to the ears of many backends.

The most immediate use I can think of is that I could write many internal applications easily that I can share with the team, regardless of the hacking skills of my team members.

What do you use rich for?

rich has a series of ready-to-use functions that you can take advantage of without having to build anything additionally.

Better print

https://rich.readthedocs.io/en/stable/pretty.html

rich has made me love using print again. I no longer use the built-in print, but use rich’s:

1
from rich import print

For example, print(my_json) with rich, the output is much more elegant and readable than using pprint. Other examples are print(my_list), print(my_dict), etc.

Better Logging

https://rich.readthedocs.io/en/stable/logging.html

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import logging
from rich.logging import RichHandler

FORMAT = "%(message)s"
logging.basicConfig(
    level="NOTSET", format=FORMAT, datefmt="[%X]", handlers=[RichHandler()]
)

log = logging.getLogger("rich")
log.info("Hello, World!")

Better Traceback

https://rich.readthedocs.io/en/stable/traceback.html

1
2
from rich.traceback import install
install(show_locals=True)

DIY

Then you have a series of objects with which you can build really useful and beautiful console outputs, such as columns, tables, layouts, etc.

In the documentation, you have everything you need to get started.

Just try it, you will enjoy the output.