May 3, 2020 will

How to get Rich with Python (a terminal rendering library)

I'm happy to announce the release of version 1.0.0 of Rich - my Python library for fancy terminal rendering.

Rich is a project I started back in October 2019 when I was staying in Wuhan, a few weeks before it was locked down. Back then it was a toy project designed to pass the time while traveling. In the spirit of XKCD 927 I wanted to create a library that could display rich content in the terminal as effortlessly as you can with HTML.

Naturally, it took a lot longer than I had anticipated. Once I had the core feature of text which can word wrap while preserving style attributes, I used it to implement syntax highlighting, Markdown rendering, pretty tracebacks, progress bars, and more. At the risk of over-engineer I've drawn a line in the core feature set for this 1.0 release, which has enough batteries incuded for now.

Rich is also something of a framework in that it defines a protocol you can use to render custom objects with style and color to the terminal. This could be rich text, but could also be a table or markdown. Here's a trivial example from the docs:

Rendering rich text from a custom object.

Rich already knows how to render simple types, and will pretty print and colorize containers by default. Here's an example that shows what happens when you print a dict with Rich:

A quick example of Rich print compared to builtin print.

This feature was inspired by Javascript's console.log which I always miss when working in the back-end.

I'm hoping that library developers will begin to implement the Rich protocol. At the simplest level it doesn't even require you to add Rich as a dependancy.

Rich is production ready, with high test coverage, and completely typed. It follows semver. See the Rich docs for the details.

Use Markdown for formatting
*Italic* **Bold** `inline code` Links to [Google](http://www.google.com) > This is a quote > ```python import this ```
your comment will be previewed here
gravatar
Evan E. Baird

I can forgive the clickbait title but not the pun. Also, this is an amazing library!

gravatar
Will McGugan

Thanks, Evan. And sorry.

gravatar
Arseniy Terekhin

Hi! Great library. Can I somehow clear the screen using the library? Currently I do:

  import sys
  sys.stdout.write("\033c")
gravatar
Will McGugan

Not currently, but that seems something that is worth adding. Could you post an issue on the Github repos to remind me? https://github.com/willmcgugan/rich

gravatar
Teru Watanabe

Hi, is there any simple way to use Rich in Visual Studio Code?

gravatar
Will McGugan

Hi. Rich works fine in the vscode terminal. Are you having problems with it?

gravatar
Teru Watanabe

I guess it works, but I have to use the print to get the beautiful output, right? What I asked is that if there is any vscode extension that beautifies the console output automatically. My line of work is data analysis so I often look up contents of arrays and DataFrames in console. It would be great if I just type an object name in console and get the beautified output.

gravatar
Will McGugan

Ah. The only way that could work would be by wrapping stdout. Which is theoretically possible. I'll think about it. It actually sounds like Jupyter would be more the tool of your trade than the terminal.

gravatar
Teru Watanabe

Thank you, I look forward to it.

gravatar
Baala_M

Hi, Thanks for this great library. Is it possible to add a list as row.

gravatar
Baala_M

I went through your examples and figured it out. Thanks for this wonderful library again.