From 4adf47f972cd2b16e3c4a77cafb5a4a87e2ddd6c Mon Sep 17 00:00:00 2001 From: Stefan Kuethe Date: Tue, 24 Sep 2024 11:49:53 +0200 Subject: [PATCH] Add formatter funcs --- docs/examples/getting_started/app.py | 12 +++++++++++- pytabulator/tabulator.py | 27 ++++++++++++++++++++------- tests/test_tabulator_columns.py | 2 +- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/docs/examples/getting_started/app.py b/docs/examples/getting_started/app.py index 6d7e018..ecd7287 100644 --- a/docs/examples/getting_started/app.py +++ b/docs/examples/getting_started/app.py @@ -15,7 +15,17 @@ def tabulator(): df = pd.read_csv( "https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv" ) - return Tabulator(df, options={"height": 311}) + """ + return Tabulator(df, options={"height": 311}).set_column_formatter( + "Pclass", "star", {"stars": 3}, hozAlign="center" + ) + """ + return ( + Tabulator(df) + .set_options(height=311) + .set_column_formatter_star("Pclass", 3) + .set_column_formatter_tick_cross("Survived", hozAlign="center") + ) @render.code async def txt(): diff --git a/pytabulator/tabulator.py b/pytabulator/tabulator.py index efba098..45691a2 100644 --- a/pytabulator/tabulator.py +++ b/pytabulator/tabulator.py @@ -49,7 +49,7 @@ def update_column(self, col_name: str, **kwargs: Any) -> Self: return self - def set_formatter( + def set_column_formatter( self, col_name: str, formatter: str, @@ -65,16 +65,29 @@ def set_formatter( ), ) + def set_column_formatter_star(self, column: str, stars: int, **kwargs) -> Self: + formatter_params = dict(stars=stars) + self.set_column_formatter( + column, "star", formatter_params, hozAlign="center", **kwargs + ) + return self + + def set_column_formatter_tick_cross(self, column, **kwargs) -> Self: + self.set_column_formatter(column, "tickCross", **kwargs) + return self + + def set_column_editor(self) -> Self: + return self + def set_options(self, **kwargs) -> Self: - pass + self._options = self._options.model_copy(update = kwargs) return self def to_dict(self) -> dict: - # TODO: Rename 'data' to ??? model!? - data = df_to_dict(self.df) - data["options"] = self._options.to_dict() - data["bindingOptions"] = dict(lang = "python") - return data + payload = df_to_dict(self.df) + payload["options"] = self._options.to_dict() + payload["bindingOptions"] = dict(lang="python") + return payload def to_html(self): pass diff --git a/tests/test_tabulator_columns.py b/tests/test_tabulator_columns.py index ec7ebc8..5cde48a 100644 --- a/tests/test_tabulator_columns.py +++ b/tests/test_tabulator_columns.py @@ -13,5 +13,5 @@ def test_tabulator_columns(persons): table = table.update_column("Name", editor = True) print(table.columns) - table = table.set_formatter("Age", "html", hozAlign="center") + table = table.set_column_formatter("Age", "html", hozAlign="center") print(table.columns)