Unable to use text_input form validation without form_submit_button show "Missing Submit Button"

I’m trying to require file extensions on a text_input widget, but since it’s a part of my form it causes the form_submit_button to not show up on the page.

Error

Missing Submit Button

This form has no submit button, which means that user interactions will never be sent to your Streamlit app.

To create a submit button, use the st.form_submit_button() function.

For more information, refer to the documentation for forms.

def main():
    with st.form("Form For File Search"): 
        search_text = st.text_input("Enter filename", placeholder="example_file.csv")
        if search_text.endswith(".txt"):
            start_date = st.date_input("Start date", format="YYYY-MM-DD", key="start_date")
            end_date = st.date_input("End date", format="YYYY-MM-DD", key="end_date")
            st.form_submit_button("Submit", )

    if search_text and start_date and end_date:
        df = get_df(search_text, start_date, end_date)
        with st.container():
            st.table(df)

Versions

streamlit 1.29.0
Python 3.10

I figured it out. I needed to add an if condition after the form and check if the user input text endswith the extension. Then execute rest of code.

ALLOWED_EXTENSIONS = [".txt", ".csv", ".pdf"]
def main():
    with st.form("Form For File Search"): 
        search_text = st.text_input("Enter filename", placeholder="example_file.csv")
        start_date = st.date_input("Start date", format="YYYY-MM-DD", key="start_date")
        end_date = st.date_input("End date", format="YYYY-MM-DD", key="end_date")
        st.form_submit_button("Submit", )

    for extension in ALLOWED_EXTENSIONS:
        if search_text.endswith(extension):
            df = get_df(search_text, start_date, end_date)
            with st.container():
                st.table(df)

Now I need to figure out how to show an error after clicking the submit button and don’t clear the form.

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