Skip to content

Commit

Permalink
Add Tabulator types
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Nov 15, 2024
1 parent 19b786f commit e051df1
Showing 1 changed file with 38 additions and 9 deletions.
47 changes: 38 additions & 9 deletions panel/widgets/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from functools import partial
from types import FunctionType, MethodType
from typing import (
TYPE_CHECKING, Any, Callable, ClassVar, Mapping, Optional,
TYPE_CHECKING, Any, Callable, ClassVar, Literal, Mapping, NotRequired,
Optional, TypedDict,
)

import numpy as np
Expand Down Expand Up @@ -47,6 +48,34 @@
CellClickEvent, SelectionEvent, TableEditEvent,
)

class FilterSpec(TypedDict):
headerFilter: NotRequired[str | bool]
headerFilterParams: NotRequired[dict[str, Any]]
headerFilterFunc: NotRequired[str]
headerFilterPlaceholder: NotRequired[str]

class ColumnSpec(TypedDict):
editable: NotRequired[bool]
editor: NotRequired[str | CellEditor]
editorParams: NotRequired[dict[str, Any]]
field: NotRequired[str]
frozen: NotRequired[bool]
headerHozAlign: NotRequired[Literal["center", "left", "right"]]
headerSort: NotRequired[bool]
headerTooltip: NotRequired[str]
hozAlign: NotRequired[Literal["center", "left", "right"]]
formatter: NotRequired[str | CellFormatter]
formatterParams: NotRequired[dict[str, Any]]
sorter: NotRequired[str]
title: NotRequired[str]
titleFormatter: NotRequired[str | CellFormatter]
titleFormatterParams: NotRequired[dict[str, Any]]
width: NotRequired[str | int]

class GroupSpec(TypedDict):
columns: list[ColumnSpec]
title: str


def _convert_datetime_array_ignore_list(v):
if isinstance(v, np.ndarray):
Expand Down Expand Up @@ -250,12 +279,12 @@ def _get_column_definitions(self, col_names: list[str], df: pd.DataFrame) -> lis
else:
if isinstance(formatter, CellFormatter):
formatter = clone_model(formatter)
if hasattr(formatter, 'text_align'):
if formatter and hasattr(formatter, 'text_align'):
default_text_align = type(formatter).text_align.class_default(formatter) == formatter.text_align
else:
default_text_align = True

if not hasattr(formatter, 'text_align'):
if not formatter or not hasattr(formatter, 'text_align'):
pass
elif isinstance(self.text_align, str):
formatter.text_align = self.text_align
Expand Down Expand Up @@ -1827,8 +1856,8 @@ def _get_model(
self._register_events('cell-click', 'table-edit', 'selection-change', model=model, doc=doc, comm=comm)
return model

def _get_filter_spec(self, column: TableColumn) -> dict[str, Any]:
fspec = {}
def _get_filter_spec(self, column: TableColumn) -> FilterSpec:
fspec: FilterSpec = {}
if not self.header_filters or (isinstance(self.header_filters, dict) and
column.field not in self.header_filters):
return fspec
Expand Down Expand Up @@ -1895,10 +1924,10 @@ def _get_filter_spec(self, column: TableColumn) -> dict[str, Any]:
fspec['headerFilterPlaceholder'] = filter_placeholder
return fspec

def _config_columns(self, column_objs: list[TableColumn]) -> list[dict[str, Any]]:
def _config_columns(self, column_objs: list[TableColumn]) -> list[ColumnSpec | GroupSpec]:
column_objs = list(column_objs)
groups = {}
columns = []
groups: dict[str, GroupSpec] = {}
columns: list[ColumnSpec] = []
selectable = self.selectable
if self.row_content:
columns.append({
Expand Down Expand Up @@ -1945,7 +1974,7 @@ def _config_columns(self, column_objs: list[TableColumn]) -> list[dict[str, Any]
group for group, group_cols in grouping.items()
if field in group_cols
]
col_dict = dict(field=field)
col_dict: ColumnSpec = dict(field=field)
if isinstance(self.sortable, dict):
col_dict['headerSort'] = self.sortable.get(field, True)
elif not self.sortable:
Expand Down

0 comments on commit e051df1

Please sign in to comment.