Page reruns after user input

Summary

Page reruns after user input

Steps to reproduce

So after clicking out of the text input, the whole page reruns and the elements “shake”. If for example you click out of username onto password, sometimes it reruns and deselects that element, so you need to click again on it.

Can somebody help me find a fix?

Code snippet:

import streamlit as st
from user import * 


if 'loggedIn' not in st.session_state:
    st.session_state['loggedIn'] = False
if 'CurrentUser' not in st.session_state:
    st.session_state['CurrentUser'] = ""

main = st.container()
loginSection1, loginSection2, loginSection3, loginSection4= st.columns(4)
vv1, vv2, vv3 = st.columns(3)

def show_login_page():
    with loginSection4:
        with vv3:
            userName = st.text_input (label="User", key="userName", )
            password = st.text_input (label="Password", key="pswdLogIn", type="password")
            Login = st.button ("Login", key="loginButton",on_click=LoggedIn_Clicked, args= (userName, password))

            
                
def LoggedIn_Clicked(userName, password):
    if login(userName, password):
        st.session_state['loggedIn'] = True
        st.session_state['CurrentUser'] = userName
    else:
        st.error("Invalid user name or password")


with main:
    if not st.session_state.loggedIn:   
        show_login_page()

Hey @zs00,

Thanks for sharing your question! Streamlit’s execution model reruns your script from start to finish whenever a user interacts with a widget by default. To prevent the app from rerunning a specific piece of code, you can use caching or batch input widgets as a form

Unfortunately I wasn’t able to reproduce the “shake” that you mentioned (I hit an error because “login” is undefined in the snippet you shared). Can you share a screen recording/a complete code snippet so we can try to reproduce it?

Hi Caroline, thank you very much. Placing the widgets on a form did the trick!

Im attaching a gif that shows the “shaking” after clicking outside of the text boxes in case it helps you find the issue.

Tanks!

2023-05-31 11-56-22 - Trim

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.