Adding __rich__ methods to python classes

waylonwalker

Waylon Walker

Posted on December 24, 2021

Adding __rich__ methods to python classes

Adding a __render__ method that returns a rich renderable to any python class makes it display this output if printed with rich. This also includes being nested inside of a rich Layout.

import rich
from rich.panel import Panel


class ShowMe:
    def __rich__(self):
        return Panel("hello", border_style="gold1")


if __name__ == "__main__":
    rich.print(ShowMe())
Enter fullscreen mode Exit fullscreen mode

results of printing ShowMe with rich


I'm shortening up my content pipeline and getting small ideas out more often by creating small tils that will eventually bubble up into larger peices.

All of my tils are on my website -> https://waylonwalker.com/til

💖 💪 🙅 🚩
waylonwalker
Waylon Walker

Posted on December 24, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related