Skip to content

Deleting a key from Session State WILL ~~not~~ revert the widget to a default value #1489

Description

@Varos8

The following statement in the docs dont seem to be true to me. I cant replicate the behaviour stated.

Doc Page: https://docs.streamlit.io/develop/concepts/architecture/widget-behavior
"

Calling a widget command when the widget already exists

When rerunning a script without changing a widget's identity:

Streamlit will connect to the existing frontend and backend parts.
If the widget has a key that was deleted from st.session_state, then Streamlit will recreate the key using the current frontend value. This is because deleting a key from Session State will not revert the widget to a default value.
The widget command will return the current value of the widge
"

Here is the code I used to replicate the stated behaviour:
"

import streamlit as st

st.title("Streamlit Widget Behavior Demo")

if "my_slider" in st.session_state:
del st.session_state["my_slider"]

Initialize a widget with a key.,

current_val = st.slider("Move this slider:", min_value=0, max_value=100, value=0, key="my_slider")

Display the current state

st.write(f"The widget command returned: {current_val}")
st.write("Current st.session_state contents:", st.session_state.to_dict())

"

The widget command does return the default value if the related key is deleted beforehand.
Please explain to me why this is the case if I misunderstodd the docs.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions