This commit is contained in:
Nick Crews 2026-02-05 10:45:21 -08:00 committed by GitHub
commit e89931be56
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 0 deletions

View File

@ -83,6 +83,12 @@ def _render_segments(segments: Iterable[Segment]) -> str:
def display(segments: Iterable[Segment], text: str) -> None:
"""Render segments to Jupyter."""
segments = list(segments)
if not segments and not text:
# display() always prints a newline, so if there is no content then
# we don't want a single newline appearing.
# See https://github.com/Textualize/rich/issues/3274
return None
html = _render_segments(segments)
jupyter_renderable = JupyterRenderable(html, text)
try:

View File

@ -30,3 +30,22 @@ def test_jupyter_lines_env():
console = Console(
width=40, _environ={"JUPYTER_LINES": "broken"}, force_jupyter=True
)
def test_jupyter_capture(monkeypatch):
# If inside a capture, ipython's display shouldn't be called,
# or we would get spurious newlines.
# See https://github.com/Textualize/rich/issues/3274
called = False
def mock_display(*args, **kwargs):
nonlocal called
called = True
monkeypatch.setattr("IPython.display.display", mock_display)
console = Console(force_jupyter=True)
with console.capture():
console.print("foo")
assert not called
console.print("foo")
assert called