Skip to content

Commit

Permalink
Add method to update column
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Kuethe committed Sep 21, 2024
1 parent f30bde2 commit 39ed0d1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 23 deletions.
7 changes: 5 additions & 2 deletions pytabulator/shiny_bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

from ._types import TableOptions
from ._utils import df_to_dict
from .tabulator import Tabulator, jsonifiable_table_options
# from .tabulator import Tabulator, jsonifiable_table_options
from .tabulator import Tabulator

# from . import TableOptions

Expand Down Expand Up @@ -95,5 +96,7 @@ async def render(self) -> Jsonifiable:
# return {"values": value.values.tolist(), "columns": value.columns.tolist()}
# TODO: convert with js
data = df_to_dict(df)
data["options"] = jsonifiable_table_options(self.table_options)

# TODO: Fix this, func was removed
# data["options"] = jsonifiable_table_options(self.table_options)
return data
51 changes: 31 additions & 20 deletions pytabulator/tabulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,59 @@

from pandas import DataFrame

from ._types import TableOptions
from ._utils import df_to_dict
from typing import Self
from typing import Self, Any

# TODO: Move somewhere else!?
def jsonifiable_table_options(
table_options: TableOptions | dict,
) -> dict:
if isinstance(table_options, TableOptions):
return table_options.to_dict()

return table_options
from .tabulator_options import TabulatorOptions
from .utils import create_columns


class Tabulator(object):
"""Tabulator
Args:
df (DataFrame): A data frame.
table_options (TableOptions): Table options.
options (TabulatorOptions): Setup options.
"""

def __init__(
self,
df: DataFrame,
table_options: TableOptions | dict = {},
options: TabulatorOptions | dict = TabulatorOptions(),
) -> None:
self.df = df
# self.table_options = table_options
self._table_options = jsonifiable_table_options(table_options)
self._options = (
options
if isinstance(options, TabulatorOptions)
else TabulatorOptions(**options)
)
if not self._options.columns:
self._options.columns = create_columns(self.df)

@property
def columns(self) -> list[dict]:
return self._table_options["columns"]
return self._options.columns

def _find_column(self, col_name: str) -> tuple:
for i, col in enumerate(self.columns):
if col["field"] == col_name:
return i, col

return None, None

def update_column(self, col_name: str, **kwargs: Any) -> Self:
i, col = self._find_column(col_name)
if col is not None:
self._options.columns[i] = col | kwargs

return self

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

def to_dict(self) -> dict:
# TODO: Rename 'data' to ???
data = df_to_dict(self.df)
# data["options"] = jsonifiable_table_options(self.table_options)
data["options"] = self._table_options
data["options"] = self._options.to_dict()
return data
14 changes: 13 additions & 1 deletion tests/test_tabulator_columns.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
from pytabulator import Tabulator


def test_tabulator_columns(persons):
print(persons)
# print(persons)
table = Tabulator(persons)

print(table.columns)

col = table._find_column("Name")
print(col)

table = table.update_column("Name", editor = True)
print(table.columns)

0 comments on commit 39ed0d1

Please sign in to comment.