Rendering to String

Surely this is possible more elegantly, but by browsing the rich docs and source code I figured I can extract the unstyled string for use e.g. in files like this:

from rich.panel import Panel
from rich.console import Console

s=''.join(s.text for s in Console().render(Panel("test",width=40)))
print(s)

output:

╭──────────────────────────────────────╮
│ test                                 │
╰──────────────────────────────────────╯

Is this the official way to get the unstyled string from a renderable? Or is there another convenience method?

I’m not clear on what you mean by “unstyled string”, but I suspect you want to Rich capture API.

https://rich.readthedocs.io/en/latest/console.html#capturing-output

1 Like

Thanks, Will, I must have categorically overread that as I thought this has to do with shell redirection.