Using st.cache_resource to limit streamlit concurrent users

A minor update to @marduk code at Right way to manage same user opening multiple sessions - #3 by marduk

The goal is to limit the number of times a user can concurrently connect to a Streamlit app.

Replacing the deprecated @st.experimental_singleton with @st.cache_resource() st.cache_resource - Streamlit Docs

import streamlit as st
import datetime as dt

# Example list of users
USER_LIST = ['Barbara','Christopher']

# Updates the active session for a given user
#def get_active_session(username):
#    return st.session_state.session_id

def get_active_session(username):
    return st.session_state.session_id

# App when logged out
if 'session_id' not in st.session_state:
    st.warning('You are currently logged out')

    #Login form
    with st.form('Login form'):
        username = st.text_input('Username (try "Barbara" or "Christopher")')
        sign_in = st.form_submit_button('Sign In')
    if sign_in and username in USER_LIST:
        st.session_state.user_id = username
        st.session_state.session_id = username + '_' + str(
        aux_active_session = get_active_session(st.session_state.user_id)

# App when logged in and session active
elif st.session_state.session_id == get_active_session(st.session_state.user_id):
    st.success('You are currently logged in')
    st.write('Session ID: ',st.session_state.session_id)
    st.button('Useless button')

    # Logout button
    if st.button('Logout'):
        for key in st.session_state.keys():
            del st.session_state[key]

# App when session expired
else:'Your session has expired')
    for key in st.session_state.keys():
        del st.session_state[key]
    st.button('Return to homepage')
1 Like

Hi @IndigoJay,

Thanks for posting!

I have linked your post to the original post for continuity.