forked from plotly/dash-recipes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdash-cache-layout.py
54 lines (43 loc) · 1.36 KB
/
dash-cache-layout.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
import dash
from dash.dependencies import Input, Output
import dash_html_components as html
import dash_core_components as dcc
import datetime
import os
from flask_caching import Cache
app = dash.Dash(__name__)
cache = Cache(app.server, config={
'CACHE_TYPE': 'filesystem',
'CACHE_DIR': 'cache'
})
app.config.supress_callback_exceptions = True
timeout = 20
@cache.memoize(timeout=timeout)
def compute_expensive_data():
return str(datetime.datetime.now())
def generate_layout():
expensive_data = compute_expensive_data()
return html.Div([
html.H3('Last updated at: ' + expensive_data),
html.Div(id='flask-cache-memoized-children'),
dcc.RadioItems(
id='flask-cache-memoized-dropdown',
options=[
{'label': 'Option {}'.format(i), 'value': 'Option {}'.format(i)}
for i in range(1, 4)
],
value='Option 1'
),
html.Div('Results are cached for {} seconds'.format(timeout))
])
app.layout = generate_layout
@app.callback(
Output('flask-cache-memoized-children', 'children'),
[Input('flask-cache-memoized-dropdown', 'value')])
@cache.memoize(timeout=timeout)
def render(value):
return 'Selected "{}" at "{}"'.format(
value, datetime.datetime.now().strftime('%H:%M:%S')
)
if __name__ == '__main__':
app.run_server(debug=True)