cache = Cache(app.server, config={
'CACHE_TYPE': 'filesystem',
'CACHE_DIR': 'cache-directory'
})
TIMEOUT = 60
@app.callback(Output("store1", "data"), [Input("Submit_button", "n_clicks")],
[State("project_id", "value"), State("start_time", "value"), State("end_time", "value")],
prevent_initial_call=True)
@cache.memoize(timeout=TIMEOUT)
def store_data(*args):
if not any(args):
raise PreventUpdate
else:
clicks, player_id, start_time, end_time = args
if clicks > 0:
try:
dff = json_to_df(player_id, start_time, end_time)
except Exception:
raise PreventUpdate
dff = dff.to_dict("records")
return dff
@app.callback([Output("line-graph", "figure"), Output("map-graph", "figure")],
[Input("dropdown", "value"), Input("store1", "data"), Input("euis", "value"),
Input("plot_button", "n_clicks"), Input("map-styles", "value"), Input("color-source", "value"), Input("lat_correction", "value"), Input("long_correction", "value")],
prevent_initial_call=True)
@cache.memoize(timeout=TIMEOUT)
def graph_output(*args):
if not any(args):
raise PreventUpdate
else:
MODE, DATA, eui, click, map_style, color_source,lat_correction,long_correction = args
if MODE is None:
raise PreventUpdate
DATA = dataframe(DATA)
# DATA = pd.DataFrame(DATA)
if click > 0:
DATA = DATA[DATA["Eui"] == eui]
if color_source == 'numSv':
cmax = 30
cmin = 5
legend_title = '#Sats'
color_source = 'numSv'
if color_source == 'hdop':
cmax = 200
cmin = 0
legend_title = 'HDOP'
color_source = 'hdop'
if color_source == 'RSSI':
cmax = 100
cmin = 0
legend_title = 'RSSI'
color_source = 'RSSI'
if MODE == "Bat_lvl":
fig = px.line(DATA, x="totaltime", y="Bat_lvl", labels=dict(x="Time", y="Bat_lvl"))
fig.update_layout(line_color='rgb(47, 168, 227)')
elif MODE == "RSSI":
DATA = DATA[DATA["RSSI"].notna()]
DATA = DATA.reset_index()
fig = px.line(DATA, x="totaltime", y="RSSI", labels=dict(x="Time", y="RSSI"))
elif MODE == "hdop":
DATA = DATA[DATA["hdop"].notna()]
DATA = DATA.reset_index()
fig = px.line(DATA, x="totaltime", y="hdop", labels=dict(x="Time", y="Hdop"))
elif MODE == "numSv":
DATA = DATA[DATA["hdop"].notna()]
DATA = DATA.reset_index()
fig = px.line(DATA, x="totaltime", y="numSv", labels=dict(x="Time", y="numSv"))
elif MODE == "speed":
DATA = DATA[DATA["speed"].notna()]
DATA = DATA.reset_index()
fig = px.line(DATA, x="totaltime", y="speed", labels=dict(x="Time", y="speed"))
elif MODE == "acc_load":
DATA = DATA[DATA["acc_load"].notna()]
DATA = DATA.reset_index()
fig = px.line(DATA, x="totaltime", y="acc_load", labels=dict(x="Time", y="acc_load"))
elif MODE == "heartrate":
DATA = DATA[DATA["heartrate"].notna()]
DATA = DATA.reset_index()
fig = px.line(DATA, x="totaltime", y="heartrate", labels=dict(x="Time", y="heartrate"))
fig_scatter = px.scatter_mapbox(lat=DATA["latitude"]+lat_correction, lon=DATA["longitude"]+long_correction,
color_continuous_scale=["black", "purple", "red"], zoom=20,
# center = dict(lat = g.center)
title='GNSS Technology Map')
# mapbox_style="open-street-map"
fig_scatter.update_layout(font_size=16, title={'xanchor': 'center', 'yanchor': 'top', 'y': 0.9, 'x': 0.5, },
title_font_size=24, mapbox_accesstoken=mapbox_access_token,
mapbox_style=map_style)
fig_scatter.update_traces(marker=dict(size=6, cmax=cmax, cmin=cmin, color=DATA[color_source].ffill(),colorbar=dict(
title=legend_title,
x=0.93,
xpad=0,
nticks=10,
tickfont=dict(color="#d8d8d8"),
titlefont=dict(color="#d8d8d8"),
thicknessmode="pixels",
)))
fig_scatter.write_image('images/scatter.png', engine="kaleido")
# mapbox_style = "mapbox://styles/strym/ckhd00st61aum19noz9h8y8kw"
save_images(DATA, eui)
# try:
# save_images(DATA, eui,map_style,color_source,cmax,cmin)
#
# except Exception:
# raise PreventUpdate
try:
return fig, fig_scatter
except Exception:
raise PreventUpdate
I suspect this doesn’t work because memoize makes it such that the cache is based on the parameters passed as well. In the first block, the input “n_clicks” is continuously increasing, making it different every time “Submit_button” is clicked, even if the other parameters are the same.
try changing the first cache decorator (above store_data)
@cache.memoize(timeout=TIMEOUT)
to
@cache.cached(timeout=TIMEOUT, key_prefix="store_data")
and the second (above graph_output)
@cache.memoize(timeout=TIMEOUT)
to:
@cache.cached(timeout=TIMEOUT, key_prefix="graph_output")