Persisting selectbox choice when options change

Hi guys,

I am having trouble wrapping my head around this one. The challenge is, that I want to persist selectbox choice even if the options change (unless the option is removed). I’ve tried using the sessionstate gist for it, but I am running into similar to this one. It is the exactly same logic in the behaviour as @karriebear describes nicely in the linked issue, but I’ve got some problems finding a fix in this case… Anyone has a good idea?

Below is a reproducible snippet

import streamlit as st
import session_state

state = session_state.get(letter="a")

available_letters = st.multiselect("Available letters", ["a", "b", "c", "d", "e"], default=["a", "b", "c"])

if state.letter in available_letters:
    default_idx = available_letters.index(state.letter)
else:
    default_idx = 0
    st.warning(f"{state.letter} no longer in available letters")

letter = st.selectbox("Select letter", available_letters, index=default_idx)
state.letter = letter