Streamlit-Authenticator icon indicating copy to clipboard operation
Streamlit-Authenticator copied to clipboard

Code executed 3 times when login

Open kinokocorp opened this issue 3 years ago • 9 comments

Hello, When you log in with Streamlit-Authenticator, the code is executed 3 times. I added a print('test') command and I can see 'test' 3 times in the console.

I'm using version 0.2.1.

import streamlit as st
import streamlit_authenticator as stauth
import yaml

st.set_page_config(page_title='Auth', layout='wide')

# Load Authenticator Configuration
with open('config.yaml') as file:
    config = yaml.load(file, Loader=stauth.SafeLoader)

# Authenticator Parameters
authenticator = stauth.Authenticate(
    config['credentials'],
    config['cookie']['name'],
    config['cookie']['key'],
    config['cookie']['expiry_days']
)

name, authentication_status, username = authenticator.login('Login', 'main')

if st.session_state["authentication_status"]:
    authenticator.logout('Logout', 'main')
    st.write(f'Welcome *{st.session_state["name"]}*')
    st.title('Some content')
    print("test")
elif st.session_state["authentication_status"] == False:
    st.error('Username/password is incorrect')
elif st.session_state["authentication_status"] == None:
    st.warning('Please enter your username and password')

After logging in, the problem doesn't occur again.

kinokocorp avatar Sep 18 '22 19:09 kinokocorp

Hi @kinokocorp, I believe this is related to the extra_streamlit_components package that reads the reauthentication cookie. Thank you for pointing this out, as soon as a robust solution is found I will push a new release.

mkhorasani avatar Sep 18 '22 20:09 mkhorasani

Hi @mkhorasani, i am having similar problem with Streamlit-Authenticator, the code is executed multiple times. I am using extra-streamlit-components version 0.1.60, streamlit-authenticator version 0.2.3 and streamlit version 1.26.0. My code: Login.py

import streamlit as st
import streamlit_authenticator as stauth
import yaml

# Load Authenticator Configuration
with open('config.yaml') as file:
     config = yaml.load(file, Loader=stauth.SafeLoader)
     
def login():

 # Authenticator Parameters
 authenticator = stauth.Authenticate(
    config['credentials'],
    config['cookie']['name'],
    config['cookie']['key'],
    config['cookie']['expiry_days']
 )

 name, authentication_status, username = authenticator.login('Login', 'main')

 if st.session_state["authentication_status"]:
    authenticator.logout('Logout', 'main')
    st.write(f'Welcome *{st.session_state["name"]}*')
    st.title('Some content')
    return true
 elif st.session_state["authentication_status"] == False:
    st.error('Username/password is incorrect')
 elif st.session_state["authentication_status"] == None:
    st.warning('Please enter your username and password')
 return false

main.py

def main():
    st.title("Main page")
    print("hello")
if __name__ == "__main__":
    if login():
        main()

When i login, text "hello" print two times. It look likes there are two threads calling in same time. Am i using wrong ? Please help, thank you.

mvn-hoangnguyen-hn avatar Nov 06 '23 02:11 mvn-hoangnguyen-hn

Hi @mvn-hoangnguyen-hn, Have you solved your problem ?

Hi @mkhorasani, I have similar problem. Login needs 2 clicks and so generates 2 API calls. New users calling the API for the first time are supposed to have a specific display. This behavior is impossible due to the double execution.

Any help is welcome.

MadigRG avatar Mar 01 '24 08:03 MadigRG

Any updates on this issue?

ShuaHousetable avatar Mar 28 '24 13:03 ShuaHousetable

Every time the session state changes, streamlit reruns the page, so that's probably what's going on, making the code run over and over again, I'm going to try to fix it

AngelicSage avatar Jun 04 '24 14:06 AngelicSage