Hard refresh, session_state and react login component

Hi comunity,

How are you? Hope you are doing fine.

Where I work we develop a Login Active Directory component to work with our apps.
Its currently on develop, but the first verision is on pypi. The idea is to make this a page of login and with a button of logout to refresh everything, but this does not happend.

I need help to find why, here is the code:

import streamlit as st
from streamlit import caching
from streamlit_azure_login import login_component
from PIL import Image

def main():
    # Imagenes a utilizar
    favicon = Image.open('src/app/components/assets/ic.png')
    st.set_page_config(page_icon=favicon)
    logo = Image.open('src/app/components/assets/log_ic.png')

    st.session_state.token = False
    print(f'Not Loged 1: {st.session_state.token}')
    print(st.session_state)
    print(type(st.session_state))

    if st.session_state.token is False:
        container = st.empty()
        print(f'Not Loged 2: {st.session_state.token}')
        with container.container():
            token = login_component(
                header_text='Intercement', 
                authentication_endpoint_url=environ.get('AD_ENDPOINT'), 
                logo_uri=environ.get('AD_LOGO_URI'),
                prefix=environ.get('AD_PREFIX'),
            )
        print(f'Token: {token}')
        if token:
            st.session_state.token = True
            token = login_component('', '', '', '',)
            print(f'Token 2: {token}')
            container.empty()

    if st.session_state.token is True:
        print(f'Loged: {st.session_state.token}')
        # Estructura básica de la app
        st.title('Dashboard 0800')
        st.sidebar.image(logo, width = 300)
        st.sidebar.title('Informacion de llamadas 0800')

        informations = [
            SidebarOptions.current_information.value,
            SidebarOptions.attention_leves.value,
            SidebarOptions.attention_on_levels_of_deviations.value,
            SidebarOptions.top_15_sales_representatives.value,
            SidebarOptions.top_customers_and_no_customers.value,
        ]

        choice = st.sidebar.selectbox('Información de llamadas', informations)

        if st.sidebar.button('Logout'):
            del st.session_state['token']
            print(f'Logout: {st.session_state}\n\n\n')
            caching.clear_cache()
            st.experimental_rerun()

        if choice == SidebarOptions.current_information.value:
            current_information_tab()

        if choice == SidebarOptions.attention_leves.value:
            attention_levels_tab()

        if choice == SidebarOptions.attention_on_levels_of_deviations.value:
            attention_levels_of_deviations_tab()

        if choice == SidebarOptions.top_15_sales_representatives.value:
            top_15_sales_representatives_tab()

        if choice == SidebarOptions.top_customers_and_no_customers.value:
            top_customers_and_no_customers()

I try deleting the cache, seting the token to none but its apear to pass away the logic and dont start again the app.
Its there any way to make a browser refresh from streamlit?

Thanks!

Hi @galloramiro - try putting this at the top:

if 'token' not in st.session_state:
   st.session_state.token = False

I found that using st.experimental_rerun() works better with session state if you wrap the initialisation in a function, e.g.:

def auth_state():
    if 'token' not in st.session_state:
        st.session_state.token = False
    return st.session_state

   :
  etc. (other auth state
  variables you may have)
   :

Then use this in your code by calling auth_state().token. This way the token is always guaranteed to survive the cache being cleared.

If you like, you do this instead if all you have is token in session state:

def token_state():
    if 'token' not in st.session_state:
        st.session_state.token = False
    return st.session_state.token

… and then use token_state() directly.

Hope that works… (I haven’t run your code).

Arvindra