Access Cookie In Serve Layout Function

Hello everyone,

I am trying to initally populate a dropdown in my layout with a cookie. If a user has populated the dropdown with 20 items from their last visit, I would like them to be able to pickup where they left off. I am storing my cookies in the callback context as specified here.

@app.callback(Output('dropdown_container', 'value'),
             [Input('someothercomponent', 'clickData')])
def update_dropdown(clickData):
   #do something
            'mycookie', pickle.dumps(values)) 
   return values

I also have a dynamically loaded layout on refresh.

def get_dropdown():
    ...setup options from data...

    dd = dcc.Dropdown(
        value=['worldwide', 'COUNTRY_US:NONE', 'STATE_New York:US'],
        style={'position': 'relative', 'zIndex': '3', 'font-size': '75%'}
    return dd

def serve_dash_layout():
    return html.Div(id='root-container', children=get_dropdown())

server = flask.Flask(__name__)
app = dash.Dash(server=server,
app.layout = layouts.serve_dash_layout

Because the function get_dropdown() which is under serve_dash_layout creates the initial dropdown default values, I was wondering if it could have access to the mycookie that I setup in the callback the nexttime the user comes to my app. What global variables does serve_dash_layout have access to?

Thanks so much,

+1 having the same issue

For anyone else stumbling across this, I ended up finding this solution:

This allows you to pull of any cookies inside of callbacks.

So, just to complete this post with all the information that I was looking as well:

  1. to set cookies

     dash.callback_context.response.set_cookie('mycookie', myvalue)
  2. to retrieve the cookie that was set
    . . .
    import flask
    if ‘mycookie’ in allcookies:
    myvalue=allcookies[‘mycookie’] # I was unable to indent this…:frowning:

Thanks to all

thanks for sharing your solution!

Could someone tell me how I can set the cookie’s attribute’s “samesite”=“None” and “secure”=“true”?

I have to do this since I am embedding my application in an iFrame because currently they are not visible anymore once the application is embedded in an iFrame…

Any suggestions are welcome as I am looking for a solution to this for some time…



did you come across a solution for this?