Textualize/rich

[BUG] Regression: cannot export captured output since v13.8.0

Closed this issue · 4 comments

Describe the bug

Since version 13.8 calling export_... or save_... methods on a console stopped exporting captured output. This broke my library that uses this to save the output of the console to different file formats.

To reproduce create a file "console_export_bug.py":

from rich.console import Console

console = Console(record=True)
with console.capture():
    console.print("ExportMe")
exported_text = console.export_text(clear=False)
assert "ExportMe" in exported_text, repr(exported_text)
exported_svg = console.export_svg(clear=False)
assert "ExportMe" in exported_svg, exported_svg
exported_html = console.export_html(clear=False)
assert "ExportMe" in exported_html, exported_html
$ uv run --with 'rich<13.8' console_export_bug.py
$ uv run --with 'rich>13.8' console_export_bug.py
Installed 5 packages in 6ms
Traceback (most recent call last):
  File "/tmp/console_export_bug.py", line 7, in <module>
    assert "ExportMe" in exported_text, repr(exported_text)
AssertionError: ''

All three asserts above fail now.

This also fails to print even if the export_... calls are moved into the with block.

If this change is intentional, is there a way to export the contents of a console without printing them to the terminal that still works with recent versions of rich? Thanks.

Reported downstream issue: hamdanal/rich-argparse#133

Platform

Click to expand
╭───────────────────────── <class 'rich.console.Console'> ─────────────────────────╮
│ A high level console interface.                                                  │
│                                                                                  │
│ ╭──────────────────────────────────────────────────────────────────────────────╮ │
│ │ <console width=102 ColorSystem.EIGHT_BIT>                                    │ │
│ ╰──────────────────────────────────────────────────────────────────────────────╯ │
│                                                                                  │
│     color_system = '256'                                                         │
│         encoding = 'utf-8'                                                       │
│             file = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'> │
│           height = 53                                                            │
│    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=102, height=53),             │
│                        legacy_windows=False,                                     │
│                        min_width=1,                                              │
│                        max_width=102,                                            │
│                        is_terminal=True,                                         │
│                        encoding='utf-8',                                         │
│                        max_height=53,                                            │
│                        justify=None,                                             │
│                        overflow=None,                                            │
│                        no_wrap=False,                                            │
│                        highlight=None,                                           │
│                        markup=None,                                              │
│                        height=None                                               │
│                    )                                                             │
│            quiet = False                                                         │
│           record = False                                                         │
│         safe_box = True                                                          │
│             size = ConsoleDimensions(width=102, height=53)                       │
│        soft_wrap = False                                                         │
│           stderr = False                                                         │
│            style = None                                                          │
│         tab_size = 8                                                             │
│            width = 102                                                           │
╰──────────────────────────────────────────────────────────────────────────────────╯
╭─── <class 'rich._windows.WindowsConsoleFeatures'> ────╮
│ Windows features available.                           │
│                                                       │
│ ╭───────────────────────────────────────────────────╮ │
│ │ WindowsConsoleFeatures(vt=False, truecolor=False) │ │
│ ╰───────────────────────────────────────────────────╯ │
│                                                       │
│ truecolor = False                                     │
│        vt = False                                     │
╰───────────────────────────────────────────────────────╯
╭────── Environment Variables ───────╮
│ {                                  │
│     'TERM': 'xterm-256color',      │
│     '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"

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

This is the expected behavior. The captured content is excluded from the output. It was broken in an earlier version.

If this change is intentional, is there a way to export the contents of a console without printing them to the terminal that still works with recent versions of rich? Thanks.

import io

from rich.console import Console
console = Console(file=io.StringIO(), record=True)
console.print("ExportMe")
exported_text = console.export_text(clear=False)
print(repr(exported_text))

Thank you, I'll try this.

I hope we solved your problem.

If you like using Rich, you might also enjoy Textual