Skip to content

Commit

Permalink
refactor(repr): pass Interactive dataclass around, not raw values
Browse files Browse the repository at this point in the history
  • Loading branch information
NickCrews committed Nov 20, 2024
1 parent e43d986 commit 7440d28
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 46 deletions.
7 changes: 4 additions & 3 deletions ibis/expr/types/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1407,14 +1407,15 @@ def preview(
"""
from ibis.expr.types.pretty import to_rich

return to_rich(
self,
overrides = dict(
max_rows=max_rows,
max_length=max_length,
max_string=max_string,
max_depth=max_depth,
console_width=console_width,
)
overrides = {k: v for k, v in overrides.items() if v is not None}
options = ibis.options.repr.interactive.copy(**overrides)
return to_rich(self, options, console_width=console_width)

def __pyarrow_result__(
self,
Expand Down
56 changes: 17 additions & 39 deletions ibis/expr/types/pretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import ibis.expr.datatypes as dt

if TYPE_CHECKING:
from ibis.config import Interactive
from ibis.expr.types import Column, Expr, Scalar, Table


Expand Down Expand Up @@ -261,64 +262,41 @@ def format_dtype(dtype, max_string: int) -> Text:
def to_rich(
expr: Expr,
*,
max_rows: int | None = None,
max_columns: int | None = None,
max_length: int | None = None,
max_string: int | None = None,
max_depth: int | None = None,
console_width: int | float | None = None,
options: Interactive | None = None,
) -> Pretty:
"""Truncate, evaluate, and render an Ibis expression as a rich object."""
if options is None:
options = ibis.options.repr.interactive
from ibis.expr.types import Scalar

if isinstance(expr, Scalar):
return _to_rich_scalar(
expr, max_length=max_length, max_string=max_string, max_depth=max_depth
)
return _to_rich_scalar(expr, options)
else:
return _to_rich_table(
expr,
max_rows=max_rows,
max_columns=max_columns,
max_length=max_length,
max_string=max_string,
max_depth=max_depth,
console_width=console_width,
)
return _to_rich_table(expr, options, console_width=console_width)


def _to_rich_scalar(
expr: Scalar,
*,
max_length: int | None = None,
max_string: int | None = None,
max_depth: int | None = None,
) -> Pretty:
def _to_rich_scalar(expr: Scalar, options: Interactive) -> Pretty:
value = format_values(
expr.type(),
[expr.to_pyarrow().as_py()],
max_length=max_length or ibis.options.repr.interactive.max_length,
max_string=max_string or ibis.options.repr.interactive.max_string,
max_depth=max_depth or ibis.options.repr.interactive.max_depth,
max_length=options.max_length,
max_string=options.max_string,
max_depth=options.max_depth,
)[0]
return Panel(value, expand=False, box=box.SQUARE)


def _to_rich_table(
tablish: Table | Column,
*,
max_rows: int | None = None,
max_columns: int | None = None,
max_length: int | None = None,
max_string: int | None = None,
max_depth: int | None = None,
options: Interactive,
console_width: int | float | None = None,
) -> rich.table.Table:
max_rows = max_rows or ibis.options.repr.interactive.max_rows
max_columns = max_columns or ibis.options.repr.interactive.max_columns
console_width = console_width or float("inf")
max_string = max_string or ibis.options.repr.interactive.max_string
show_types = ibis.options.repr.interactive.show_types
max_rows = options.max_rows
max_columns = options.max_columns
max_string = options.max_string
show_types = options.show_types

table = tablish.as_table()
orig_ncols = len(table.columns)
Expand Down Expand Up @@ -360,9 +338,9 @@ def _to_rich_table(
formatted, min_width, max_width = format_column(
dtype,
result[name].to_pylist()[:max_rows],
max_length=max_length,
max_length=options.max_length,
max_string=max_string,
max_depth=max_depth,
max_depth=options.max_depth,
)
dtype_str = format_dtype(dtype, max_string)
if show_types and not isinstance(dtype, (dt.Struct, dt.Map, dt.Array)):
Expand Down
9 changes: 5 additions & 4 deletions ibis/expr/types/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,15 +559,16 @@ def preview(
"""
from ibis.expr.types.pretty import to_rich

return to_rich(
self,
max_columns=max_columns,
overrides = dict(
max_rows=max_rows,
max_columns=max_columns,
max_length=max_length,
max_string=max_string,
max_depth=max_depth,
console_width=console_width,
)
overrides = {k: v for k, v in overrides.items() if v is not None}
options = ibis.options.repr.interactive.copy(**overrides)
return to_rich(self, options=options, console_width=console_width)

@overload
def __getitem__(self, what: str | int) -> ir.Column: ...
Expand Down

0 comments on commit 7440d28

Please sign in to comment.