From 7dbb3c1786f6086c5ba7a3ce5929fc848bf66dae Mon Sep 17 00:00:00 2001 From: Phillip Cloud <417981+cpcloud@users.noreply.github.com> Date: Tue, 3 Sep 2024 11:09:19 -0400 Subject: [PATCH] chore: bring back mimebundle --- docs/backends/support/operations.qmd | 1 - ibis/backends/tests/test_client.py | 13 ++++++----- ibis/expr/types/core.py | 33 ++++++++++++++++------------ 3 files changed, 26 insertions(+), 21 deletions(-) delete mode 100644 docs/backends/support/operations.qmd diff --git a/docs/backends/support/operations.qmd b/docs/backends/support/operations.qmd deleted file mode 100644 index a1e0dde71c3f6..0000000000000 --- a/docs/backends/support/operations.qmd +++ /dev/null @@ -1 +0,0 @@ -{{< include ../../reference/operations.qmd >}} diff --git a/ibis/backends/tests/test_client.py b/ibis/backends/tests/test_client.py index 067c10ecad828..9dba682e11ed5 100644 --- a/ibis/backends/tests/test_client.py +++ b/ibis/backends/tests/test_client.py @@ -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) @@ -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( diff --git a/ibis/expr/types/core.py b/ibis/expr/types/core.py index de645aa4fa4ac..813fbd22ce56b 100644 --- a/ibis/expr/types/core.py +++ b/ibis/expr/types/core.py @@ -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 @@ -45,7 +63,7 @@ def _capture_rich_renderable( @public -class Expr(Immutable, Coercible): +class Expr(Immutable, Coercible, _FixedTextJupyterMixin): """Base expression class.""" __slots__ = ("_arg",) @@ -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