rich
rich copied to clipboard
[BUG] Captured console output still results in newline being printed in Jupyter
- [x] I've checked docs and closed issues for possible solutions.
- [x] I can't find my issue in the FAQ.
Describe the bug
When using console.capture()
in Jupyter, a new line gets printed to the notebook when exiting the capture context. This is unexpected, and is also different from rich
's behavior in terminal (no new lines printed).
Here's a minimal example. After running the following in a Jupyter cell, 100 new lines are printed:
import rich.console
console = rich.console.Console()
for _ in range(100):
with console.capture() as capture:
console.print(1)
I think the issue is within Console._check_buffer
. When exiting a capture context, self._render_buffer(self._buffer[:])
returns an empty string. When not in Jupyter, we eventually call self.file.write
which is no-op when the text is empty. However, in Jupyter we'd call IPython.display.display
, which IIUC always results in an empty line.
https://github.com/Textualize/rich/blob/26152e9cc95eef9c8f363d7bf1dfda426275348d/rich/console.py#L2003-L2066
Platform
Click to expand
What platform (Win/Linux/Mac) are you running on? What terminal software are you using?
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:
python -m rich.diagnose
pip freeze | grep rich
If you're using Rich in a Jupyter Notebook, run the following snippet in a cell and paste the output in your bug report.
from rich.diagnose import report
report()
╭────────────────────── <class 'rich.console.Console'> ──────────────────────╮
│ A high level console interface. │
│ │
│ ╭────────────────────────────────────────────────────────────────────────╮ │
│ │ <console width=115 ColorSystem.TRUECOLOR> │ │
│ ╰────────────────────────────────────────────────────────────────────────╯ │
│ │
│ color_system = 'truecolor' │
│ encoding = 'utf-8' │
│ file = <ipykernel.iostream.OutStream object at 0x7f8bf0a35090> │
│ height = 100 │
│ is_alt_screen = False │
│ is_dumb_terminal = False │
│ is_interactive = False │
│ is_jupyter = True │
│ is_terminal = False │
│ legacy_windows = False │
│ no_color = False │
│ options = ConsoleOptions( │
│ size=ConsoleDimensions(width=115, height=100), │
│ legacy_windows=False, │
│ min_width=1, │
│ max_width=115, │
│ is_terminal=False, │
│ encoding='utf-8', │
│ max_height=100, │
│ justify=None, │
│ overflow=None, │
│ no_wrap=False, │
│ highlight=None, │
│ markup=None, │
│ height=None │
│ ) │
│ quiet = False │
│ record = False │
│ safe_box = True │
│ size = ConsoleDimensions(width=115, height=100) │
│ soft_wrap = False │
│ stderr = False │
│ style = None │
│ tab_size = 8 │
│ width = 115 │
╰────────────────────────────────────────────────────────────────────────────╯
╭─── <class 'rich._windows.WindowsConsoleFeatures'> ────╮
│ Windows features available. │
│ │
│ ╭───────────────────────────────────────────────────╮ │
│ │ WindowsConsoleFeatures(vt=False, truecolor=False) │ │
│ ╰───────────────────────────────────────────────────╯ │
│ │
│ truecolor = False │
│ vt = False │
╰───────────────────────────────────────────────────────╯
╭────── Environment Variables ───────╮
│ { │
│ 'TERM': 'xterm-color', │
│ 'COLORTERM': None, │
│ 'CLICOLOR': '1', │
│ 'NO_COLOR': None, │
│ 'TERM_PROGRAM': 'tmux', │
│ 'COLUMNS': None, │
│ 'LINES': None, │
│ 'JUPYTER_COLUMNS': None, │
│ 'JUPYTER_LINES': None, │
│ 'JPY_PARENT_PID': '112278', │
│ 'VSCODE_VERBOSE_LOGGING': None │
│ } │
╰────────────────────────────────────╯
platform="Linux"