Skip to content

Commit

Permalink
Add generic method to update columns
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Kuethe committed Oct 31, 2024
1 parent b69f9a5 commit 4adcccb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pytabulator/formatters.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
from enum import Enum
from pydantic import BaseModel
from typing import Optional

from pydantic import BaseModel

from ._utils import as_camel_dict_recursive


class Formatters(Enum):
STAR = "star"
PROGRESS = "progress"
TICK_CROSS = "tickCross"


class Formatter(BaseModel):
def to_dict(self) -> dict:
return as_camel_dict_recursive(self.model_dump(exclude_none=True))
Expand All @@ -16,18 +20,21 @@ def to_dict(self) -> dict:
def name(self) -> str:
return ""


class StarFormatter(Formatter):
stars: Optional[int] = None

@property
def name(self) -> str:
return Formatters.STAR.value


class ProgressFormatter(Formatter):
@property
def name(self) -> str:
return Formatters.PROGRESS.value


class TickCrossFormatter(Formatter):
@property
def name(self) -> str:
Expand Down
11 changes: 11 additions & 0 deletions pytabulator/tabulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ def update_column(self, col_name: str | list, **kwargs: Any) -> Self:

return self

def update_column2(self, col_name, formatter: dict | Formatter = None, editor: dict | Editor = None, **kwargs: Any) -> Self:
if formatter is not None:
self.set_column_formatter(col_name, formatter)

if editor is not None:
self.set_column_editor(col_name, editor)

self._update_column(col_name, **kwargs)

return self

def set_column_formatter(
self,
col_name: str | list,
Expand Down

0 comments on commit 4adcccb

Please sign in to comment.