forked from plotly/dash-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dash-cache-signal-session.py
50 lines (40 loc) · 1.27 KB
/
dash-cache-signal-session.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_core_components as dcc
import dash_html_components as html
import datetime
from flask_caching import Cache
import pandas as pd
import time
import uuid
app = dash.Dash()
cache = Cache(app.server, config={
'CACHE_TYPE': 'filesystem',
'CACHE_DIR': 'cache-directory',
'CACHE_THRESHOLD': 50 # should be equal to maximum number of active users
})
@cache.memoize()
def query_data(session_id):
df = pd.DataFrame({'a': [datetime.datetime.now()]})
return df.to_json()
def dataframe(session_id):
return pd.read_json(query_data(session_id))
def serve_layout():
session_id = str(uuid.uuid4())
df = dataframe(session_id)
return html.Div([
html.Div(session_id, id='session-id', style={'display': 'none'}),
dcc.Dropdown(id='dropdown', value='a', options=[
{'label': i, 'value': i} for i in ['a', 'b', 'c', 'd']
]),
html.Pre(id='output')
])
app.layout = serve_layout
@app.callback(Output('output', 'children'),
[Input('dropdown', 'value'),
Input('session-id', 'children')])
def display_value(value, session_id):
df = dataframe(session_id)
return df.to_csv()
if __name__ == '__main__':
app.run_server(debug=True)