Cookies support in Streamlit!

An extension to extra-streamlit-components · PyPI | (Github) , I added browser cookie support. You can preserve data between sessions, update, get, and delete browser cookies. Newly added cookies with this module is strictly same-site, so you don’t have to worry about privacy.
Highly advised to be on Streamlit >= 0.84.0 for seamless experience as it harnesses state management for faster performance

cookie_manager

6 Likes

Hey @Mohamed

You should definitely add your extensions to the Components Tracker :slight_smile: those are very neat features!

Happy Streamlitlin’ :balloon:
Fanilo

1 Like

Woah! This is amazing Mohammed! :heart_eyes:

I’ve shared internally with the Dev team.

Best,
Charly

1 Like

The cookie solution solves a major pain point. Thank you @Mohamed. I am going to experiment with tabs and other features here. Great work!

The tab feature is really cool. I tested it with 6+ elements and some are of course hidden. I was able to drag the tabs but it took me a while to find out that it could be done.

It may be nice to show something like a scrollbar in the bottom if there are many elements. What do you think?

Glad you like it! Will consider that feedback in later iterations of the library.

2 Likes

Quick question Mohammed.

Do you know if the cookie feat works in Streamlit Sharing?

Thanks
Charly

cc: @jrieke

This looks splendid! I can’t wait to incorporate this and the other extra components in my app.

Good going! :clap:

1 Like

I think the pip package’s main.py shall work fine without any additional setup. I tried figuring share.streamlit.io out on but my invitation request hasn’t been processed in days.
cc: @jrieke

1 Like

FYI @Charly_Wargnier I’ve just shared the package on streamlit sharing @ extra-streamlit-components. Enjoy!

1 Like

Beautiful!

I’ve shared internally with the team!

Charly

1 Like

@Mohamed would it take a lot of work to add expiration dates to the cookies that are being set? If not, it would be very useful to add it.

Nice idea @vivekdse, by default it’s set to a day. But will try to make it user configurable in the next iterations.

That would be great. Thank you.

You can update to 0.1.43 now, and add a cookie expiration date as a python datetime object.

Awesome. I did and it works. Do you want to add the instruction on github on how to add expires_at? I had to dig through the source code for it, and it was worth it :slight_smile: Thank you for taking this on quickly.

1 Like

@Mohamed How to get the cookie from a existed internet address
and judge its specific cookie is exist or not?
if not exist, do redirect operation, only get valid cookie, user can view the app page.
how can we make it come true?

I had made it use asp, it very convenient
like this:

<%
     if len(request.cookies("cookie_name"))<1 then
           response.redirect("specific address to finish identity check  & back to current page url")
    end if
%>

can we achieve a similar function?

Well @BeyondMyself this is not considered as a limitiation of this library rather an application design problem. I propose you do either of the following:

  • Make this authentication mechanism using a REST API where you expect the response to be a cookie or token which then you can use stx to add the cookie.
  • Make the identity check redirect to the app with a query parameter set to the token. i.e. 127.0.0.1:8501?token=1234S3cr3t then use built in streamlit commands to extract that query parameter and then set it as a cookie
  • I can remeber seeing a post on this forum mentioning successfully using AOuth2 which as far as I know, does the identity check in a new window browser and returns an auth token, which then you can also add to the browser cookie using stx
    Good Luck!