How to change with an button the input value of input_number


How to change input_number value to default after clicking st.button?

Steps to reproduce

Code snippet:

example = st.number_input('Example', min_value=0.0, max_value=1.0,step=0.1, format='%.7f', value=0.0)

if st.button('Reset'):
    #I thought that by setting the default parameter to 0.0 in st.number_input, the input field     #would be initialized with that value. However, even after changing the value, the old value #is still displayed in the input field, and when I press a button to print the updated value,   #the old value is always printed because it is still present in the input field.

   #What i want:
    #reset value of example to default and changing also the html input value

Explain what you expect to happen when you run the code above.

It should update the hmtl input line and the input value

Debug info

  • OS version: Windows

You can assign a key to the number_input() and manipulate the value using session_state.

You cannot change the value of an already instantiated widget, so you have to create the button before the number_input. Use empty() to display the widgets in a different order.


if "example" not in st.session_state:
    st.session_state.example = DEFAULT_NUMBER

placeholder = st.empty()

if st.button('Reset'):
    st.session_state.example = DEFAULT_NUMBER

example = placeholder.number_input('Example', min_value=0.0, max_value=1.0,step=0.1, format='%.7f', key="example")

Thank you very much this helps a lot and solved my problem :slight_smile: