I have a issue using widget inside a st.button.
When I click on button it displays a plot and some st.input_number widgets. When I start to enter a number in st.input_number, the button refresh and we can not see the plot anymore.
I think there is something to do with st.session_state, but I reaaly don’t understand how st.session_state works honestly…
Buttons will only be True immediately after clicked, then go back to False. So if you want a button to conditionally display something, and keep it displaying while you proceed with other interaction, yes, you’d need to use session state.
if 'show' not in st.session_state:
st.session_state.show = False
def toggle_show():
st.session_state.show = not st.session_state.show
st.button('Show', on_click=toggle_show)
if st.session_state.show:
# Your code, including widgets if you want
Instead of setting your condition directly on the button, you use the button to affect something in session state, then condition on that data stored in session state so it persists.
I did a little introductory video on session state geared around widget interaction: Session State Introduction
Actually it works for this, but I have another issue. At the beggin on my app I have two select_box, and I want everything is refreshed when I choose other values in the select_box.
To draw a schema it will be:
When I change the number_input, I want the some_plots1 and some_plots2 stay the same while changing my number_input.
But when I change Select_box, I want everything be refreshed whitout displaying some_plots2 before pushing the button, and without some_plots3 without the numbers entered in number_input.
Because I want to keep the plot behind the button visible, but not the plot after if I did not enter all the number_input. For this I created a second button, to click when the input_number are entered.
But still, every micro change of input_number make the app refresh, and from a ux point of view, it is a bit annoying…
It may take some experimenting what works best as there are some limitations, but you’ll want to maximize the use of caching where you can. I’ve gotten much better performance out of the experimental cache functions in general, and st.cache is destined for sunsetting at some point.
The app reruns on every user interaction, that is pretty much unavoidable. But it should not be a painful UX. In this example I use st.experimental_memo to cache the plots.
import time
def on_selection_change():
st.session_state["plots2_clicked"] = False
def on_plots2_click():
st.session_state["plots2_clicked"] = True
@st.experimental_memo
def plot1(selection):
time.sleep(5)
data = {selection: [1, 2, 3], "Plot 1":[1, 4, 9]}
st.line_chart(data)
@st.experimental_memo
def plot2(selection):
time.sleep(5)
data = {selection: [1, 2, 3], "Plot 2":[1, 4, 9]}
st.line_chart(data)
# Don't let the cache grow without limits.
@st.experimental_memo(max_entries=5)
def plot3(selection, number):
time.sleep(5)
data = {selection: [1, 2, 3], f"Plot #{number}":[1, 4, 9]}
st.line_chart(data)
selection = st.selectbox(
label="Select",
options=["Foo", "Bar", "FooBar"],
on_change=on_selection_change
)
plot1(selection)
st.button("Plots 2", on_click=on_plots2_click)
if st.session_state.get("plots2_clicked", False):
plot2(selection)
number = st.number_input(label="Number")
if st.button("Plots 3"):
plot3(selection, number)
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.