Multiselect is getting reset, when i change another multi select in different form

Summary

This code is to reproducing my issue, i have multiple forms and each form has it’s multi select, while i change multi select ‘m1’ from form 1, multi select ‘m2’ from form2 is getting reset, i would like ‘m2’ persistence it’s last selected values.

Steps to reproduce

Code snippet:

import streamlit as st


def main():
    with st.form("form_1"):
        selected_m1 = st.multiselect(label="Select ABC",
                                     options=m_1,
                                     key="selected_m1_tmp")
        st.form_submit_button("Apply")

    with st.form("form_2"):
        m2_option = [s for s in m_2 if s.startswith(
            tuple(st.session_state.selected_m1_tmp))]
            
        selected_m2 = st.multiselect(label="Select ABC sub",
                                     options=m2_option,
                                     key="selected_m2_tmp")
        st.form_submit_button("Apply")
        selected_m2


if __name__ == "__main__":
    m_1 = ["a", "b", "c"]
    m_2 = ["a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3"]
    main()

Steps to reproduce:

  1. Run given snippet
  2. Select “a” and ‘b’ in first multi select and click apply
  3. select “a1” and “b1” in second multi select and click apply
  4. Now add “c” to first multi select and click apply
  5. You will see second multi select is getting reset to empty instead holding previously selected “a1” and “b1”

Expected behavior:

Expected behavior is to persist previous selected values in multi select in form 2 when we change form 1 multi select.

I have already try setting session but was not successful. Thank you.

1 Like

I end up managing session with some condition; Better solution or feedback would appreciate.

import streamlit as st
import time

m_1 = ["a", "b", "c"]
m_2 = ["a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3"]


def get_m2_options(selected_m1):
    return [s for s in m_2 if s.startswith(tuple(selected_m1))]


def update_m2_options(pre_selected_m1, selected_m1):
    # If there are change in selection compare to prior
    if pre_selected_m1 != selected_m1:
        st.write(time.time())
        st.session_state.m2_option = get_m2_options(
            selected_m1)   # Update m2 option based on m1 selection
        st.session_state.selected_m2_tmp = list(
            set(st.session_state.selected_m2) & set(st.session_state.m2_option))  # Filter out selected m2 based on m1 selection


def main():
    with st.form("form_1"):
        selected_m1 = st.multiselect(label="Select ABC",
                                     options=m_1,
                                     key="selected_m1_tmp")
        if st.form_submit_button("Apply"):
            update_m2_options(st.session_state.selected_m1, selected_m1)
            st.session_state.selected_m1 = selected_m1

    with st.form("form_2"):
        selected_m2 = st.multiselect(label="Select ABC sub",
                                     options=st.session_state.m2_option,
                                     key="selected_m2_tmp")
        st.session_state.selected_m2 = selected_m2
        st.form_submit_button("Apply")


if __name__ == "__main__":
    # Initialize session state variables
    st.session_state.setdefault('selected_m1', [])
    st.session_state.setdefault('selected_m2', [])
    st.session_state.setdefault('m2_option', [])
    main()

1 Like

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