How to change with an button the input value of input_number

Summary

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

Steps to reproduce

Example:
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.

DEFAULT_NUMBER = 0.0

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")

2 Likes

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

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