Skip to content

Retrieving the raw string of a Renderable #1661

Answered by paw-lu
cadojo asked this question in Q&A
Discussion options

You must be logged in to vote

Probably the easiest way to do this is to capture the output via io.StringIO

import io

from rich import console

con = console.Console(file=io.StringIO(), force_terminal=True)
con.print("[bold red]Hello[/] World")
str_output = con.file.getvalue()

print(f"  {str_output}")

Docs for more info.

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by cadojo
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants