# How to checkbox value by code

**URL:** <https://discuss.streamlit.io/t/how-to-checkbox-value-by-code/63707>\
**Category:** Using Streamlit\
**Tags:** discussion\
**Created:** [March 2, 2024, 6:11pm UTC](https://discuss.streamlit.io/t/how-to-checkbox-value-by-code/63707 "2024-03-02T18:11:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ibaha](https://avatars.discourse-cdn.com/v4/letter/i/c57346/32.png) [@ibaha](https://discuss.streamlit.io/u/ibaha)\
**Post date:** [March 2, 2024, 6:11pm UTC](https://discuss.streamlit.io/t/how-to-checkbox-value-by-code/63707/1 "2024-03-02T18:11:46Z")

</div>

Is there a way to set the checkbox value (False/True) by code? I have a checkbox that doesn’t retain its value when I switch between pages of page=st.sidebar.selectbox. For example, I click the checkbox, then switch the page, and when I get back to original page where the checkbox is, it is unchecked. This should be simple but couldn’t seem to get it working; see below. Appreciate any comments.

```auto
if 'BoundingStiffnessFlag' not in st.session_state:
        st.session_state.BoundingStiffnessFlag = st.checkbox('Enable Bounding Stiffness Analysis',value=False, on_change=BoundingStiffnessFlag_callback_fcn, key='BoundingStiffnessFlag_Key')
# 
 st.session_state.BoundingStiffnessFlag_Key = st.session_state.BoundingStiffnessFlag

def Page_on_change_callback_fcn():
    if 'BoundingStiffnessFlag' in st.session_state:
        st.session_state.BoundingStiffnessFlag= st.session_state.BoundingStiffnessFlag_Key

```

---

<div class="post-metadata">

**Author:** ![ferdy](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/ferdy/32/29957_2.png) [@ferdy](https://discuss.streamlit.io/u/ferdy)\
**Post date:** [March 3, 2024, 1:00am UTC](https://discuss.streamlit.io/t/how-to-checkbox-value-by-code/63707/2 "2024-03-03T01:00:02Z")

</div>

The checkbox has a parameter called value. This is responsible in updating the ui with check or not.

 ![image](https://global.discourse-cdn.com/streamlit/original/3X/4/1/41002ec0152129b828995e46de29c373e16b60fa.png)

### Example

```auto
st.checkbox(label='Absent', value=ss.absent, key='cb', on_change=update)

```

For multi-pages app we need to create **a session variable and assign it to the value** so that it will be seen by other pages too. Session variables are visible to other pages.

This is our main page. Please pay attention to the comments.

_streamlit\_app.py_

```auto
import streamlit as st
from streamlit import session_state as ss

# Define a session variable to store the state of the checkbox
# and assign it to the value parameter of the said checkbox.
if 'absent' not in ss:
    ss.absent = False

def update():
    """A function to be called once there is a change in the checkbox.
    
    This is an oppurtunity for us to assign the checkbox
    value to our session variable.
    """
    ss.absent = ss.cb

# This checkbox is a bit busy, we use the value, key and on_change parameters.
st.checkbox(label='Absent', value=ss.absent, key='cb', on_change=update)
st.write(f'Absent: {ss.absent}')

selected = st.selectbox('Select Page', options=[None, 'page1', 'page2'])
if selected:
    st.switch_page(f'pages/{selected}.py')

```

In other pages.

_pages/page1.py_ and _pages/page2.py_

```auto
import streamlit as st
from streamlit import session_state as ss

# Define it here too, in case of page refresh, we will not get an error.
if 'absent' not in ss:
    ss.absent = False

st.write(f'Absent: {ss.absent}')

```

We use two pages to test if the value of our session variable will persist if the users navigate from page1 to page2 and vice versa.

---

<div class="post-metadata">

**Author:** ![ibaha](https://avatars.discourse-cdn.com/v4/letter/i/c57346/32.png) [@ibaha](https://discuss.streamlit.io/u/ibaha)\
**Post date:** [March 3, 2024, 3:39am UTC](https://discuss.streamlit.io/t/how-to-checkbox-value-by-code/63707/3 "2024-03-03T03:39:28Z")

</div>

This works, thanks a lot, I really appreciate it.

---

<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:** [March 5, 2024, 3:40am UTC](https://discuss.streamlit.io/t/how-to-checkbox-value-by-code/63707/4 "2024-03-05T03:40:22Z")

</div>

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