I'm using server side caching in my dash application and I've followed example 4 in the documentation. I'm using this to query and process the "global" data, this prepared dataset is then shared between different components in the application.
I'm trying to tweak the example such that the caching also depends and reloads on some user input.
I've tried with something like below which doesn't really make sense - what is the best approach to achieve what I'm after?
app = dash.Dash(__name__)
cache = Cache(app.server, config={
'CACHE_TYPE': 'redis',
'CACHE_TYPE': 'filesystem',
'CACHE_DIR': 'cache-directory',
'CACHE_THRESHOLD': 200
})
def get_dataframe(session_id, user_input_value):
@cache.memoize()
def query_and_serialize_data(session_id):
df = # Here I make my query based on user_input_value
# If there is no input I just return an empty df
return df.to_json()
return pd.read_json(query_and_serialize_data(session_id))
def serve_layout():
session_id = str(uuid.uuid4())
return html.Div([
html.Div(session_id, id='session-id', style={'display': 'none'}),
# User input
# My output which doesn't behave as expected
])
app.layout = serve_layout
# Callback related to the user input
@app.callback(
[
Output('my-output', 'output-component')
],
[
Input('session-id', 'children'),
Input('user-input', 'value')
]
)
def generate_output(session_id, user_input_value):
df = get_dataframe(session_id, user_input_value)
return # Output component
if __name__ == '__main__':
app.run_server(debug=True)