Prevent default value of multiselect from being deleted?


I have a multiselect with a single default option, because it is required for the app to work. I wrote a callback to capture when the user deleted the default and warned them that it would be added back to the selected list. However, this does not work.

st.multiselect("RANK BY",

def query_rankby_changed(self):
        if 'SERIAL_NUMBER' not in st.session_state.q_rank_by:
            alert_user(self._messenger, level='warning', msg="The rank-by argument must contain the 'SERIAL_NUMBER' column, adding it back in")
            st.session_state.q_rank_by.insert(0, 'SERIAL_NUMBER')

I know I could just hard-code SERIAL_NUMBER and only allow users to add to it, but I would like it visible, so the user knows it is being selected.


I’m not sure how your larger class is structured, but this works for me:

import streamlit as st

def validate():
    if 'A' not in st.session_state.multiselect:
        st.session_state.multiselect = ['A']+st.session_state.multiselect

st.multiselect('Multiselect', ['A', 'B', 'C', 'D', 'E'],default='A', on_change=validate, key='multiselect')

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