Hello!
I’ve been thinking about if and how there is a solution to the following situation: In a multi-user app where the “global” data query/processing is expensive, I’ve found server side caching (example 4 in the documentation) to be very useful.
What I’m facing now is the same situation, but where the expensive data prep depends on user input. Is there any way to approach this? How can I make the cached user data update based on changes to the input?
Below is some code I’m playing with based on the example in the documentation. At the moment, however, what happens is that the data loads on page load/refresh and returns nothing since the input is empty. But my callback is failing to reload the data when the input changes.
Any ideas/hints would be appreciated
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)