Reputation: 518
I am trying a simple concept of two widgets, where one is dependent on the other
I want to user to click a button, that opens up a number slider. The user then has the option to select the number through number slider.
Here is the code I am using
import streamlit as st
press_button = st.button("Press it Now!")
if press_button :
# ask for the value
th = st.number_input("Please enter the values from 0 - 10",)
The issue I face is the moment I change the number slider's value, streamlit reruns the entire thing, and one has to push the "button" again to get back to the number slider. Ultimately, the number slider never changes
Upvotes: 5
Views: 3822
Reputation: 153
Streamlit reruns a script every time an action occurs: a button is pressed, a number is input, etc. To save a value, use a cached function to maintain values between runs.
I'm pretty sure this is what you want:
import streamlit as st
# Keep the state of the button press between actions
@st.cache_resource
def button_states():
return {"pressed": None}
press_button = st.button("Press it Now!")
is_pressed = button_states() # gets our cached dictionary
if press_button:
# any changes need to be performed in place
is_pressed.update({"pressed": True})
if is_pressed["pressed"]: # saved between sessions
th = st.number_input("Please enter the values from 0 - 10")
Note: you said you wanted a slider, but then you used a number input. If you actually wanted a number slider, use
st.slider("Select a Number", min_value=1, max_value=10, step=1)
A few extra things to consider:
btn = st.empty()
nmb = st.empty()
This guarantees that nmb
will appear directly beneath btn
. And when you define your widgets, instead of calling st.button(...)
you call btn.button(...)
.
if press_button:
is_pressed.update({"pressed": not is_pressed["pressed"]})
if is_pressed["pressed"]:f
btn.empty()
th = nmb.number_input(...)
Upvotes: 6
Reputation: 518
There seems to be a workaround as the button widget returns a boolean. When I used the check box, I got what I wanted:
import streamlit as st
press_button = st.checkbox("Press it Now!")
if press_button :
# ask for the value
th = st.number_input("Please enter the values from 0 - 10",)
Upvotes: 0