Home > Net >  Save a cookie from callback function in Dash by Plotly
Save a cookie from callback function in Dash by Plotly

Time:05-04

Looked at the following post explaining how to store cookies: How to access a cookie from callback function in Dash by Plotly? I'm trying to replicate this and I'm not able to store/retrieve cookies.What is wrong in the simple example below ? There are no error messages, but when debugging, the all_cookies dict is empty, while I'd expect it to have at least one member 'dash cookie'.

@app.callback(
    Output(ThemeSwitchAIO.ids.switch("theme"), "value"),
    Input("url-login", "pathname"),
)
def save_load_cookie(value):
    dash.callback_context.response.set_cookie('dash cookie', '1 - cookie')
    all_cookies = dict(flask.request.cookies)
    return dash.no_update

Please note the app is running on my local machine via the standard flask server:

app.run_server(host='127.0.0.1', port=80, debug=True,
               use_debugger=False, use_reloader=False, passthrough_errors=True)

CodePudding user response:

Thank you @coralvanda, the callback needs to return a value instead of dash.no_update. Code should simply be:

@app.callback(
    Output(ThemeSwitchAIO.ids.switch("theme"), "value"),
    Input("url-login", "pathname"),
)
def save_load_cookie(value):
    dash.callback_context.response.set_cookie('dash cookie', '1 - cookie')
    all_cookies = dict(flask.request.cookies)
    return value
  • Related