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
Before clicking “Create Topic”, please make sure your post includes the following information (otherwise, the post will be locked).
- Are you running your app locally or is it deployed?
Answer: running locally - 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. - 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