Skip to content

Commit

Permalink
feat(ux): add Table and Column.to_rich()
Browse files Browse the repository at this point in the history
  • Loading branch information
NickCrews committed Feb 29, 2024
1 parent 3d94221 commit 6e1c8e7
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 60 deletions.
19 changes: 4 additions & 15 deletions ibis/expr/types/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import ibis.expr.operations as ops
from ibis.common.annotations import ValidationError
from ibis.common.exceptions import IbisError, TranslationError
from ibis.common.exceptions import IbisError
from ibis.common.grounds import Immutable
from ibis.common.patterns import Coercible, CoercionError
from ibis.config import _default_backend
Expand Down Expand Up @@ -78,20 +78,9 @@ def __repr__(self) -> str:
if not opts.interactive:
return self._repr()

from ibis.expr.types.pretty import simple_console

with simple_console.capture() as capture:
try:
simple_console.print(self)
except TranslationError as e:
lines = [
"Translation to backend failed",
f"Error message: {e!r}",
"Expression repr follows:",
self._repr(),
]
return "\n".join(lines)
return capture.get().rstrip()
from ibis.expr.types.pretty import pretty_repr

return pretty_repr(self)

def __reduce__(self):
return (self.__class__, (self._arg,))
Expand Down
62 changes: 59 additions & 3 deletions ibis/expr/types/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from collections.abc import Iterable, Sequence
from typing import TYPE_CHECKING, Any, Literal

import rich
from public import public

import ibis
Expand All @@ -14,6 +15,7 @@
from ibis.common.grounds import Singleton
from ibis.expr.rewrites import rewrite_window_input
from ibis.expr.types.core import Expr, _binop, _FixedTextJupyterMixin
from ibis.expr.types.pretty import to_rich_table
from ibis.util import deprecated

if TYPE_CHECKING:
Expand Down Expand Up @@ -1309,9 +1311,63 @@ def __array__(self, dtype=None):
return self.execute().__array__(dtype)

def __interactive_rich_console__(self, console, options):
named = self.name(self.op().name)
projection = named.as_table()
return console.render(projection, options=options)
return console.render(to_rich_table(self), options=options)

def preview(
self,
*,
max_rows: int | None = None,
max_length: int | None = None,
max_string: int | None = None,
max_depth: int | None = None,
console_width: int | float | None = None,
) -> None:
"""Print as a Rich Table.
This is an explicit version of what you get when you inspect
this object in interactive mode, except with this version you
can pass formatting options. The options are the same as those exposed
in `ibis.options.interactive`.
Parameters
----------
max_rows
Maximum number of rows to display
max_length
Maximum length for pretty-printed arrays and maps.
max_string
Maximum length for pretty-printed strings.
max_depth
Maximum depth for nested data types.
console_width
Width of the console in characters. If not specified, the width
will be inferred from the console.
Examples
--------
>>> import ibis
>>> ibis.options.interactive = False
>>> t = ibis.examples.penguins.fetch()
>>> t.island.to_rich(max_rows=3, max_string=5)
┏━━━━━━━━┓
┃ island ┃
┃ string ┃
┡━━━━━━━━┩
│ Torg… │
│ Torg… │
│ Torg… │
│ … │
└────────┘
"""
rt = to_rich_table(

Check warning on line 1362 in ibis/expr/types/generic.py

View check run for this annotation

Codecov / codecov/patch

ibis/expr/types/generic.py#L1362

Added line #L1362 was not covered by tests
self,
max_rows=max_rows,
max_length=max_length,
max_string=max_string,
max_depth=max_depth,
console_width=console_width,
)
rich.print(rt)

Check warning on line 1370 in ibis/expr/types/generic.py

View check run for this annotation

Codecov / codecov/patch

ibis/expr/types/generic.py#L1370

Added line #L1370 was not covered by tests

def __pyarrow_result__(
self, table: pa.Table, data_mapper: type[PyArrowData] | None = None
Expand Down
Loading

0 comments on commit 6e1c8e7

Please sign in to comment.