Skip to content

Commit

Permalink
chore: revert name to .preview()
Browse files Browse the repository at this point in the history
  • Loading branch information
NickCrews committed Feb 6, 2024
1 parent 9528f2d commit a87d8c4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 29 deletions.
20 changes: 9 additions & 11 deletions ibis/expr/types/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from collections.abc import Iterable, Sequence
from typing import TYPE_CHECKING, Any, Literal

import rich
from public import public
from rich.table import Table as RichTable

import ibis
import ibis.common.exceptions as com
Expand All @@ -13,6 +13,7 @@
from ibis.common.deferred import Deferred
from ibis.common.grounds import Singleton
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 @@ -1294,18 +1295,18 @@ def __array__(self, dtype=None):
return self.execute().__array__(dtype)

def __interactive_rich_console__(self, console, options):
return console.render(self.to_rich(), options=options)
return console.render(to_rich_table(self), options=options)

def to_rich(
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,
) -> RichTable:
"""Convert to a Rich Table.
) -> 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
Expand Down Expand Up @@ -1342,18 +1343,15 @@ def to_rich(
│ … │
└────────┘
"""
from ibis.expr.types.pretty import to_rich_table

named = self.name(self.op().name)
projection = named.as_table()
return to_rich_table(
projection,
rt = to_rich_table(
self,
max_rows=max_rows,
max_length=max_length,
max_string=max_string,
max_depth=max_depth,
console_width=console_width,
)
rich.print(rt)

def __pyarrow_result__(
self, table: pa.Table, data_mapper: type[PyArrowData] | None = None
Expand Down
22 changes: 18 additions & 4 deletions ibis/expr/types/pretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@
import json
from functools import singledispatch
from math import isfinite
from typing import TYPE_CHECKING
from urllib.parse import urlparse

import rich
from rich.align import Align
from rich.console import Console
from rich.table import Table as RichTable
from rich.text import Text

import ibis
import ibis.expr.datatypes as dt
from ibis.common.exceptions import TranslationError
from ibis.expr.types import Table

if TYPE_CHECKING:
from rich.table import Table as RichTable

from ibis.expr.types import Column, Table


def pretty_repr(expr: ibis.Expr) -> str:
Expand Down Expand Up @@ -72,7 +76,7 @@ def _(dtype, values, **fmt_kwargs):


@format_values.register(dt.GeoSpatial)
def _(dtype, values):
def _(dtype, values, **fmt_kwargs):
import shapely

return _format_nested([None if v is None else shapely.from_wkb(v) for v in values])
Expand Down Expand Up @@ -272,7 +276,7 @@ def format_dtype(dtype):


def to_rich_table(
table: Table,
tablish: Table | Column,
*,
max_rows: int | None = None,
max_columns: int | None = None,
Expand All @@ -281,6 +285,8 @@ def to_rich_table(
max_depth: int | None = None,
console_width: int | float | None = None,
) -> RichTable:
from ibis.expr.types import Column, Table

if max_rows is None:
max_rows = ibis.options.repr.interactive.max_rows
if max_columns is None:
Expand All @@ -289,6 +295,14 @@ def to_rich_table(
console_width = float("inf")
show_types = ibis.options.repr.interactive.show_types

if isinstance(tablish, Column):
named = tablish.name(tablish.op().name)
table = named.as_table()
elif isinstance(tablish, Table):
table = tablish
else:
raise TypeError(f"Expected Table or Column, got {type(tablish)}")

orig_ncols = len(table.columns)

if console_width == float("inf"):
Expand Down
24 changes: 10 additions & 14 deletions ibis/expr/types/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from keyword import iskeyword
from typing import TYPE_CHECKING, Callable, Literal

import rich
import toolz
from public import public

Expand All @@ -22,6 +23,7 @@
from ibis.common.deferred import Deferred, Resolver
from ibis.expr.types.core import Expr, _FixedTextJupyterMixin
from ibis.expr.types.generic import literal
from ibis.expr.types.pretty import to_rich_table

if TYPE_CHECKING:
import pandas as pd
Expand Down Expand Up @@ -366,7 +368,7 @@ def __interactive_rich_console__(self, console, options):
width = options.max_width

try:
table = self.to_rich(console_width=width)
rt = to_rich_table(self, console_width=width)
except Exception as e:
# In IPython exceptions inside of _repr_mimebundle_ are swallowed to
# allow calling several display functions and choosing to display
Expand All @@ -384,22 +386,19 @@ def __interactive_rich_console__(self, console, options):
# This restriction is only present in IPython, not in other REPLs.
console.print_exception()
raise e
return console.render(table, options=options)
return console.render(rt, options=options)

return console.render(self.to_rich(console_width=width), options=options)

def to_rich(
def preview(
self,
*,
max_rows: int | None = None,
max_columns: int | None = None,
show_types: bool | None = None,
max_length: int | None = None,
max_string: int | None = None,
max_depth: int | None = None,
console_width: int | float | None = None,
) -> RichTable:
"""Pretty-print a preview using rich.
) -> 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
Expand All @@ -412,8 +411,6 @@ def to_rich(
Maximum number of rows to display
max_columns
Maximum number of columns to display
show_types
Whether to show column types in the 2nd row, under the column name
max_length
Maximum length for pretty-printed arrays and maps
max_string
Expand All @@ -434,31 +431,30 @@ def to_rich(
>>> t.preview(
... max_rows=3,
... max_columns=3,
... show_types=False,
... max_string=8,
... console_width=30,
... )
┏━━━━━━━━━┳━━━━━━━━━━┳━━━┓
┃ species ┃ island ┃ … ┃
┃ string ┃ string ┃ … ┃
┡━━━━━━━━━╇━━━━━━━━━━╇━━━┩
│ Adelie │ Torgers… │ … │
│ Adelie │ Torgers… │ … │
│ Adelie │ Torgers… │ … │
│ … │ … │ … │
└─────────┴──────────┴───┘
"""
from ibis.expr.types.pretty import to_rich_table

return to_rich_table(
rt = to_rich_table(
self,
max_columns=max_columns,
max_rows=max_rows,
show_types=show_types,
max_length=max_length,
max_string=max_string,
max_depth=max_depth,
console_width=console_width,
)
rich.print(rt)

def __getitem__(self, what):
"""Select items from a table expression.
Expand Down

0 comments on commit a87d8c4

Please sign in to comment.