Widget dependent values?

Is there a way to set the value of an existing widget during interaction?

Two examples of things Iโ€™d like to be able to do:

  1. Pressing a button sets a bunch of other widgets back to their default values
  2. I have variables a and b, for which I require b>=a. There is a slider for each variable. I want to keep the sliders to enforce the constraint. For example, if the user drags a to b+1, then b should be updated to b=a.

This seems related to the topic of SessionState discussed here: Update slider value.

If these capabilities Iโ€™m asking for donโ€™t already exist, then I think they could be added to Streamlit without requiring general-purpose state management like SessionState. Instead, youโ€™d need to add a new argument to each widget that specifies behavior that may vary each time the widget is called.

For example, for the reset button, you could add an additional variable to each widget called forceValue. When forceValue is True, then the value of the widget will always be value, no matter what other interaction has happened.

resetPressed = st.button('Reset to defaults')
sliderVal = st.slider('a', value=defaultA, forceValue=resetPressed)

This could also be used to set things to different default preset defaults, e.g., by making defaultA dependent on other widgets.

Handling the slider case would be more difficult; it might require passing a function ( val -> val) that takes the current value and determines what to replace it with, e.g.,:

sliderA = st.slider("A", value = 0)
sliderB = st.slider("B", value = 0, 
               forceValue = lambda b: b if b > sliderA else sliderA )

This doesnโ€™t quite have ideal UI behavior when the user drags b < a, but it does enforce the constraint.

An even nicer way to solve my second goal would be to introduce a new slider widget that lets you specify a range of values on the same scale (i.e., specify the start and end value).

Another use case: selecting a new item from a dropdown causes a bunch of widgets to reset to defaults.

Never mind; Iโ€™ve discovered that Streamlit already has range sliders, and Iโ€™ve found workarounds for the other use cases.

1 Like