Conditioning for st.form

Hello, been trying to add conditioning when using st.form. I need to add an if statement that checks if files have been uploaded when a user clicks submit when using st.form.

but this has been difficult to implement. I’ll appreciate any help on this

Hello @nelsonchristof , just copying my answer here :wink:

Not sure I understood correctly, but I usually prefer conditioning with Python rather than Streamlit widgets

The way I usually do it is (this code is not tested, just wrote it out of my mind) :

def main():
    with st.sidebar:
        X_train = st.file_uploader(...)
        y_train = st.file_uploader(...)
        X_test = st.file_uploader(...)
        y_test = st.file_uploader(...)

    if st.button("Run ML model"):
        if None in [X_train, y_train, X_test, y_test]:
             st.warning("Upload a file for each dataset before running ML model")
             return  # that way you stop the app. There's also a st.stop() I think if you're not using the main() method like I do

        do_stuff_with_data(X_train, y_train, X_test, y_test)

if __name__ == "__main__":
    main()

This just gave me an idea that I think will work. thanks once again