How to wait for number inputs?

Hello,

I am currently working on building an app to process data. Essentially, the user uploads some data, selects a processing option (currently, there is only the AMA option), enters specific parameters, and then initiates the processing. To achieve this, I used the ‘st.sidebar.number_input’ option to gather the user’s inputs for the processing function. However, I encountered an issue: when I input the first parameter value, the program does not wait for the second parameter input. The sidebar closes prematurely, and the program does not execute.

My function ‘f_AMA’ requires the data and parameters dx and dy to run. The intention is for the function to run after clicking the ‘Run’ button.

Any assistance is greatly appreciated. Below is my code. Thank you.

import streamlit as st
from proc_AMA import func_AMA as f_AMA

st.set_page_config(layout='wide',page_title="TEST")

def main():
    st.title("TITLE")
    st.subheader("SUBHEADER")

    data = data_load()

    if st.button("AMA",type="secondary",key="AMA_bt"):

        dx,dy = get_input_AMA()

        if st.button("Run",type="primary",key="RUN_bt"):
            if data != None and dx != None and dy != None:
                f_AMA(data,dx,dy)
            else:
                st.write("Upload the file, input dx and dy parameters !!!")

def data_load():
    uploaded_file = st.file_uploader("Choose a file")
    return uploaded_file

def get_input_AMA():
    st.sidebar.header("Configuration")
        
    dx = st.sidebar.number_input("dx",key="dx_box",value=None,)
    dy = st.sidebar.number_input("dy",key="dy_box",value=None)

    return dx,dy

if __name__ == "__main__":
    main()

The best way to gather input from users is to use a form.

Without using form, once you change the state of one widget, streamlit will run the code from top to bottom. This is normal.

Hello, thank you for your reply.

I am still facing challenges in making my idea work. The concept involves the user pushing a specific procedure button, filling out a form with their inputs, and upon clicking the ‘Submit’ button, the program executes the procedure.

I have written a simplified code to implement this concept. There is only one button; upon clicking it, the user provides two numbers, clicks ‘Submit’, and the program adds these inputs using a function. It then adds two in the main function and displays the result. Everything works smoothly until the ‘Submit’ button is pressed. After that, the program returns to the beginning without completing the step of displaying the result. Below is the simplified code.

Once again, thank you for the help.

import streamlit as st

st.set_page_config(layout='wide',page_title="GMP")

def main():
    st.title("TEST")
    st.subheader("Test")

    if st.button("OPT1",type="secondary",key="OPT1_bt"):
        with st.form("Inputs"):
            n1 = st.number_input("n1",key="n1_box",value=None,)
            n2 = st.number_input("n2",key="n2_box",value=None)

            if st.form_submit_button("Submit"):

                res = sum_ns(n1,n2)
                res += 2
                st.write(res)

    return

def sum_ns(n1,n2):
    return n1 + n2

if __name__ == "__main__":
    main()

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