Saving data in local storage via streamlit

Hi, I was wondering if there was a way to store some fields in the browser local storage of a client and access them through streamlit. I want to be able to repopulate certain fields if the user closes my form and opens it after some time.

If this isn’t feasible is using cookies my best option and is there a good way of setting and updating cookie keys via streamlit?

Hi @pramodith, welcome to the forum. There’s st.experimental_set_query_params and st.experimental_get_query_params that allows you to send field information via query parameters in the browser’s URL bar.

More info here:

1 Like

Hey @dataprofessor thanks for the pointer but I’m afraid I’m looking for something else. In my application the user should be able to close the url to my application, but on returning certain fields of the user need to be restored. The application does not have a login feature so we want to store these fields in the clients browser via local storage or through cookies.

Hi @pramodith

Would this Streamlit component be useful? It’s intended to help you manage your client’s cookies streamlit-cookies-manager · PyPI

Let us know! :relaxed:

2 Likes

Sir, I had a similar doubt. Can these be used for this? Or is there another way? I have not deployed my app yet, if I deploy it, will I be able to keep track of some ss variables from all the open instances at a central location?

Hi @arnaud I’ve tried using this module but I keep running into an error. I’ve written this simple function to update any of the keys of the cookie.

def update_cookie_state(key, value):
    cookie_manager_obj = get_manager()
    tomorrow = datetime.datetime.now() + datetime.timedelta()
    if cookie_manager_obj.get(key):
        cookie_manager_obj.delete(key)
    cookie_manager_obj.set(key, value, expires_at=datetime.datetime(year=tomorrow.year, month=tomorrow.month, day=tomorrow.day))

However, when I try running this I get the following error :

streamlit.errors.DuplicateWidgetID: There are multiple identical st.extra_streamlit_components.CookieManager.cookie_manager widgets with
key='set'.
To fix this, please make sure that the key argument is unique for
each st.extra_streamlit_components.CookieManager.cookie_manager you create.

I’m not sure which key is not unique in this case.

@Ishan_Mistry I answered in your thread, I think a fairly common solution to your problem is a database!