I have a multi-page Streamlit app with a form that stores input data in st.session_state
, and displays it on the next page. I want to add input validation and display a warning message for missing input below the respective field using a placeholder (st.empty()
). The issue is that I have to click the form button twice to proceed after adding missing input:
import streamlit as st
if 'page' not in st.session_state: st.session_state.page = 0
def nextPage(): st.session_state.page += 1
def firstPage(): st.session_state.page = 0
def main_page(name_error_message=None):
st.markdown("# Main page")
with st.form(key='main_page'):
st.session_state.name = st.text_input("Enter your name (required)")
name_error_message = st.empty()
submit_button = st.form_submit_button(label='Next')
if submit_button:
if st.session_state.name == '':
name_error_message.error("Please enter your name")
else:
nextPage()
def page2():
st.markdown(f"# Hi {st.session_state.name}!")
st.button("First page", on_click=firstPage)
page_names_to_funcs = {
0: main_page,
1: page2,
}
page_names_to_funcs[st.session_state.page]()
It seems that Streamlit first needs a run to remove the error message and only then can proceed to the next page. Any solutions or workarounds?