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 e921f03 commit 9ab3590
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
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: 20 additions & 13 deletions ibis/expr/types/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@
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 is not installed."""
else:

class _FixedTextJupyterMixin(JupyterMixin):
"""JupyterMixin adds a spurious newline to text, this fixes the issue."""

def _repr_mimebundle_(self, *args, **kwargs):
try:
bundle = super()._repr_mimebundle_(*args, **kwargs)
except Exception: # noqa: BLE001
return None

Check warning on line 50 in ibis/expr/types/core.py

View check run for this annotation

Codecov / codecov/patch

ibis/expr/types/core.py#L49-L50

Added lines #L49 - L50 were not covered by tests
else:
bundle["text/plain"] = bundle["text/plain"].rstrip()
return bundle


def _capture_rich_renderable(
renderable: RenderableType, *, force_terminal: bool | None = None
Expand Down Expand Up @@ -67,19 +87,6 @@ def __repr__(self) -> str:
def _repr_pretty_(self, p, cycle):
p.text(_capture_rich_renderable(self))

Check warning on line 88 in ibis/expr/types/core.py

View check run for this annotation

Codecov / codecov/patch

ibis/expr/types/core.py#L88

Added line #L88 was not covered by tests

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
4 changes: 2 additions & 2 deletions ibis/expr/types/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from ibis.common.deferred import Deferred, _, deferrable
from ibis.common.grounds import Singleton
from ibis.expr.rewrites import rewrite_window_input
from ibis.expr.types.core import Expr, _binop, _is_null_literal
from ibis.expr.types.core import Expr, _binop, _FixedTextJupyterMixin, _is_null_literal
from ibis.util import deprecated, promote_list, warn_deprecated

if TYPE_CHECKING:
Expand Down Expand Up @@ -1367,7 +1367,7 @@ def _repr_html_(self) -> str | None:


@public
class Column(Value):
class Column(Value, _FixedTextJupyterMixin):
# Higher than numpy & dask objects
__array_priority__ = 20

Expand Down
4 changes: 2 additions & 2 deletions ibis/expr/types/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from ibis.common.deferred import Deferred, Resolver
from ibis.common.selectors import Expandable, Selector
from ibis.expr.rewrites import DerefMap
from ibis.expr.types.core import Expr
from ibis.expr.types.core import Expr, _FixedTextJupyterMixin
from ibis.expr.types.generic import Value, literal
from ibis.expr.types.temporal import TimestampColumn
from ibis.util import deprecated
Expand Down Expand Up @@ -140,7 +140,7 @@ def unwrap_aliases(values: Iterator[ir.Value]) -> Mapping[str, ir.Value]:


@public
class Table(Expr):
class Table(Expr, _FixedTextJupyterMixin):
"""An immutable and lazy dataframe.
Analogous to a SQL table or a pandas DataFrame. A table expression contains
Expand Down

0 comments on commit 9ab3590

Please sign in to comment.