Skip to content

Commit

Permalink
Add new TabulatorOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Kuethe committed Sep 21, 2024
1 parent 6853e98 commit f30bde2
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/py-tabulator.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pytabulator/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from pandas import read_csv

def titanic():
pass
9 changes: 7 additions & 2 deletions pytabulator/tabulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from ._types import TableOptions
from ._utils import df_to_dict

from typing import Self

# TODO: Move somewhere else!?
def jsonifiable_table_options(
Expand Down Expand Up @@ -33,7 +33,12 @@ def __init__(
# self.table_options = table_options
self._table_options = jsonifiable_table_options(table_options)

def options(self, **kwargs) -> Tabulator:
@property
def columns(self) -> list[dict]:
return self._table_options["columns"]

# TODO: Rename to set_options
def options(self, **kwargs) -> Self:
self._table_options.update(kwargs)
return self

Expand Down
78 changes: 78 additions & 0 deletions pytabulator/tabulator_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from __future__ import annotations

from typing import Literal, Union, Optional

from pydantic import BaseModel, ConfigDict, Field, field_validator

class TabulatorOptions(BaseModel):
"""Tabulator options.
Attributes:
index (str, optional): The index of the table. Defaults to `id`.
header_visible (bool, optional): Whether to display the header of the table. Defaults to `True`.
movable_rows (bool, optional): Whether rows are movable or not. Defaults to `False`.
group_by: Columns to group by. Defaults to `None`.
height (int, optional): Height in px. Defaults to `300`.
pagination (bool, optional): Whether to enable pagination. Defaults to `False`.
pagination_counter (str, optional): Whether to display counted rows in footer. Defaults to `rows`.
pagination_add_row: Where to add rows when pagination is enabled. Defaults to `page`.
selectable_rows: Whether a row is selectable. An integer value sets the maximum number of rows, that can be selected.
If set to `highlight`, rows do not change state when clicked. Defaults to `highlight`.
columns (list, optional): Columns configuration. Defaults to `None`,
which means that the default configuration is used.
layout: The layout of the table. Defaults to `fitColumns`.
add_row_pos: Where to add rows. Defaults to `bottom`.
frozen_rows (int, optional): Number of frozen rows. Defaults to `Ǹone`.
row_height: Fixed height of rows. Defaults to `None`.
history (bool, optional): Whether to enable history. Must be set if `undo` and `redo` is used. Defaults to `False`.
Note:
See [Tabulator Setup Options](https://tabulator.info/docs/5.5/options) for details.
"""

index: str = "id"
header_visible: Optional[bool] = Field(True, serialization_alias="headerVisible")
movable_rows: Optional[bool] = Field(False, serialization_alias="movableRows")
group_by: Union[str, list, None] = Field(None, serialization_alias="groupBy")
height: Union[int, str, None] = None
pagination: Optional[bool] = False
pagination_counter: str = Field("rows", serialization_alias="paginationCounter")
pagination_add_row: Literal["page", "table"] = Field(
"page", serialization_alias="paginationAddRow"
)
selectable_rows: Union[str, bool, int] = Field(
"highlight", serialization_alias="selectableRows"
)
columns: Optional[list] = None
layout: Literal[
"fitData", "fitDataFill", "fitDataStretch", "fitDataTable", "fitColumns"
] = "fitColumns"
add_row_pos: Literal["bottom", "top"] = Field(
"bottom", serialization_alias="addRowPos"
)
frozen_rows: Optional[int] = Field(None, serialization_alias="frozenRows")
row_height: Optional[int] = Field(None, serialization_alias="rowHeight")
resizable_column_fit: Optional[bool] = Field(False, serialization_alias="resizableColumnFit")
history: Optional[bool] = False

# New features to be added in the next release
"""
responsiveLayout: str = "hide"
columnDefaults: dict = {"tooltip": True}
"""

model_config = ConfigDict(
validate_assignment=True,
extra="allow",
# use_enum_values=True
)

@field_validator("height")
def validate_height(cls, v):
if isinstance(v, int):
return f"{v}px"

return v

def to_dict(self) -> dict:
return self.model_dump(by_alias=True, exclude_none=True)
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import pytest
import pandas as pd

@pytest.fixture
def persons():
data = [["Peter", 10, 10.5], ["Hans", 12, 13.7]]
return pd.DataFrame(data, columns=["Name", "Age", "JustANumber"])
2 changes: 2 additions & 0 deletions tests/test_tabulator_columns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def test_tabulator_columns(persons):
print(persons)

0 comments on commit f30bde2

Please sign in to comment.