Saving user inputs for next session

I am trying to save user inputs to json file for next session. When I save number input and import it, there is no problem, it works fine. I have select box user inputs too. It works well when I deleted the json file and run the code. However, it gives the error on screenshot when I change inputs. When I open the json file to check if it saves correctly, it looks ok

here the first run everything is ok
orijinal

here is what I see after changing the inputs
error

code to save number input
x = st.number_input(“x”, value=user_inputs.get(“x”,1267.41))
user_inputs[‘x’] = x;

code to save select box input
ent_type = st.selectbox(“Enterance Type”,user_inputs.get(“ent_type”,(“circular bellmount”,“square bellmount”,“r/D>=0.15 entrance”,“slightly rounded”,“square cornered”)))
user_inputs[‘ent_type’] = ent_type

thanks

Hi @ufk_sn ,

Your example code is not complete enough to run it by itself. However, I made some assumptions - see this example:

ent_type_options = (
    "circular bellmount",
    "square bellmount",
    "r/D>=0.15 entrance",
    "slightly rounded",
    "square cornered"
)

def save_ent_type():
    st.session_state.user_input_index = ent_type_options.index(st.session_state.ent_type)

st.selectbox(
    label="Enterance Type",
    key='ent_type',
    options=ent_type_options,
    index=st.session_state.get('user_input_index',0),
    on_change=save_ent_type
)

Note that the function save_ent_type saves the index of the selected option whenever the st.selectbox changes. The example uses Streamlit’s session state, but hopefully you can convert to your use case with json files. Let me know!

1 Like

Thank you very much for your time and reply,

I tried the following and solved the problem thanks to chat gpt :slight_smile:
ent_opt = (“circular bellmount”,“square bellmount”,“r/D>=0.15 entrance”,“slightly rounded”,“square cornered”)
ent_type = st.selectbox(f"Enterance Type{id}“, ent_opt, index=ent_opt.index(user_inputs.get(f"ent_type{id}”, “circular bellmount”)))