RANT -> Feeling very disillusioned building on Streamlit with lack of cookie support for cloud deployment

@brdemorin

The SSO token is persisted in the browser by default after the first login and not through streamlit, so the only thing your users need to do is click on the login button and they will be logged in without additional interactions after a previous succesful login (ofc before the token expiration).

I did something like this in my website, you can try it by yourself.

For some reason, Keycloak takes time to see that msg. Either refresh the screen again (or) just wait for a few seconds and it will auto-refresh… Its a quirk. Sorry if this didn’t work for you still.

This is the worst framework I have ever worked with, so annoying to just add an easy authentication, can’t persist anything ! Never touching this thing again

Take a look at the code @PZ1 was kind enough to share. You will be to be able persist whatever you want in localStorage

Not able to read it after , and its also causing issues trying to setup navigation on auth. What a waste of time

did you try the new st.login() feature? Introduced in 1.42.0 version, the latest version as of today (16 Feb 2025)

I’ve stated the development of a web application a few weeks ago and its being very tricky to get things done with streamlit. It’s a great framework and what the team have achieved so far is great, but I feel it is not ready for web application development, yet.

The first hard thing I’ve found difficult to get done is this post theme: cookies. I am using CookieManager from extra-streamlit-components to store a cookie with user that I need in multiple parts of my application. The main problem here, for me, is that it works with javascript on the background (of course…) but it does not waits for the cookie to be set to continue execution. To workaround this, I’ve being putting some time.sleep() everytime I get or set cookies, not the ideal solution…

Another problem is for logout the user, because I had to delete the cookie with the user data on logout, but seems that the delete method of CookieManager is not working as expected, probably some bug from the library itself. To be specific, it stores the cookie in a dictinary and, when I call delete() method, it tries to delete the cookie from this dictionary, but it throws an error of “key not found”, with the key in the dictionary. To workaround this, instead of deleting the cookie, I am just setting its value to an empty dictionary.

It should work, and it does in my local environment, but for some reason, in production environment the cookie in logout() sometimes is set, sometimes it’s not, very weird behavior. With some debuging, I’ve found that it is related to some session_state settings I do right before setting the cookie, something like this:

def logout():
st.session_state[‘user_data’] = {}
st.session_state[‘auth_checked’] = False
self.cookie_manager.set(cookie_name=‘user’, data={})

If I comment the first two lines of logout() method, it works as expected. This problem I haven’t figured out how to solve yet.

For last, this web application has a very good and very beautifull UX design developed by one of my teammates. He did is using streamlit library components on Figma, but even so, getting the application to look exactly like the figma design is very, very tricky. I’ve being inspecting the HTML elements that stremalit components creates on the page and figuring out how to customize its style using the classes and attributes I’ve found on the specific HTML elements. Again, not the ideal. Despite of being very difficult, with some pacience I’ve managing to acomplish the expect result in terms of design, excepting customizing gaps between containers and columns, this one I haven’t found how to do.

Bottomline, with the little streamlilt experience and knowledge I have today, I wouldn’t start the project with it. I would go for a robust JS framework, probably Angular, using Materials for generic components. And for the back end I would go for anything: Nest.js, Laravel, Flask, Django, FastAPI, etc… For my case, where I am having a lot of work to customize streamlit components according to Figma design, it is not saving me development time as I thought it would in the begining, I feel it is actually giving me more work to do.

The solution is to create custom streamlit components for your app.
It is very easy and you can code it all up with HTML + your fav JS library.
The component will then interact with your main python app and will merge into the Streamlit’s application seamlessly.
Peace…Silence…Bliss…

@Thalles

when executing critical javascript functions in streamlit that must execute before streamlit executes the next line of code, I have only had success with this module:

from streamlit_js import st_js_blocking #

It blocks streamlit until the JS is fully executed.

one of the more hairpulling issues is js not fully done executing before streamlit takes the next action.

The module is built by ? and forgive my laziness for not giving the author credit by name. But it is built on top of streamlit-javascript package which failed for me

1 Like

@brdemorin

I will definitely give it a try. Thanks for sharing!.

About the peoblem I was having with logout, I found out that it was related to some sesssion state stuff I was doing within the on click event of the logout button. Seems like session state modification can trigger reruns and stop the exeuction flow of the code.

The solution, as advised on streamlit docs, was to make the logout button a st.form_submit_button().

@Sarnath_K

Yeah, just did a custom component for my project this week.

Altough I’ve managed to do what I needed, I had some problems on setting the height of the component to take the available height when embbeding it into the main application, I believe that is because streamlit uses iframe to load the component. Or, it could be something else that I haven’t figured out yet.

At the end, I gave up and just set a fixed height for the component.

1 Like

That’s great to know. I am not sure about the height thing. I too just set it to an absolute value… If you do figure this out, pls do let me know. Thanks!

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