Streamlit multiselect, if I don't select anything, doesn't show data frame

Question:

I am building a Streamlit app. Part of my code includes multiselect as follows, when I dont select anything in multiselect, I want to show whole data frame without any filtration, but it doesn’t show any data, how should I modify the code?

code_= df_temp.CODE.unique().tolist()
type_ = df_temp.TYPE.unique().tolist()

options, options2  = st.columns([0.1, 0.1])
  
options = options.multiselect('Select Code', code_ )
options2 = options2.multiselect('Select Type', type_ )

df_filtered = time_filtered.query('CODE in @options or TYPE in @options2')
Asked By: user14269252

||

Answers:

Just do a if/else on the output of the .multiselect:

code_= df_temp.CODE.unique().tolist()
type_ = df_temp.TYPE.unique().tolist()

options, options2  = st.columns([0.1, 0.1])
  
options = options.multiselect('Select Code', code_ )
options2 = options2.multiselect('Select Type', type_ )

df = None

if not options and not options2:
    # nothing has been selected, don't filter
    df = time_filtered
else:
    # at least one option has been selected,
    # filter
    df = time_filtered.query('CODE in @options or TYPE in @options2')

st.text(f"options {options}")
st.text(f"options2 {options2}")

st.dataframe(df)
Answered By: vvvvv
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.