rich icon indicating copy to clipboard operation
rich copied to clipboard

[BUG] Printing with markup disabled, disables markup in renderables using __rich_console__ protocol

Open daniel-kukiela opened this issue 7 months ago • 3 comments

  • [x] I've checked docs and closed issues for possible solutions.
  • [x] I can't find my issue in the FAQ.

Describe the bug

Hi, I'm using Live() with a few custom renderables using __rich_console__ protocol. In my app, I have a need to print in 2 ways - with and without markup:

    # [...]
    self._rich_console = get_console()
    # [...]
    self._rich_console.print(line, markup=False, highlight=False, no_wrap=True, overflow="ignore", crop=False)
    # [...]
    self._rich_console.print(log_str, markup=True, highlight=False, no_wrap=True, overflow="ignore", crop=False)

For renderables, I'm using markup:

class ExampleRenderable:
    def __rich_console__(self, console: Console, options: ConsoleOptions) -> RenderResult:
        yield '[green]Some result'

The problem is that whenever I print with markup disabled, I can see that Live renderables also have markup disabled (because of the volume of console messages at that time and the fps I set, I see it for very short periods, I think that printing with markup of following lines switches it back). I tried to set the markup back in renderables using options.markup = True, and even console._markup = True, but this does not help:

class ExampleRenderable:
    def __rich_console__(self, console: Console, options: ConsoleOptions) -> RenderResult:
        options.markup = True
        console._markup = True
        yield '[green]Some result'

I think this has something to do with how print() works, it sets the render options: render_options = self.options.update(...), and the order of operations. Is it not possible to print with settings in a way they do not affect renderables? I do not have a code sample to reproduce this yet, but I could write it later if necessary. I thought I would ask first before proceeding with further debugging.

Platform

Click to expand

What platform (Win/Linux/Mac) are you running on? What terminal software are you using? Ubuntu 22.04 through SSH using Putty on Windows.

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

If you're using Rich in a terminal:

╭───────────────────────── <class 'rich.console.Console'> ─────────────────────────╮
│ A high level console interface.                                                  │
│                                                                                  │
│ ╭──────────────────────────────────────────────────────────────────────────────╮ │
│ │ <console width=139 ColorSystem.STANDARD>                                     │ │
│ ╰──────────────────────────────────────────────────────────────────────────────╯ │
│                                                                                  │
│     color_system = 'standard'                                                    │
│         encoding = 'utf-8'                                                       │
│             file = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'> │
│           height = 36                                                            │
│    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=139, height=36),             │
│                        legacy_windows=False,                                     │
│                        min_width=1,                                              │
│                        max_width=139,                                            │
│                        is_terminal=True,                                         │
│                        encoding='utf-8',                                         │
│                        max_height=36,                                            │
│                        justify=None,                                             │
│                        overflow=None,                                            │
│                        no_wrap=False,                                            │
│                        highlight=None,                                           │
│                        markup=None,                                              │
│                        height=None                                               │
│                    )                                                             │
│            quiet = False                                                         │
│           record = False                                                         │
│         safe_box = True                                                          │
│             size = ConsoleDimensions(width=139, height=36)                       │
│        soft_wrap = False                                                         │
│           stderr = False                                                         │
│            style = None                                                          │
│         tab_size = 8                                                             │
│            width = 139                                                           │
╰──────────────────────────────────────────────────────────────────────────────────╯
╭─── <class 'rich._windows.WindowsConsoleFeatures'> ────╮
│ Windows features available.                           │
│                                                       │
│ ╭───────────────────────────────────────────────────╮ │
│ │ WindowsConsoleFeatures(vt=False, truecolor=False) │ │
│ ╰───────────────────────────────────────────────────╯ │
│                                                       │
│ truecolor = False                                     │
│        vt = False                                     │
╰───────────────────────────────────────────────────────╯
╭────── Environment Variables ───────╮
│ {                                  │
│     'TERM': 'xterm',               │
│     'COLORTERM': None,             │
│     'CLICOLOR': None,              │
│     'NO_COLOR': None,              │
│     'TERM_PROGRAM': None,          │
│     'COLUMNS': None,               │
│     'LINES': None,                 │
│     'JUPYTER_COLUMNS': None,       │
│     'JUPYTER_LINES': None,         │
│     'JPY_PARENT_PID': None,        │
│     'VSCODE_VERBOSE_LOGGING': None │
│ }                                  │
╰────────────────────────────────────╯
platform="Linux"

rich==13.7.1

daniel-kukiela avatar Jun 04 '25 15:06 daniel-kukiela

We found the following entries in the FAQ which you may find helpful:

Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

This is an automated reply, generated by FAQtory

github-actions[bot] avatar Jun 04 '25 15:06 github-actions[bot]

I'm afraid I don't follow. Please post that MRE and I will take a look.

willmcgugan avatar Jun 24 '25 07:06 willmcgugan

Hi, Thank you for the reply.

Here's the MRE:

import rich
from rich.console import Console
from rich.console import ConsoleOptions, RenderResult
from rich.live import Live
import time


class ExampleRenderable:
    def __rich_console__(self, console: Console, options: ConsoleOptions) -> RenderResult:
        options.markup = True
        # console._markup = True
        yield '[green]Markup issues here'

live = Live(ExampleRenderable(), refresh_per_second=0.1)
live.start()

console = rich.get_console()

line_markup = '[red]Markup print'
line_plain = 'Plain text print'

for _ in range(10):
    time.sleep(1)
    console.print(line_markup, markup=True, highlight=False, no_wrap=True, overflow="ignore", crop=False)
    time.sleep(1)
    console.print(line_plain, markup=False, highlight=False, no_wrap=True, overflow="ignore", crop=False)

I set the refresh_per_second value really low to illustrate the problem more clearly.

A workaround for that I'm using is that I never set markup=False and escape [ -> \[

daniel-kukiela avatar Jun 26 '25 09:06 daniel-kukiela