Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code executed 3 times when login #33

Open
kinokocorp opened this issue Sep 18, 2022 · 5 comments
Open

Code executed 3 times when login #33

kinokocorp opened this issue Sep 18, 2022 · 5 comments
Labels
help wanted Extra attention is needed

Comments

@kinokocorp
Copy link

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.

@mkhorasani
Copy link
Owner

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.

@mvn-hoangnguyen-hn
Copy link

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.

@mkhorasani mkhorasani added the help wanted Extra attention is needed label Jan 22, 2024
@MadigRG
Copy link

MadigRG commented Mar 1, 2024

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.

@ShuaHousetable
Copy link

Any updates on this issue?

@AngelicSage
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

6 participants