maxschulz-COL commited on
Commit
987be42
1 Parent(s): 5938311

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -0
app.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Dev app to try things out."""
2
+ import vizro.models as vm
3
+ import vizro.plotly.express as px
4
+ import plotly.graph_objects as go
5
+ from vizro import Vizro
6
+ from vizro.tables import dash_ag_grid
7
+ from dash import Input, Output, State, callback, no_update, ctx
8
+ import pandas as pd
9
+
10
+ df = pd.DataFrame({
11
+ "date": pd.Series(dtype="datetime64[ns]"),
12
+ "value": pd.Series(dtype="float")
13
+ })
14
+
15
+ columnDefs = [
16
+ {"headerName": "Date", "field": "date", "editable": True, "cellEditor": "datePicker","checkboxSelection": True},
17
+ {"headerName": "Value", "field": "value", "editable": True, "cellEditor": "numericEditor"},
18
+ ]
19
+
20
+ empty_figure = go.Figure(
21
+ layout={
22
+ "title": "Timeline of input data",
23
+ "paper_bgcolor": "rgba(0,0,0,0)",
24
+ "plot_bgcolor": "rgba(0,0,0,0)",
25
+ "xaxis": {"visible": False},
26
+ "yaxis": {"visible": False},
27
+ }
28
+ )
29
+
30
+ #update the chart based on the edited table
31
+ @callback(
32
+ Output("output_chart", "figure", allow_duplicate=True),
33
+ Input("__input_editing-grid2", "cellValueChanged"),
34
+ Input("__input_editing-grid2", "rowData"),
35
+ Input("theme_selector", "checked"),
36
+ prevent_initial_call=True
37
+ )
38
+ def update(_, rows,theme_selector):
39
+ dff = pd.DataFrame(rows)
40
+ if dff.empty:
41
+ return empty_figure
42
+ fig = px.line(dff,title = "Timeline of input data", x="date", y="value")
43
+ fig.update_layout(template="vizro_light" if theme_selector else "vizro_dark") #to get the theme right
44
+ return fig
45
+
46
+ # add or delete rows of table
47
+ @callback(
48
+ Output("__input_editing-grid2", "deleteSelectedRows"),
49
+ Output("__input_editing-grid2", "rowData"),
50
+ Input("delete-row-btn", "n_clicks"),
51
+ Input("add-row-btn", "n_clicks"),
52
+ State("__input_editing-grid2", "rowData"),
53
+ prevent_initial_call=True,
54
+ )
55
+ def update_dash_table(n_dlt, n_add, data):
56
+ if ctx.triggered_id == "add-row-btn":
57
+ new_row = {
58
+ "date": ["2020-01-01"],
59
+ "value": [0.0]
60
+ }
61
+ df_new_row = pd.DataFrame(new_row)
62
+ updated_table = pd.concat([pd.DataFrame(data), df_new_row])
63
+ return False, updated_table.to_dict("records")
64
+
65
+ elif ctx.triggered_id == "delete-row-btn":
66
+ return True, no_update
67
+
68
+ page = vm.Page(
69
+ title="Example of adding rows to AG Grid and updating a chart based on the edited table",
70
+ layout=vm.Layout(grid = [
71
+ [0,0,0,0,1,1,1,1],
72
+ [0,0,0,0,1,1,1,1],
73
+ [0,0,0,0,1,1,1,1],
74
+ [2,3,-1,-1,-1,-1,-1,-1],
75
+ ] ),
76
+ components=[
77
+ vm.AgGrid(
78
+ id = "editing-grid2",
79
+ title="Editable Table / AG Grid",
80
+ figure=dash_ag_grid(
81
+ data_frame=df,
82
+ columnDefs=columnDefs,
83
+ defaultColDef={"editable": True},
84
+ columnSize="sizeToFit",
85
+ dashGridOptions={"rowSelection": "multiple", "suppressRowClickSelection": True},
86
+ ),
87
+ ),
88
+ vm.Graph(id = "output_chart",figure = px.line(df, title = "Timeline of input data",x="date", y="value")),
89
+ vm.Button(
90
+ id="delete-row-btn",
91
+ text="Delete row",
92
+ ),
93
+ vm.Button(
94
+ id="add-row-btn",
95
+ text="Add row",
96
+ ),
97
+ ],
98
+ )
99
+
100
+ dashboard = vm.Dashboard(pages=[page])
101
+
102
+ app = Vizro().build(dashboard).dash