/
three_figure_demo.py
110 lines (95 loc) · 2.88 KB
/
three_figure_demo.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
from dash import Dash, dcc, html, Input, Output, State, ctx, no_update
import plotly.express as px
import dash_bootstrap_components as dbc
import dash_chart_editor as dce
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/solar.csv")
app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP], prevent_initial_callbacks=True)
chart_editor_modal = dbc.Modal(
[
dbc.ModalTitle(["Editing Figure #", html.Span(id="chart-id")]),
dbc.ModalBody(
dce.DashChartEditor(
dataSources=df.to_dict("list"),
id="chart-editor",
),
),
dbc.ModalFooter(dbc.Button("Save & Close", id="save", color="secondary")),
],
id="editor-modal",
size="xl",
)
def make_card(card_number):
return dbc.Card(
[
dbc.CardHeader(
[
f"Figure {card_number} ",
dbc.Button(
"Edit",
id=f"edit-{card_number}",
color="info",
),
],
className="text-end",
),
dcc.Graph(
id=f"fig-{card_number}",
figure=px.bar(df, x="State", y="Number of Solar Plants"),
),
],
className="m-1",
)
app.layout = dbc.Container(
[
html.H3("Dash Chart Editor Demo"),
dbc.Row([dbc.Col(make_card(i)) for i in [1, 2, 3]]),
chart_editor_modal,
],
fluid=True,
)
@app.callback(
Output("editor-modal", "is_open"),
Output("chart-editor", "loadFigure"),
Output("chart-id", "children"),
Input("edit-1", "n_clicks"),
Input("edit-2", "n_clicks"),
Input("edit-3", "n_clicks"),
Input("save", "n_clicks"),
State("fig-1", "figure"),
State("fig-2", "figure"),
State("fig-3", "figure"),
)
def edit_fig(edit1, edit2, edit3, save, fig1, fig2, fig3):
triggered = ctx.triggered_id
chart_id = triggered[-1]
if triggered == "save":
return False, no_update, no_update
if triggered == "edit-1":
return True, fig1, chart_id
if triggered == "edit-2":
return True, fig2, chart_id
if triggered == "edit-3":
return True, fig3, chart_id
@app.callback(
Output("chart-editor", "saveState"),
Input("save", "n_clicks"),
)
def update_chart_editor_fig(n):
return True
@app.callback(
Output("fig-1", "figure"),
Output("fig-2", "figure"),
Output("fig-3", "figure"),
Input("chart-editor", "figure"),
State("chart-id", "children"),
)
def save_to_fig(dce_fig, fig_number):
if fig_number == "1":
return dce_fig, no_update, no_update
if fig_number == "2":
return no_update, dce_fig, no_update
if fig_number == "3":
return no_update, no_update, dce_fig
if __name__ == "__main__":
app.run_server(debug=True)