Skip to content

Commit

Permalink
fix: include subset_frame for gt render
Browse files Browse the repository at this point in the history
  • Loading branch information
machow committed Aug 12, 2024
1 parent c8c40b9 commit 2dc6332
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
32 changes: 31 additions & 1 deletion reactable/_tbl_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from datetime import datetime, date, time
from functools import singledispatch
from typing import TYPE_CHECKING, Any, Union, Literal
from typing import TYPE_CHECKING, Any, Union, Literal, Optional
from typing_extensions import TypeAlias

from abc import ABC
Expand Down Expand Up @@ -163,3 +163,33 @@ def _(data: PdDataFrame) -> "list[str]":
@column_names.register
def _(data: SimpleFrame) -> "list[str]":
return list(data.columns)


# subset_frame --------------------------------------------------------
@singledispatch
def subset_frame(
data: DataFrameLike, row: Optional[list[int]], column: Optional[list[str]]
) -> DataFrameLike:
raise TypeError(f"Unsupported type: {type(data)}")


@subset_frame.register
def _(
data: PdDataFrame, rows: Optional[list[int]] = None, cols: Optional[list[str]] = None
) -> PdDataFrame:

cols_indx = slice(None) if cols is None else data.columns.get_indexer_for(cols)
rows_indx = slice(None) if rows is None else rows

return data.iloc[rows_indx, cols_indx]


@subset_frame.register
def _(
data: PlDataFrame, rows: Optional[list[int]] = None, cols: Optional[list[str]] = None
) -> PlDataFrame:

cols_indx = slice(None) if cols is None else cols
rows_indx = slice(None) if rows is None else rows

return data[rows_indx, cols_indx]
3 changes: 2 additions & 1 deletion reactable/render_gt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import ipywidgets

from great_tables import GT
from great_tables._tbl_data import n_rows, subset_frame
from great_tables._tbl_data import n_rows
from great_tables._helpers import random_id
from great_tables._text import _process_text
from great_tables._gt_data import ColInfoTypeEnum
Expand All @@ -15,6 +15,7 @@
from .models import Column, Language, Theme, ColGroup
from . import Reactable
from .tags import as_react_style, to_widget
from ._tbl_data import subset_frame

if TYPE_CHECKING:
from great_tables._gt_data import Locale, Spanners, Heading, Footnotes, SourceNotes
Expand Down

0 comments on commit 2dc6332

Please sign in to comment.