mirror of
https://github.com/Textualize/rich.git
synced 2026-02-06 10:58:48 +00:00
Merge b5ef155de5 into 0752ff0472
This commit is contained in:
commit
e89931be56
@ -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:
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user