Download app.py from maxschulz-COL/example-editable-table: direct link, hf CLI and curl.
- Browser
- Download file 3.49 kB
-
https://huggingface.co/spaces/maxschulz-COL/example-editable-table/resolve/main/app.py
- Command line
-
hf download hf://spaces/maxschulz-COL/example-editable-table/app.py
-
curl -L -o app.py https://huggingface.co/spaces/maxschulz-COL/example-editable-table/resolve/main/app.py
3.49 kB
| """Dev app to try things out.""" | |
| import vizro.models as vm | |
| import vizro.plotly.express as px | |
| import plotly.graph_objects as go | |
| from vizro import Vizro | |
| from vizro.tables import dash_ag_grid | |
| from dash import Input, Output, State, callback, no_update, ctx | |
| import pandas as pd | |
| df = pd.DataFrame({ | |
| "date": pd.Series(dtype="datetime64[ns]"), | |
| "value": pd.Series(dtype="float") | |
| }) | |
| columnDefs = [ | |
| {"headerName": "Date", "field": "date", "editable": True, "cellEditor": "datePicker","checkboxSelection": True}, | |
| {"headerName": "Value", "field": "value", "editable": True, "cellEditor": "numericEditor"}, | |
| ] | |
| empty_figure = go.Figure( | |
| layout={ | |
| "title": "Timeline of input data", | |
| "paper_bgcolor": "rgba(0,0,0,0)", | |
| "plot_bgcolor": "rgba(0,0,0,0)", | |
| "xaxis": {"visible": False}, | |
| "yaxis": {"visible": False}, | |
| } | |
| ) | |
| #update the chart based on the edited table | |
| def update(_, rows,theme_selector): | |
| dff = pd.DataFrame(rows) | |
| if dff.empty: | |
| return empty_figure | |
| fig = px.line(dff, x="date", y="value") | |
| fig.update_layout(template="vizro_light" if theme_selector else "vizro_dark") #to get the theme right | |
| return fig | |
| # add or delete rows of table | |
| def update_dash_table(n_dlt, n_add, data): | |
| if ctx.triggered_id == "add-row-btn": | |
| new_row = { | |
| "date": ["2020-01-01"], | |
| "value": [0.0] | |
| } | |
| df_new_row = pd.DataFrame(new_row) | |
| updated_table = pd.concat([pd.DataFrame(data), df_new_row]) | |
| return False, updated_table.to_dict("records") | |
| elif ctx.triggered_id == "delete-row-btn": | |
| return True, no_update | |
| page = vm.Page( | |
| title="Example of adding rows to AG Grid and updating a chart based on the edited table", | |
| layout=vm.Grid(grid = [ | |
| [0,0,0,0,1,1,1,1], | |
| [0,0,0,0,1,1,1,1], | |
| [0,0,0,0,1,1,1,1], | |
| [2,3,-1,-1,-1,-1,-1,-1], | |
| ] ), | |
| components=[ | |
| vm.AgGrid( | |
| id = "editing-grid2", | |
| title="Edit the Table by adding rows and changing the Date and Value column", | |
| figure=dash_ag_grid( | |
| data_frame=df, | |
| columnDefs=columnDefs, | |
| defaultColDef={"editable": True}, | |
| columnSize="sizeToFit", | |
| dashGridOptions={"rowSelection": "multiple", "suppressRowClickSelection": True}, | |
| ), | |
| ), | |
| vm.Graph(id = "output_chart",figure = px.line(df,x="date", y="value"),title="See the results of editable table here"), | |
| vm.Button( | |
| id="delete-row-btn", | |
| text="Delete row", | |
| ), | |
| vm.Button( | |
| id="add-row-btn", | |
| text="Add row", | |
| ), | |
| ], | |
| ) | |
| dashboard = vm.Dashboard(pages=[page]) | |
| app = Vizro().build(dashboard) | |
| server = app.dash.server | |
| if __name__ == "__main__": | |
| app.run() | |