Programmatically Changing Text of Checkbox


Right now I have multiple streamlit checkboxes in the sidebar like this:

myFirstCheckbox = st.sidebar.checkbox("Original Text 1")
mySecondCheckbox = st.sidebar.checkbox("Original Text 2")
myThirdCheckbox = st.sidebar.checkbox("Original Text 3")

I also have a text input box in the main section:
youtubeLink = st.text_input('Youtube Link:')

I was wondering if it was possible to programmatically change the text of the checkboxes after someone enters stuff into the text input box?

I wrote this code but it doesn’t change the text of the original checkbox, it just adds new checkboxes to the bottom of the sidebar:

if youtubeLink:
    myFirstCheckbox = st.sidebar.checkbox("Next Text 1")
    mySecondCheckbox = st.sidebar.checkbox("Next Text 2")
    myThirdCheckbox = st.sidebar.checkbox("Next Text 3")

Hi @Audrey_Ha07, welcome to the Streamlit community!! :wave: :partying_face:

You can use st.empty to create placeholders your checkboxes, which you can then overwrite once the text input box is populated. For example:

import streamlit as st

placeholder1 = st.sidebar.empty()
placeholder2 = st.sidebar.empty()
placeholder3 = st.sidebar.empty()

myFirstCheckbox = placeholder1.checkbox("Original Text 1")
mySecondCheckbox = placeholder2.checkbox("Original Text 2")
myThirdCheckbox = placeholder3.checkbox("Original Text 3")

youtubeLink = st.text_input('Youtube Link:')

if youtubeLink:
    myFirstCheckbox = placeholder1.checkbox("Next Text 1")
    mySecondCheckbox = placeholder2.checkbox("Next Text 2")
    myThirdCheckbox = placeholder3.checkbox("Next Text 3")


Best, :balloon:

1 Like

Thank you! This code worked

1 Like