Why do default values cause a session state warning?

It seems that we can’t set a default option on widgets without getting a warning like this on our dashboards:

The widget with key “reporting” was created with a default value but also had its value set via the Session State API.

What’s the rationale for this?

It doesn’t seem right because the point of a default is that it’s used when no other value is specified. If there’s a value in the session state then that should (silently) override the default. The default only applies if there’s no existing session state.

1 Like

Hi @quantoid

Thanks for posting your question.

Would you mind pasting the related code snippet, so we can delve into this please?

Thanks
Charly

Hey @quantoid,

When the team was designing st.session_state they had played around with a few options on how state would interact with parameters like value for specifying default ones. For right now, this is expected behaviour but the team is looking into changing it, along with some other changes they are considering for state.

We will circle back to this in the near future. In the meantime, you can remove the value parameter and that will remove the warning your seeing.

Hope this helps!
Happy Streamlit-ing!
Marisa

1 Like

Hello everybody,
I have another question about this issue: I have a case where I got such a warning for a number input widget without setting a value parameter. You can reproduce it with the following code:

    import streamlit as st
    def loop_slider_change():
        st.session_state.tmp_loop_number_input = st.session_state.tmp_current_loop_element

    def loop_number_input_change():
        st.session_state.tmp_current_loop_element = st.session_state.tmp_loop_number_input

    st.slider(
        label=f"Test with 10 entries",
        min_value=1,
        max_value=10,
        key="tmp_current_loop_element",
        on_change=loop_slider_change,
    )

    st.number_input(
        "Goto Index:",
        min_value=1,
        max_value=10,
        key="tmp_loop_number_input",
        on_change=loop_number_input_change,
    )

If you click in this toy example on the slider to change its inner state you get the warning. Is it a bug or do I have to code this interaction between a slider and a number_input in another way?

1 Like