forked from plotly/dash-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dash-cached-data-hidden-div.py
50 lines (38 loc) · 1.05 KB
/
dash-cached-data-hidden-div.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
import dash
from dash.dependencies import Input, Output
import dash_html_components as html
import dash_core_components as dcc
import pandas as pd
df = pd.DataFrame({
'x': [1, 2, 3, 4, 5, 6],
'y': [3, 1, 2, 3, 5, 6],
'z': ['A', 'A', 'B', 'B', 'C', 'C']
})
app = dash.Dash()
app.layout = html.Div([
dcc.Dropdown(
id='dropdown',
options=[{'label': i, 'value': i} for i in ['A', 'B', 'C']],
value='A'
),
dcc.Graph(
id='graph'
),
html.Div(id='cache', style={'display': 'none'})
])
@app.callback(Output('cache', 'children'), [Input('dropdown', 'value')])
def update_cache(value):
filtered_df = df[df['z'] == value]
return filtered_df.to_json()
@app.callback(Output('graph', 'figure'), [Input('cache', 'children')])
def update_graph(cached_data):
filtered_df = pd.read_json(cached_data)
return {
'data': [{
'x': filtered_df['x'],
'y': filtered_df['y'],
'type': 'bar'
}]
}
if __name__ == '__main__':
app.run_server(debug=True)