-
Notifications
You must be signed in to change notification settings - Fork 7
/
usage.py
151 lines (140 loc) · 4.9 KB
/
usage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from dash import Dash, dcc, html, Input, Output, State, MATCH, ALL, ctx, no_update, Patch
import plotly.express as px
import plotly.graph_objs as go
import dash_bootstrap_components as dbc
import dash_chart_editor as dce
df = px.data.iris()
app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
app.layout = dbc.Container(
[
dcc.Store(id='oldSum', data=0),
dbc.Modal([
dbc.ModalTitle('Customizing Charts'),
dbc.ModalBody([dcc.Input(id="chartId"),dce.DashChartEditor(dataSources=df.to_dict('list'), id='editor',
style={'height': '60vh'})]),
dbc.ModalFooter([dbc.Button('Save Chart', id='saveEditor'),
dbc.Button('Save & Close', id='saveCloseEditor', color='secondary')])
], id='editorMenu', size='xl'),
dbc.Row(
dbc.Col(
[
html.H3("Pattern Matching Callbacks Demo"),
dbc.Button(
"Add Chart", id="pattern-match-add-chart", n_clicks=0
),
html.Div(id="pattern-match-container", children=[], className="mt-4"),
]
)
),
],
fluid=True,
)
def make_card(n_clicks):
return dbc.Card(
[
dbc.CardHeader(
[
f"Figure {n_clicks + 1} ",
dbc.Button(
"Edit",
id={"type": "dynamic-edit", "index": n_clicks},
n_clicks=0,
color="info",
),
dbc.Button(
"X",
id={"type": "dynamic-delete", "index": n_clicks},
n_clicks=0,
color="secondary",
),
],
className="text-end",
),
dcc.Graph(
id={"type": "dynamic-output", "index": n_clicks},
style={"height": 400},
figure=go.Figure()
),
],
style={
"width": 400,
"display": "inline-block",
},
className="m-1",
id={"type": "dynamic-card", "index": n_clicks},
)
@app.callback(
Output("pattern-match-container", "children"),
Input("pattern-match-add-chart", "n_clicks"),
)
def add_card(n_clicks):
patched_children = Patch()
new_card = make_card(n_clicks)
patched_children.append(new_card)
return patched_children
@app.callback(
Output("pattern-match-container", "children", allow_duplicate=True),
Input({"type": "dynamic-delete", "index": ALL}, "n_clicks"),
State({"type": "dynamic-card", "index": ALL}, 'id'),
prevent_initial_call=True,
)
def remove_card(_, ids):
rem = Patch()
if ctx.triggered[0]['value'] > 0:
for i in range(len(ids)):
if ids[i]['index'] == ctx.triggered_id['index']:
del rem[i]
return rem
return no_update
@app.callback(
Output("editorMenu", "is_open"), Output("editor", "loadFigure"), Output('chartId', 'value'),
Output('oldSum', 'data'),
Input({"type": "dynamic-edit", "index": ALL}, "n_clicks"),
State({"type": "dynamic-output", "index": ALL}, "figure"),
State('oldSum', 'data'),
State({"type": "dynamic-card", "index": ALL}, 'id'),
prevent_initial_call=True,
)
def remove_card(edit, figs, oldSum, ids):
if sum(edit) > 0 and sum(edit) != oldSum:
oldSum = sum(edit)
if ctx.triggered[0]['value'] > 0:
for i in range(len(ids)):
if ids[i]['index'] == ctx.triggered_id['index']:
if figs[i]['data']:
return True, figs[i], ctx.triggered_id['index'], oldSum
return True, {'data': [], 'layout': {}}, ctx.triggered_id['index'], oldSum
return no_update, no_update, no_update, oldSum
@app.callback(
Output('editor', 'saveState'),
Input('saveEditor', 'n_clicks'),
Input('saveCloseEditor', 'n_clicks'),
)
def saveChart(n, n1):
if n or n1:
return True
@app.callback(
Output("editorMenu", "is_open", allow_duplicate=True),
Input('saveCloseEditor', 'n_clicks'),
prevent_initial_call=True
)
def saveChart(n):
if n:
return False
return no_update
@app.callback(
Output("pattern-match-container", "children", allow_duplicate=True),
Input('editor', 'figure'), State('chartId', 'value'),
State({"type": "dynamic-card", "index": ALL}, 'id'),
prevent_initial_call=True
)
def saveToFig(f, v, ids):
if f:
figs = Patch()
for i in range(len(ids)):
if ids[i]['index'] == v:
figs[i]['props']['children'][1]['props']['figure'] = dce.chartToPython(f, df)
return figs
return no_update
if __name__ == "__main__":
app.run_server(debug=True)