How to avoid sidebar getting refreshed automatically

I am working on search filter in streamlit says when we enter a text and searching and filter the matched fields from the orginal list and update the sidebar accordingly. The issue I am facing is that when i filtered itself the sidebar is getting refreshed. I dont want this behavior.

Please provide me a solution using st.cache_resource.
Streamlit version – 1.29.0
python – 3.11.5

:rotating_light: Before clicking “Create Topic”, please make sure your post includes the following information (otherwise, the post will be locked). :rotating_light:

  1. Are you running your app locally or is it deployed?
    Answer: running locally
  2. If your app is deployed:
    a. Is it deployed on Community Cloud or another hosting platform?
    b. Share the link to the public deployed app.
  3. Share the link to your app’s public GitHub repository (including a requirements file).
    import streamlit as st

if “checkbox_values” not in st.session_state:
st.session_state.checkbox_values =
if “original_checkbox_values” not in st.session_state:
st.session_state.original_checkbox_values =
if “counter” not in st.session_state:
st.session_state.counter = 0

def formSideBarData():
try:
showOptions_placeholder.empty()
with showOptions_placeholder.container():
with st.expander(“Please submit your choice:”, expanded=True):
st.session_state.checkbox_values.sort()
for eachLabel in st.session_state.checkbox_values:
tempkey = eachLabel + “-key”
st.checkbox(eachLabel, key=tempkey)
except Exception as ex:
print(“Error in formSideBarData method”, ex)

def applyFilter():
try:
st.session_state.checkbox_values = list(filter(lambda k: st.session_state[‘txt’].lower() in k.lower(), st.session_state.original_checkbox_values))
except Exception as ex:
print(“Error in applyFilter method”, ex)

def addSearchFilterOption():
try:
searchOption_placeholder.empty()
with searchOption_placeholder.container():
with st.expander(“Search Filter Options”, expanded=True):
st.text_input(“label”, placeholder=“Enter a text to filter the results”,
label_visibility=“collapsed”, key=“txt”)
st.button(“Apply Filter”, on_click=applyFilter)
except Exception as ex:
print(“Error in addSearchFilterOption method”, ex)

@st.cache_resource(experimental_allow_widgets=True)
def sidebardata():
try:
addSearchFilterOption()
formSideBarData()
except Exception as ex:
print(“Error in sidebardata method:”,ex)

with st.sidebar:
searchOption_placeholder = st.sidebar.empty()
showOptions_placeholder = st.sidebar.empty()
for x in range(5):
st.session_state.counter = st.session_state.counter + 1
testData = “TestNumber” + str(st.session_state.counter)
st.session_state.checkbox_values.append(testData)
st.session_state.original_checkbox_values = st.session_state.checkbox_values
sidebardata()

def main():
st.title(“Sample POC”)

if name == “main”:
main()
4. Share the full text of the error message (not a screenshot).
5. Share the Streamlit and Python versions.
Streamlit version – 1.29.0
python – 3.11.5

Hi @Techie

Perhaps you can use st.form so that the app does not refresh whenever one of the widget changes (and subsequent triggering an app refresh).

More info on this in the Docs:

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