Overwriting a dict using selectbox and text_input


In my app, the user selects a parameter using a selectbox, the value of the selected parameter is then displayed in a text_input widget. The user can then change the value of the parameter which is stored in a dictionary. The code below works fine, however, I’ve noticed that if I try to change the value of a parameter which share the same value with another key in the dict, then the later gets affected by the change when it is selected with the selectbox. For example, changing a to something and then selecting b in the list works fine and output {'a': 'something', 'b': 'b', 'c': 'a'}. However, if c is selected instead of b, the value of c in the dict changes to something.
This has given me a headache for the past few hours so I’d be very grateful if someone could point my mistake :slight_smile:


import streamlit as st

# Load the dictionary and store it in the session state
if 'var' not in st.session_state:
    st.session_state.var = dict(a='a', b='b', c='a')

# Parameter select
key = st.selectbox('Parameter', (st.session_state.var.keys()))
st.write('Parameter selected %s' % key)

# Display the value
value = st.text_input('Value', st.session_state.var[key])

# Store the value
st.session_state.var[key] = value

# Write the dictionary content
st.write('The dictionary content is now %s' % st.session_state.var)