St.context.cookies.get() works perfectly in local but collects an empty dict once deployed in Cloud Community

I am creating an application for a private event board game table reservation manager.
Since the users need to set an username in order to create/join a table I am trying to avoid them to set it every time.
For this reason I ended up into the Streamlit Extra Component for the CookieManager and as a first attempt, I just used CookieManager for both read and write usernames and it works apart from some additional rerun that the CookieManager triggers once instantiated.
Something like:

cookie_manager = stx.CookieManager() # ← triggers a useless rerun slowing down
st.session_state[‘username’] = cookie_manager.get(“username”)

# some lines later, when the users submits the (new) username:

cookie_manager.set(“username”, username, max_age=302460*60)

Since I don’t like the reruns I see at startup, I am trying now a mixed approach:

  • read the cookies using st.context.cookies.get(“username”)
  • set it into the cookies using:

cookie_manager = stx.CookieManager()
cookie_manager.set(“username”, username, max_age=302460*60)

And this approach works like a charm in my local environment (avoiding the initial annoying rerun) and if I print the whole st.context.cookies.to_dict() I get:

{'username-localhost-8888': '...', '_xsrf': '...', 'username': '...'}

Once deployed into the Stramlit Community Cloud, instead, the st.context.cookies.to_dict() returns an empty dict

{}

And the cookies are there in both the cases.
Deployed:


Local:

Do you know if I am doing something wrong or is there some issue with the st.context.cookies.to_dict() when deployed?

Hey @Marco5, this happens because Stremalit Cloud filters some headers and almost all cookies in the proxy layer. So, this is expected behavior when deployed on the Cloud.

Hello, thanks for the fast reply.
But if “Stremalit Cloud filters some headers and almost all cookies” how come CookieManager from Streamlit Extra Component works as expected in the Stremalit Cloud too (apart from the above mentioned additional rerun)?

Do you mean that the filter is performed at st.context.cookies layer?
And if so, since all the cookies are filtered out, what st.context.cookies can be used for in the Stremalit Cloud?

But if “Stremalit Cloud filters some headers and almost all cookies” how come CookieManager from Streamlit Extra Component works as expected in the Stremalit Cloud too (apart from the above mentioned additional rerun)?
I don’t know, but I assume that the CookieManager custom component reads cookie information in the front end and sends that data to the back end.

In the case of st.context, we directly read information about headers /cookies from the request, and the filtering happens before the information reaches the Streamlit app backend.

And if so, since all the cookies are filtered out, what st.context.cookies can be used for in the Stremalit Cloud?
At least right now it couldn’t be used for apps deployed on Community Cloud (and thanks for flagging this, we will mention it in documentation)!

Ok, makes sense! Hope this will be improved in the next future anyway

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.