Is there any way to render a _rich_ object to `str`?
See original GitHub issueI 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:
- Created 3 years ago
- Comments:7 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Perhaps the
force_jupyter
parameter in the Console constructor should have three states;False
to disable jupyter,True
to enable it, and a default ofNone
to auto detect.That way you could
force_jupyter=False
in__str__
so you always get a string.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.