Sunday, May 3, 2020

Will McGugan: 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:

© 2020 Will McGugan

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:

© 2020 Will McGugan

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.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...