Determine when SelectBox is changed

Hey guys

I’m looking for a way to determine when a selectbox is used. After that and only after that i want other functions to take action.

I have been using the key option for a reset button but if used to run or not run the filter as below this would always run the filter upon refresh because of the default value being “Any”. But what i need is that the filter function only runs when a change within the selectbox is made.

I hope i could make it somewhat clear what i’m trying to achieve.

Steps to reproduce

st.cache_data()
pulls a dataframe which uses index as parameter

Index = st.sidebar.selectbox(‘Index:’,[‘Any’,‘A’, ‘B’,‘C’],key=‘Index’)

#Reset Button

def reset():
st.session_state.Index = ‘Any’
st.sidebar.button(‘Reset Filters’, on_click=reset)

#I want to achieve is something like:

if Index:
st.cache_data.clear()

if Index:
run filter function

else:
dont run filter function

What about adding on another option of ‘None’ and make that the default:

import streamlit as st

index = st.sidebar.selectbox('Index',['None','Any','A','B','C'])

if index == 'None':
    st.stop()

st.write(f'executing code for index: {index}')
1 Like

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