# Do not reload st.button when manipulation st.selectbox

**URL:** <https://discuss.streamlit.io/t/do-not-reload-st-button-when-manipulation-st-selectbox/48743>\
**Category:** Using Streamlit\
**Created:** [August 6, 2023, 2:19pm UTC](https://discuss.streamlit.io/t/do-not-reload-st-button-when-manipulation-st-selectbox/48743 "2023-08-06T14:19:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jgutton1](https://avatars.discourse-cdn.com/v4/letter/j/3bc359/32.png) [@jgutton1](https://discuss.streamlit.io/u/jgutton1)\
**Post date:** [August 6, 2023, 2:19pm UTC](https://discuss.streamlit.io/t/do-not-reload-st-button-when-manipulation-st-selectbox/48743/1 "2023-08-06T14:19:31Z")

</div>

### Summary

As mentioned in the title, i try to manipulate a selectbox without updating the result of a st.button. In the example below, i want to keep the text “clicked\_1” when I select other option. Do you see how to do that?

### Steps to reproduce

**Code snippet:**

```auto
import streamlit as st

@st.cache_data(experimental_allow_widgets=True)
def fonction_click1():
    if st.button('first click'):
        st.write('clicked_1')
    

fonction_click1()

option = st.selectbox(
    'How would you like to be contacted?',
    ('Email', 'Home phone', 'Mobile phone'))

st.write('You selected:', option)

```

Thank you

---

<div class="post-metadata">

**Author:** ![momos](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/momos/32/30600_2.png) [@momos](https://discuss.streamlit.io/u/momos)\
**Post date:** [August 11, 2023, 11:07am UTC](https://discuss.streamlit.io/t/do-not-reload-st-button-when-manipulation-st-selectbox/48743/2 "2023-08-11T11:07:07Z")

</div>

Hi!,

This problem has occured to many before, and it is one of the most common on this forum:

> [@10 most common explanations on the Streamlit forum](https://discuss.streamlit.io/t/10-most-common-explanations-on-the-streamlit-forum/39054):
>
> A guide for Streamlit beginners Hey, community! wave My name is Debbie Matthews, and I’m a moderator on [Streamlit’s wonderful forum](http://discuss.streamlit.io/?ref=streamlit). You may have seen me around as [mathcatsand](https://discuss.streamlit.io/u/mathcatsand/summary?ref=streamlit), as in “math, cats, and….” If you hang around the forum long enough, you’ll start seeing some common pain points and areas of confusion. I thought it’d be helpful for new users to know where many people trip as they get started with Streamlit. In this post, I’ll talk about 10 of them: Buttons aren’t stateful…

Your answer should be in the above link. However, I have given an explanation here as well.

Your problem can be solved by using `st.session_state`. Whenever the state of a widget changes in Streamlit, it reruns the script. That is why your button goes back to a normal ‘unclicked’ state and your text is not visible.

However, in Streamlit, you can store values in the session state, which persists across reruns. So your code for the click function will be:

```auto
import streamlit as st

'''
    We store a parameter named 'button_clicked'(Name can be changed) in session state.
    At the start of our session, we set it to False. Once the button is clicked, we can
    change it to True. This will not reset when page is reloaded upon selecting option.
'''
if 'button_clicked' not in st.session_state:
    st.session_state['button_clicked'] = False
    

def fonction_click1():
    mybutton=st.button('first click')
    if not st.session_state['button_clicked']: # if button has not been clicked
        if mybutton:
            st.write('clicked_1')
            st.session_state['button_clicked'] = True #our button has been clicked now
    else:
        st.write('clicked_1')
        
fonction_click1()
    

option = st.selectbox(
    'How would you like to be contacted?',
    ('Email', 'Home phone', 'Mobile phone'))

st.write('You selected:', option)

```

Now, the text will persist since we are checking the session state variable to decide writing the text. This can probably be executed in a much simpler way, but this is what I came up with.

Here are some links to understand session state better:

> **[Session State - Streamlit Docs](https://docs.streamlit.io/library/api-reference/session-state)**
>
> st.session\_state is a way to share variables between reruns, for each user session.

[![](https://global.discourse-cdn.com/streamlit/original/3X/c/1/c1e8a0f7afd043345fc060a6bbef2f1570a94ea1.jpeg "Session State basics") ](https://www.youtube.com/watch?v=92jUAXBmZyU)

Please check if a solution for your problem has already been given by someone previously, before posting a question here 🙂

Cheers!  
Moiz

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/streamlit/original/3X/c/f/cf4c2ee0d938358f82448c5fe4b9c3c91d7ce19c.svg) [@system](https://discuss.streamlit.io/u/system)\
**Post date:** [February 7, 2024, 11:07am UTC](https://discuss.streamlit.io/t/do-not-reload-st-button-when-manipulation-st-selectbox/48743/3 "2024-02-07T11:07:38Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
