Set/reset selectbox value

Hello. I have two selectboxes and i want to reset the value of one of them when the user selects from the other one. Is there any way to do this? Thamks :grinning:

Hi @elwyn, welcome to the community! :wave: :partying_face:

reset the value of one of them when the user selects from the other one

Could you elaborate on what you mean by that, perhaps with an example?

Happy Streamlit-ing! :balloon:
Snehan

1 Like

Hello and thamks for the welcome :slight_smile:
In short, how can I set the value of a ‘text-input’ from code?
Say, when the user presses a button, the value of ‘transport’ text_input should be set to ‘10’

transport = st.text_input('Transport', 0)

if st.button('Set transport to 10'):
    transport = 10

^I tried something like this but it doesn’t change the ‘text_input’ value, only the variable

You could set the value of your transport text_input box to be a variable (you’ll need to assign that variable a value first), and then have the button change the value of that variable?

1 Like

Hi @elwyn ,

Will this work for you ?

import streamlit as st

emp = st.empty()
transport = emp.text_input('Transport', 0)
# Probably you don't need slider, just to show that the value can be variable user Input
val = st.slider(label = 'Transport value',min_value= 1,max_value = 20 )

if st.button('Set transport to 10'):
    # Insted of val, just plug in 10, if exactly that's what you need
    transport = emp.text_input('Transport', val)

Best,
Avra

1 Like

Hello ac3, thamks for the reply.
You mean something like this? Tried it, doesn’t seem to work.

val = 0
transport = st.text_input('Transport', val)
if st.button('Set transport to 10'):
   val = 10

@AvratanuBiswas 's solution works fine though :smiley:

Yes! Thank you so much. It works perfectly like this :ok_hand:t2: :ok_hand:t2:

1 Like

What if i also want the text_input to have a certain width?
Normally it goes smth like

col1, col2 = st.beta_columns([2, 1])
transport = col1.text_input('Transport', 0)
stock = col2.text_input('Stock', 0)

But if i replace ‘col1’ with ‘emp’, it will have full-width…