Skip to content

Commit

Permalink
chore: bring back mimebundle
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Sep 3, 2024
1 parent c064941 commit 7dbb3c1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
1 change: 0 additions & 1 deletion docs/backends/support/operations.qmd

This file was deleted.

13 changes: 7 additions & 6 deletions ibis/backends/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,7 @@ def test_interactive_repr_max_columns(alltypes, is_jupyter, monkeypatch):

@pytest.mark.parametrize("expr_type", ["table", "column"])
@pytest.mark.parametrize("interactive", [True, False])
def test_repr_html(alltypes, interactive, expr_type, monkeypatch):
def test_repr_mimebundle(alltypes, interactive, expr_type, monkeypatch):
pytest.importorskip("rich")

monkeypatch.setattr(ibis.options, "interactive", interactive)
Expand All @@ -1212,11 +1212,12 @@ def test_repr_html(alltypes, interactive, expr_type, monkeypatch):
else:
expr = alltypes.select("date_string_col")

text = expr._repr_html_()
if interactive:
assert "r0.date_string_col" not in text
else:
assert "r0.date_string_col" in text
reprs = expr._repr_mimebundle_(include=["text/plain", "text/html"], exclude=[])
for format in ["text/plain", "text/html"]:
if interactive:
assert "r0.date_string_col" not in reprs[format]
else:
assert "r0.date_string_col" in reprs[format]


@pytest.mark.never(
Expand Down
33 changes: 19 additions & 14 deletions ibis/expr/types/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@
from ibis.backends import BaseBackend
from ibis.expr.visualize import EdgeAttributeGetter, NodeAttributeGetter

try:
from rich.jupyter import JupyterMixin
except ImportError:

class _FixedTextJupyterMixin:
"""No-op when rich isn't installed."""
else:

class _FixedTextJupyterMixin(JupyterMixin):
def _repr_mimebundle_(self, *args, **kwargs):
try:
bundle = super()._repr_mimebundle_(*args, **kwargs)
except Exception: # noqa: BLE001
return None
else:
bundle["text/plain"] = bundle["text/plain"].rstrip()
return bundle


def _capture_rich_renderable(
renderable: RenderableType, *, force_terminal: bool | None = None
Expand All @@ -45,7 +63,7 @@ def _capture_rich_renderable(


@public
class Expr(Immutable, Coercible):
class Expr(Immutable, Coercible, _FixedTextJupyterMixin):
"""Base expression class."""

__slots__ = ("_arg",)
Expand All @@ -67,19 +85,6 @@ def __repr__(self) -> str:
def _repr_pretty_(self, p, cycle):
p.text(_capture_rich_renderable(self))

def _repr_html_(self):
from rich import get_console
from rich.jupyter import _render_segments

console = get_console()

try:
segments = list(console.render(self, console.options))
except Exception: # noqa: BLE001
return None
html = _render_segments(segments)
return html.rstrip()

def __rich_console__(self, console: Console, options: ConsoleOptions):
if console.is_jupyter:
# Rich infers a console width in jupyter notebooks, but since
Expand Down

0 comments on commit 7dbb3c1

Please sign in to comment.