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 the 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
Hello @fahadakbar,
Yeah, even I had to face problems like this. However, instead of using a button, I used a checkbox. The thing is - st.button() returns a boolean value ryt. So now only when it is true, it is going to display the st.number_input(). Therefore, when you enter a number, that press_button becomes false as you did not press it. That is why it happens.
Personally, I just used a checkbox and that solved the problem for me.
If there is a way around that without using checkbox, even I would love to know.
Anyways, try checkbox and see.
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",)
Letās say that you built a ML model and user inputs several fields. Then the user can press a button (āPredictā) and your app can display the predicted output (st.success()).
You want to visualize and after entering the fields, press (āVisualizeā) and so on.
Like this, there can be may combinations. It all depends on the architecture of your app i.e. how you want to use a specific widget or what you want to do with it etc. Hence, choose a component or widget based on the workflow of your app.
letās say I have a predict button, and ask for the data from the user as an input
wouldnāt pressing the button means that the user will need to give me data again?
If you implement the session state or url get/set query parameters, you can maintain a ābutton pushedā flag which will allow you to persist the button pushed state across runs.
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.