Skip to content

Commit

Permalink
feat(repr): show col and row count in rich repr
Browse files Browse the repository at this point in the history
  • Loading branch information
NickCrews committed Nov 22, 2024
1 parent 7440d28 commit 6868cd8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
4 changes: 4 additions & 0 deletions ibis/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class Interactive(Config):
Maximum depth for nested data types.
show_types : bool
Show the inferred type of value expressions in the interactive repr.
show_count: bool
For Columns and Tables, show the row count. This can be computationally
expensive and slow.
"""

Expand All @@ -76,6 +79,7 @@ class Interactive(Config):
max_string: int = 80
max_depth: int = 1
show_types: bool = True
show_count: bool = True


class Repr(Config):
Expand Down
6 changes: 5 additions & 1 deletion ibis/expr/types/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,7 @@ def preview(
max_length: int | None = None,
max_string: int | None = None,
max_depth: int | None = None,
show_count: bool | None = None,
console_width: int | float | None = None,
) -> rich.table.Table:
"""Print a subset as a single-column Rich Table.
Expand All @@ -1385,6 +1386,8 @@ def preview(
Maximum length for pretty-printed strings.
max_depth
Maximum depth for nested data types.
show_count
Show the row count. This can be computationally expensive and slow.
console_width
Width of the console in characters. If not specified, the width
will be inferred from the console.
Expand Down Expand Up @@ -1412,10 +1415,11 @@ def preview(
max_length=max_length,
max_string=max_string,
max_depth=max_depth,
show_count=show_count,
)
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)
return to_rich(self, options=options, console_width=console_width)

def __pyarrow_result__(
self,
Expand Down
15 changes: 14 additions & 1 deletion ibis/expr/types/pretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ def _to_rich_table(
options: Interactive,
console_width: int | float | None = None,
) -> rich.table.Table:
from ibis.expr import types as ir

console_width = console_width or float("inf")
max_rows = options.max_rows
max_columns = options.max_columns
Expand Down Expand Up @@ -411,7 +413,18 @@ def _to_rich_table(
if not next_flex_cols:
break

rich_table = rich.table.Table(padding=(0, 1, 0, 1))
if options.show_count:
# use underscore to be friendly to i18n and python REPL
nrows = f"{table.count().execute():_}"
else:
nrows = "…"
if isinstance(tablish, ir.Table):
dims = f"{orig_ncols:_} cols by {nrows} rows"
else:
dims = f"{nrows} rows"
rich_table = rich.table.Table(
title=dims, title_justify="left", padding=(0, 1, 0, 1)
)

# Configure the columns on the rich table.
for name, dtype, _, max_width in col_info:
Expand Down
4 changes: 4 additions & 0 deletions ibis/expr/types/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ def preview(
max_length: int | None = None,
max_string: int | None = None,
max_depth: int | None = None,
show_count: bool | None = None,
console_width: int | float | None = None,
) -> RichTable:
"""Return a subset as a Rich Table.
Expand All @@ -528,6 +529,8 @@ def preview(
Maximum length for pretty-printed strings
max_depth
Maximum depth for nested data types
show_count
Show the row count. This can be computationally expensive and slow.
console_width
Width of the console in characters. If not specified, the width
will be inferred from the console.
Expand Down Expand Up @@ -565,6 +568,7 @@ def preview(
max_length=max_length,
max_string=max_string,
max_depth=max_depth,
show_count=show_count,
)
overrides = {k: v for k, v in overrides.items() if v is not None}
options = ibis.options.repr.interactive.copy(**overrides)
Expand Down

0 comments on commit 6868cd8

Please sign in to comment.