Skip to content

Commit

Permalink
Add formatter funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Kuethe committed Sep 24, 2024
1 parent 7645f87 commit 4adf47f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
12 changes: 11 additions & 1 deletion docs/examples/getting_started/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
27 changes: 20 additions & 7 deletions pytabulator/tabulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
2 changes: 1 addition & 1 deletion tests/test_tabulator_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 4adf47f

Please sign in to comment.