6
6
7
7
from __future__ import annotations
8
8
9
+ import contextlib
9
10
from typing import TYPE_CHECKING
10
11
11
12
from ibis .expr import types as ir
@@ -29,7 +30,8 @@ class FixedTextJupyterMixin(JupyterMixin):
29
30
30
31
def _repr_mimebundle_ (self , * args , ** kwargs ):
31
32
try :
32
- bundle = super ()._repr_mimebundle_ (* args , ** kwargs )
33
+ with _with_rich_display_disabled ():
34
+ bundle = super ()._repr_mimebundle_ (* args , ** kwargs )
33
35
except Exception : # noqa: BLE001
34
36
return None
35
37
else :
@@ -42,7 +44,7 @@ def capture_rich_renderable(renderable: RenderableType) -> str:
42
44
from rich .console import Console
43
45
44
46
console = Console (force_terminal = False )
45
- with console .capture () as capture :
47
+ with _with_rich_display_disabled (), console .capture () as capture :
46
48
console .print (renderable )
47
49
return capture .get ().rstrip ()
48
50
@@ -74,3 +76,30 @@ def to_rich(
74
76
max_depth = max_depth ,
75
77
console_width = console_width ,
76
78
)
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