The press the button to calculate is not working

i am trying to let the equation to be ran after button is clicked, but in my part its calculating once i leave the input box, is the structure of the code not according to the form standards?

 def my_value(number):
     return ("{:,}".format(number)) # a function to format numbers to have commas in them

 col11, col22, col33, col44, col55, col66 = st.columns(6) # creating 6 columns to put all the input boxes next to each other


 with st.form(key='form1'): # enabling the model to calculate after button is pressed
 # putting various number inputs for the calculations
    emp = col11.number_input('Number of Pickers', value=14,min_value=1, step=1)
    orde= col22.number_input('Orders per Hour',value=10)
    cap = col33.number_input("Picker's Capacity per Hour",value=.75)
    costp = col44.number_input("Picker's Salary per Hour",value=16203.70)
    costc = col55.number_input('Cost of Call per Minute',value=0.004,format="%.5f")
    rate = col66.number_input('Insert LBP Exchange Rate',value=29500)

    try:
        result = orde/(emp*cap)
    except ZeroDivisionError: # error will appear if zero division error appears
        result = 0 # then returning 0 as output

    print(result)

    if (result < 0 or result > 1) : #if Utilization is less than 0 and greater than 1 returns an error
        st.error('negative result')
    else:
        st.write(f'Utilization of pickers {round(result*100,2)}%') # Rounding Utilization and turning it into percent
        sc=(result**np.sqrt(2*(emp+1)))/orde # formula to calculate the first part of the equation using the numbers that have been input
        try:
            uf=1/(1-(orde/(emp*cap)))
        except ZeroDivisionError: # error will appear if zero division error appears
            uf = 0# then returning 0 as output
        mint=uf*sc # calculating orders per minute
        wait=mint*60 # then turning it into hours
        dp=st.write(f'On average minutes {round(wait, 2)} before order deployment') # writing the results of minutes on average
        lc=costp*emp # calculating labor costs
        wc=costc*60 # calculating the cost of call per hour
        wcr=wc*rate # turning it into the black market exhange of LBP
        wcrr=mint*wcr*orde # total cost of call per hour
        total=lc+wcrr # total cost of labor and call
        # Writing the results from the calculations
        st.write(f'Labor cost for {emp} pickers would be {my_value(round(lc))} LBP per hour')
        st.write(f'Call cost for {orde} customers waiting would be {my_value(round(wcrr))} LBP per hour')
        st.write(f'Total cost would be {my_value(round(total))} LBP per hour')
        totalusd=total/rate
        st.write(f'Total cost in U.S.D would be ${round(totalusd,2)} per hour')
    st.form_submit_button('Press to calculate') #button to be pressed to initiate calculatingrite(f'Total cost in U.S.D would be ${round(totalusd,2)} per hour')
            st.form_submit_button('Press to calculate') #button to be pressed to initiate calculating

Can you try moving the line that defines columns after the with st.form(key='form1'): line? Something along:

...
with st.form(key='form1'): # enabling the model to calculate after button is pressed
    col11, col22, col33, col44, col55, col66 = st.columns(6) # creating 6 columns to put all the input boxes next to each other
    ...

Because you define them before the form, the input boxes are not currently binded with your form! It’s also visible from the screenshot, normally you would expect the input boxes to lie within the bordered square.

Hope that helps,

Thank you soo much @arnaud for your help ive been trying to fix it for soo long but it was just a simple misplacement of code

1 Like

:smile: I understand the confusion, it’s tempting to expect being safe within the with st.form… but Streamlit reads top to bottom and the source of truth is when the variable is actually defined!

Don’t hesitate to reach out more in the forum when you’re stuck!

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