Ah, yes. In my first take at the implementation, the cached callbacks didn’t support cached inputs. This should be fixed now (0.0.19rc4). If you update to this version, the following filtering example should work,
import dash
import dash_core_components as dcc
import dash_html_components as html
import time
import plotly
from dash.dependencies import Output, Input
from dash_extensions.callback import CallbackCache, DiskCache
data_sets = ["gapminder", "tips", "iris", "wind", "election"]
# Create app.
app = dash.Dash(prevent_initial_callbacks=True)
app.layout = html.Div([
dcc.Dropdown(options=[{"label": ds, "value": ds} for ds in data_sets], id="data_dd"), dcc.Dropdown(id="filter_dd"),
html.H1("Raw data"),
html.Div(id="raw_output"),
html.H1("Filtered data"),
html.Div(id="filtered_output"),
dcc.Loading(dcc.Store(id="raw_store"), fullscreen=True, type="dot"),
dcc.Loading(dcc.Store(id="filtered_store"), fullscreen=True, type="graph")
])
# Create (server side) disk cache.
cc = CallbackCache(cache=DiskCache(cache_dir="cache_dir"), expire_after=0)
@cc.cached_callback(Output("raw_store", "data"), [Input("data_dd", "value")])
def fetch_data(key):
df = plotly.data._get_dataset(key) if key is not None else None
time.sleep(1) # sleep to emulate a database call / a long calculation
return df
@cc.callback(Output("raw_output", "children"), [Input("raw_store", "data")])
def print_raw(df):
return df.iloc[:5].to_json()
@cc.callback([Output("filter_dd", "options"), Output("filter_dd", "value")], [Input("raw_store", "data")])
def update_filter_dd(df):
return [{"label": col, "value": col} for col in df.columns], None
@cc.cached_callback(Output("filtered_store", "data"), [Input("raw_store", "data"), Input("filter_dd", 'value')])
def filter_data(df, filter):
time.sleep(1) # sleep to emulate a database call / a long calculation
return df if filter is None else df[filter]
@cc.callback(Output("filtered_output", "children"), [Input("filtered_store", "data")])
def print_filtered(df):
return df.iloc[:5].to_json()
cc.register(app)
if __name__ == '__main__':
app.run_server()