pythondataframestreamlit

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


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')

Solution

  • 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)