Skip to content

Commit 58dea60

Browse files
NickCrewscpcloud
authored andcommitted
feat(ux): workaround for spurious display()s in jupyter
1 parent 038b5d5 commit 58dea60

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

ibis/expr/types/rich.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from __future__ import annotations
88

9+
import contextlib
910
from typing import TYPE_CHECKING
1011

1112
from ibis.expr import types as ir
@@ -29,7 +30,8 @@ class FixedTextJupyterMixin(JupyterMixin):
2930

3031
def _repr_mimebundle_(self, *args, **kwargs):
3132
try:
32-
bundle = super()._repr_mimebundle_(*args, **kwargs)
33+
with _with_rich_display_disabled():
34+
bundle = super()._repr_mimebundle_(*args, **kwargs)
3335
except Exception: # noqa: BLE001
3436
return None
3537
else:
@@ -42,7 +44,7 @@ def capture_rich_renderable(renderable: RenderableType) -> str:
4244
from rich.console import Console
4345

4446
console = Console(force_terminal=False)
45-
with console.capture() as capture:
47+
with _with_rich_display_disabled(), console.capture() as capture:
4648
console.print(renderable)
4749
return capture.get().rstrip()
4850

@@ -74,3 +76,30 @@ def to_rich(
7476
max_depth=max_depth,
7577
console_width=console_width,
7678
)
79+
80+
81+
@contextlib.contextmanager
82+
def _with_rich_display_disabled():
83+
"""Workaround to keep rich from doing spurious display() calls in Jupyter.
84+
85+
When you display(ibis.Table), without this, an extra output cell is created
86+
in the notebook. With this, there is no extra output cell.
87+
88+
See https://github.com/Textualize/rich/pull/3329
89+
"""
90+
try:
91+
from IPython import display as ipython_display
92+
except ImportError:
93+
# IPython is not installed, so nothing to do
94+
yield
95+
else:
96+
97+
def noop_display(*args, **kwargs):
98+
pass
99+
100+
original_display = ipython_display.display
101+
try:
102+
ipython_display.display = noop_display
103+
yield
104+
finally:
105+
ipython_display.display = original_display

0 commit comments

Comments
 (0)