-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaching.py
85 lines (72 loc) · 2.61 KB
/
caching.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
from dash import Dash, html, dcc, Input, Output
import datetime as dt
import numpy as np
import pandas as pd
from dash.dependencies import Input, Output
from flask_caching import Cache
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = Dash(__name__, external_stylesheets=external_stylesheets)
cache = Cache(app.server, config={
'CACHE_TYPE': 'filesystem',
'CACHE_DIR': 'cache-directory'
})
TIMEOUT = 60
@cache.memoize(timeout=TIMEOUT)
def query_data():
# This could be an expensive data querying step
np.random.seed(0) # no-display
df = pd.DataFrame(
np.random.randint(0, 100, size=(100, 4)),
columns=list('ABCD')
)
now = dt.datetime.now()
df['time'] = [now - dt.timedelta(seconds=5*i) for i in range(100)]
return df.to_json(date_format='iso', orient='split')
def dataframe():
return pd.read_json(query_data(), orient='split')
app.layout = html.Div([
html.Div('Data was updated within the last {} seconds'.format(TIMEOUT)),
dcc.Dropdown(dataframe().columns, 'A', id='live-dropdown'),
dcc.Graph(id='live-graph')
])
@app.callback(Output('live-graph', 'figure'),
Input('live-dropdown', 'value'))
def update_live_graph(value):
df = dataframe()
now = dt.datetime.now()
return {
'data': [{
'x': df['time'],
'y': df[value],
'line': {
'width': 1,
'color': '#0074D9',
'shape': 'spline'
}
}],
'layout': {
# display the current position of now
# this line will be between 0 and 60 seconds
# away from the last datapoint
'shapes': [{
'type': 'line',
'xref': 'x', 'x0': now, 'x1': now,
'yref': 'paper', 'y0': 0, 'y1': 1,
'line': {'color': 'darkgrey', 'width': 1}
}],
'annotations': [{
'showarrow': False,
'xref': 'x', 'x': now, 'xanchor': 'right',
'yref': 'paper', 'y': 0.95, 'yanchor': 'top',
'text': 'Current time ({}:{}:{})'.format(
now.hour, now.minute, now.second),
'bgcolor': 'rgba(255, 255, 255, 0.8)'
}],
# aesthetic options
'margin': {'l': 40, 'b': 40, 'r': 20, 't': 10},
'xaxis': {'showgrid': False, 'zeroline': False},
'yaxis': {'showgrid': False, 'zeroline': False}
}
}
if __name__ == '__main__':
app.run_server(debug=True)