I’m just wondering if there is a way to programmatically set the label of a streamlit button e.g.
import streamlit as st
btn = st.button("Click Me!")
if btn:
btn.text = "Clicked"
I’m just wondering if there is a way to programmatically set the label of a streamlit button e.g.
import streamlit as st
btn = st.button("Click Me!")
if btn:
btn.text = "Clicked"
I am doing it this way.
if (btn):
st.markdown(
'<style> .streamlit-button.primary-button{visibility: hidden;} .streamlit-button.primary-button:after{content: "Clicked"; visibility: visible; position:relative;-webkit-tap-highlight-color: rgba(38,39,48,0);box-sizing: border-box;font-family: inherit;font-size: inherit;overflow: visible;text-transform: none;display: inline-flex;align-items: center;justify-content: center;font-weight: 400;border-radius: .25rem;margin: 0;line-height: 1.6;color: #262730;cursor: pointer;padding: .25rem .75rem;background-color: #fff;border: 1px solid #e6eaf1; left: 0;}</style>', unsafe_allow_html=True)
I know it is not how it should be done, but it’s just my small personal project and I didn’t find a quick solution so I’m doing it this hacky way. I’m sure the quality can be improved with some more CSS fiddling.
I used session_state to replace the whole button. Here is how I switch between 2 buttons:
myKey = 'my_key'
if myKey not in st.session_state:
st.session_state[myKey] = False
if st.session_state[myKey]:
myBtn = st.sidebar.button('Button 1')
st.session_state[myKey] = False
else:
myBtn = st.sidebar.button('Button 2')
st.session_state[myKey] = True
You can use integer instead of boolean to have more cases.
[minhanh29] inspired me:
container_2 = st.empty()
button_A = container_2.button('Btn A')
if button_A:
container_2.empty()
button_B = container_2.button('Btn B')
In this way, if you click other buttons, it will not change.
Here’s another hacky way it can be done:
Cheers
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.
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.
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.
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.