From 4adcccb294c4e515d62ac8ef130b7d18774938f9 Mon Sep 17 00:00:00 2001 From: Stefan Kuethe Date: Thu, 31 Oct 2024 08:08:30 +0100 Subject: [PATCH] Add generic method to update columns --- pytabulator/formatters.py | 9 ++++++++- pytabulator/tabulator.py | 11 +++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/pytabulator/formatters.py b/pytabulator/formatters.py index 29500b6..eab2774 100644 --- a/pytabulator/formatters.py +++ b/pytabulator/formatters.py @@ -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)) @@ -16,6 +20,7 @@ def to_dict(self) -> dict: def name(self) -> str: return "" + class StarFormatter(Formatter): stars: Optional[int] = None @@ -23,11 +28,13 @@ class StarFormatter(Formatter): 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: diff --git a/pytabulator/tabulator.py b/pytabulator/tabulator.py index d584593..83e3a8f 100644 --- a/pytabulator/tabulator.py +++ b/pytabulator/tabulator.py @@ -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,