How to add a function to a button?

Amateur alert

Hey, assume a function:

def add(a,b):

        c = a+b

        return c

I created a button using st.button('add'). Now I would like to link the add function to the button. How can I do it?

Thanks

1 Like
def fun():
st.write('fun click')
return

if st.button(β€˜Click Func foo’):
fun()

1 Like

Hey @Junkrat, welcome to Streamlit!

The st.button function returns True if the button was clicked by the user during the most recent run of your app. So to attach your function to the button, you could do this:

if st.button('add'):
    result = add(1, 2)
    st.write('result: %s' % result)
5 Likes