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.

3 Likes

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?

2 Likes

Hi @Marisa_Smith,

just wondering if there has been any updates on this please. I cannot use your solution because in my case this warning happens when defining some date_input with min_value and max_value values so the default value of date.today() falls outside that range triggering a StreamlitAPIException.

Thanks

1 Like

Hello @rafa-guides,
I got exactly the same error in my code. So far, I have found a temporary solution for myself to disable it in the streamlit source code. Just comment out the lines from 72-75 in the file:

..\.venv\Lib\site-packages\streamlit\elements\utils.py

My code example, when changing start_date, an error occurs.

import streamlit as st
from datetime import time, timedelta, datetime

today = datetime.now()

if not "all_day" in st.session_state:
    st.session_state.all_day = False

def time_check():
    sdt = st.session_state.start_date
    edt = st.session_state.end_date

    stm = st.session_state.start_time
    etm = st.session_state.end_time

    start = datetime.combine(sdt, stm)
    end = datetime.combine(edt, etm)
    if start > end:
        st.session_state.end_time = (start + timedelta(minutes=30)).time()
        st.session_state.end_date = (start + timedelta(minutes=30)).date()

container = st.container()
col1, col2, col3 = st.columns(3)
with container:
    with col1:
        start_date = st.date_input(
            "start date",
            min_value=today,
            value=today,
            on_change=time_check,
            key="start_date",
        )
        end_date = st.date_input(
            "end date",
            min_value=start_date,
            value=start_date,
            on_change=time_check,
            key="end_date",
            disabled=st.session_state.all_day,
        )

    with col2:
        start_time = st.time_input(
            "start time",
            value=time(8, 30),
            on_change=time_check,
            key="start_time",
            disabled=st.session_state.all_day,
        )
        end_time = st.time_input(
            "end time",
            value=time(9, 0),
            on_change=time_check,
            key="end_time",
            disabled=st.session_state.all_day,
        )

    with col3:
        with st.expander(label="Options", expanded=True):
            all_day = st.checkbox("All day", key="all_day")

A work-around for this problem is to set the default using session state rather than the widget parameter:

if "angle" not in st.session_state:
    st.session_state.angle = "beta"
st.radio(
    key="angle",
    label="Pick angle",
    options=["alpha", "beta", "gamma"],
)

This way the widget gets a default initial value from the session state instead of the index (or value) parameter, avoiding the warning about those parameters clashing with session state.

The solution from above doesn’t prevent the warning. For me the workaround that worked looked like this:

from streamlit.elements.utils import _shown_default_value_warning
_shown_default_value_warning = True
1 Like

I have the same problem! And none of the solutions provided here works for me (except I haven’t tried to change it in Streamlit source code because it will be a headache for deployment). I am using Streamlit v1.11.1.

1 Like

Yes, same problem here and I cannot seem to find a solution. Not sure what to do.

Same problem here. Has any solution been found in the meantime?

@betaigeuze The recommended solution is to not set a value in the widget if you’re also setting it with session state. If you have a situation where you’re not sure if the value has been set in session state, and you want to specify a default value in case it hasn’t, you can do something like this:

import streamlit as st

if st.checkbox("Set default value"):
    st.session_state["number_input"] = 5.0


# If the session state has a value for "number_input", use that value, and *don't* set a default value.
if "number_input" in st.session_state:
    st.number_input("Number input", key="number_input")
# Otherwise, set a default value.
else:
    st.number_input("Number input", key="number_input", value=5.0)
3 Likes

I’ve tried it but this is said by Pylance: _shown_default_value_warning" is not accessed
The warning message still shows… And I have the same problem as the author of this discuss, I have a date_input with min & max values and I use a key for changing a session state, and as he said, we can’t remove the value parameter of the date_input cause streamlit will use the current day of the user’s device, instead of the key session state value. Then giving an error as the current date is greater than the max value…

This warning is really annoying and doesn’t make sense to appear. I’ve looked on github issues and seems that no one opened one to look for this case.

I’ve opened an issue for this warning case. Hope someone can make a way to not show this.

1 Like

Hey @eduardochaves :wave:

Here’s how you can workaround the issue. Use one session state variable to hold the default value (e.g. “custom_date”), and another to assign as the widget key (e.g. “date_key”). If the widget key doesn’t exist in session state, assign the default value to the former variable:

import datetime
import streamlit as st

min_date = datetime.date(2020, 1, 1)
max_date = datetime.date(2020, 12, 31)

if "date_key" not in st.session_state:
    st.session_state["custom_date"] = min_date

st.date_input(
    "Date:",
    min_value=min_date,
    max_value=max_date,
    value=st.session_state["custom_date"],
    key="date_key",
)

st.write(st.session_state["date_key"])

This does not work for me. I get the same behavior where the warning shows the first time the app is launched.

If you describe your situation there should be a way to tweak the code to avoid the error. Usually when I encounter this, I have a bit more interaction happening so the exact tweak varies by situation…

Without actually modifying the code in streamlit/elements/utils.py I am not sure how to modify the _shown_default_value_warning variable. So the proposed solution of modifying it locally in the app script should not work since the utils script is not accessing the variable from the app script. The only way I can see to fix this is to modify the value in the utils script which I am trying not to do.

I’m suggesting not doing anything with utils.py at all and just changing the app code in a way to avoid the error. If you don’t trigger the warning, then it doesn’t matter if it’s set to show or not. :slight_smile:

It’s just the my advice on how to not trigger the warning is dependent on the scenario and the exact effect being created with the widget’s default value and state.

The proper fix is not passing a default value and a key in the same call.