From b40bec4789add09c5ec376d6a4ddc2a41f85d6b6 Mon Sep 17 00:00:00 2001 From: Stefan Kuethe Date: Thu, 11 Apr 2024 09:30:25 +0200 Subject: [PATCH] Add example --- docs/examples/edit_data/app.py | 41 ++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 docs/examples/edit_data/app.py diff --git a/docs/examples/edit_data/app.py b/docs/examples/edit_data/app.py new file mode 100644 index 0000000..bc358f9 --- /dev/null +++ b/docs/examples/edit_data/app.py @@ -0,0 +1,41 @@ +import pandas as pd +from pytabulator import ( + TableOptions, + Tabulator, + TabulatorContext, + output_tabulator, + render_tabulator, +) +from pytabulator.utils import create_columns +from shiny import App, reactive, render, ui + +df = pd.DataFrame({"id": [1, 2, 3], "name": ["Hans", "Peter", "Hanna"]}) +table_options = TableOptions(columns=create_columns(df, default_editor=True)) + +app_ui = ui.page_auto( + ui.h1("Edit data and submit changes", style="padding-top: 10px;"), + output_tabulator("tabulator"), + ui.div(ui.input_action_button("submit", "Submit data"), style="padding-top: 10px;"), +) + + +def server(input, output, session): + @render_tabulator + def tabulator(): + return Tabulator(df, table_options) + + @reactive.Effect + @reactive.event(input.submit) + async def trigger_get_data(): + async with TabulatorContext("tabulator") as table: + print("get data") + table.trigger_get_data() + + @reactive.Effect + @reactive.event(input.tabulator_data) + def tabulator_data(): + df_submitted = pd.DataFrame(input.tabulator_data()) + print(df_submitted) + + +app = App(app_ui, server)