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.

Custom __rich_console__ method - only the first line of output is being displayed

See original GitHub issue

Read the docs I have checked the documentation for __rich_console__ - I couldnโ€™t find much, so I may have missed something.

Describe the bug I have a class that wraps a renderable, with a custom __rich_console__ method which yields only the last N lines of the rendered representation of the wrapped renderable. However, when I use it, only the first of the yielded lines is displayed.

To Reproduce

import rich

class Latest:
  def __init__(self, n, renderable):
    self.n = n
    self.renderable = renderable
  def __rich_console__(self, console, options):
    for line in console.render_lines(self.renderable)[-self.n:]:
        for seg in line:
            print(repr(seg))
            yield seg

rich.print(Latest(3, "\n".join(f"Line {i}" for i in range(10))))

Output from this is:

Segment('Line ', Style())
Segment('7', Style(color=Color('cyan', ColorType.STANDARD, number=6), bold=True, italic=False))
Segment('
   ', Style())
Segment('Line ', Style())
Segment('8', Style(color=Color('cyan', ColorType.STANDARD, number=6), bold=True, italic=False))
Segment('
   ', Style())
Segment('Line ', Style())
Segment('9', Style(color=Color('cyan', ColorType.STANDARD, number=6), bold=True, italic=False))
Segment('
   ')
Line 7

It shows all of the segments, but the rendered output is only โ€œLine 7โ€ - the following lines appear to have been discarded.

Platform What platform (Win/Linux/Mac) are you running on? Windows 10 What terminal software are you using? Windows Terminal

Diagnose I may ask you to cut and paste the output of the following commands. It may save some time if you do it now.

python -m rich.diagnose
python -m rich._windows
pip freeze | grep rich
โฏ python -m rich.diagnose
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ <class 'rich.console.Console'> โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ A high level console interface.                                                  โ”‚
โ”‚                                                                                  โ”‚
โ”‚ โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ โ”‚
โ”‚ โ”‚ <console width=120 ColorSystem.TRUECOLOR>                                    โ”‚ โ”‚
โ”‚ โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ โ”‚
โ”‚                                                                                  โ”‚
โ”‚     color_system = 'truecolor'                                                   โ”‚
โ”‚         encoding = 'utf-8'                                                       โ”‚
โ”‚             file = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'> โ”‚
โ”‚           height = 30                                                            โ”‚
โ”‚    is_alt_screen = False                                                         โ”‚
โ”‚ is_dumb_terminal = False                                                         โ”‚
โ”‚   is_interactive = True                                                          โ”‚
โ”‚       is_jupyter = False                                                         โ”‚
โ”‚      is_terminal = True                                                          โ”‚
โ”‚   legacy_windows = False                                                         โ”‚
โ”‚         no_color = False                                                         โ”‚
โ”‚          options = ConsoleOptions(                                               โ”‚
โ”‚                        size=ConsoleDimensions(width=120, height=30),             โ”‚
โ”‚                        legacy_windows=False,                                     โ”‚
โ”‚                        min_width=1,                                              โ”‚
โ”‚                        max_width=120,                                            โ”‚
โ”‚                        is_terminal=True,                                         โ”‚
โ”‚                        encoding='utf-8',                                         โ”‚
โ”‚                        max_height=30,                                            โ”‚
โ”‚                        justify=None,                                             โ”‚
โ”‚                        overflow=None,                                            โ”‚
โ”‚                        no_wrap=False,                                            โ”‚
โ”‚                        highlight=None,                                           โ”‚
โ”‚                        markup=None,                                              โ”‚
โ”‚                        height=None                                               โ”‚
โ”‚                    )                                                             โ”‚
โ”‚            quiet = False                                                         โ”‚
โ”‚           record = False                                                         โ”‚
โ”‚         safe_box = True                                                          โ”‚
โ”‚             size = ConsoleDimensions(width=120, height=30)                       โ”‚
โ”‚        soft_wrap = False                                                         โ”‚
โ”‚           stderr = False                                                         โ”‚
โ”‚            style = None                                                          โ”‚
โ”‚         tab_size = 8                                                             โ”‚
โ”‚            width = 120                                                           โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
PS 16:55 (.venv) 00:00.461 C:\Work\Scratch\threadprog
โฏ python -m rich._windows
platform="Windows"
WindowsConsoleFeatures(vt=True, truecolor=True)
PS 16:56 (.venv) 00:00.449 C:\Work\Scratch\threadprog
โฏ pip freeze | grep rich
rich==10.10.0

Did I help?

If I was able to resolve your problem, consider sponsoring my work on Rich, or buy me a coffee to say thanks.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
pfmoorecommented, Sep 23, 2021

Ah! They are probably long stretches of spaces to fill the line. Itโ€™s the terminal wrapping them that confused me. That makes sense (Iโ€™m sort of abusing the machinery here, so getting something weird isnโ€™t entirely unexpected).

Thanks for the very fast and useful help.

1reaction
willmcgugancommented, Sep 23, 2021

Youโ€™ll need new lines (โ€œ\nโ€) in the output. Easiest way to do that would be to set new_lines=True on the render_lines call.

Read more comments on GitHub >

github_iconTop Results From Across the Web

rich.console โ€” Rich 12.6.0 documentation
Context manager to capture the result of printing to the console. See capture() for how to use. Parameters. console (Console) โ€“ A console...
Read more >
Redirect print and/or logging to Panel - python - Stack Overflow
Is there a way to put standard (or rich) print statements into a particular panel? Being able to show logging output in its...
Read more >
Generate Rich and Beautiful Text in the Terminal with Python ...
Solution: You can simply use Console object to print both the output and all the variables in the function. Image by Author. You...
Read more >
How to Use the Rich Library with Python - freeCodeCamp
Rich is a Python library for writing rich text (with color and style) to the terminal. It lets you display advanced content such...
Read more >
Bash Reference Manual - GNU.org
When the command history expansion facilities are being used (see ... These variables are visible only to the function and the commands itย ......
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