question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there any way to render a _rich_ object to `str`?

See original GitHub issue

I currently use io.StringIO to get a str. Is there any simpler way to do it?

from io import StringIO

from rich.table import Table
from rich.console import Console

buf = StringIO()
table = Table(...)
console(file=buf)
console.print(table)
buf.getvalue()  # this is the `str` I would like

I find __repr__ and __str__ output the same thing like <rich.table.Table object at 0x7fc9cd308ac0>, may be rich can provide an api of __str__ or .to_str()

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
willmcgugancommented, Sep 7, 2020

Perhaps the force_jupyter parameter in the Console constructor should have three states; False to disable jupyter, True to enable it, and a default of None to auto detect.

That way you could force_jupyter=False in __str__ so you always get a string.

0reactions
honnocommented, Sep 7, 2020

Awesome, thanks so much!

(I had actually encountered a problem with the notebook hosting platform Kaggle today, where Jupyter-rendered text is printed white… on a white background! So to use my library on it nicely, I really did need this behaviour in the end.)

For future reference, the below worked well for me.

from io import StringIO

from rich.console import Console
...
class MyClass():
    ...
    def __rich__(self):   # or __rich_console__
        ...

    def __str__(self):
        buf = StringIO()
        console = Console(file=buf, force_jupyter=False)
        console.print(self)

        return buf.getvalue()
Read more comments on GitHub >

github_iconTop Results From Across the Web

Console Protocol — Rich 12.6.0 documentation
This method accepts no arguments, and should return an object that Rich knows how to render, such as a Text or Table ....
Read more >
Custom `__str__` vs `__repr__` · Discussion #1847 - GitHub
So I was expecting to find a way to have full control of how the custom object gets printed, regardless of whether python...
Read more >
Colorizing Text within Rich Tree - python - Stack Overflow
You can highlight text via a Rich highlighter. The ReprHighlighter will highlight the strings produces from most objects.
Read more >
How to Use the Rich Library with Python - freeCodeCamp
Rich can render text or other Rich renderables in neat columns with the Columns class. To use, construct a Columns instance with an...
Read more >
How To Render Different Output for Rich Text in Kentico ...
Rendering Option One: Strings ; public class ; BlockquoteResolver · IInlineContentItemsResolver ; Blockquote · public string Resolve(Blockquote data) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found