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

Open Beginner friendly
#1,489 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
64/100
Issue type
Documentation
Clarity
Mostly clear
Activity status
Quiet
Tech stack
python
Domain
documentation

Research direction

Start with the widget behavior documentation page and reproduce the provided Python snippet in Streamlit. Compare the observed reset to the current wording about deleting a key from st.session_state. Done means the page accurately explains the behavior and its example no longer contradicts the documented result.

Written by the indexing model from the issue text.

Description

docs needs triage

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.

Dominant language
JavaScript
Stars
178
Forks
705
Avg merge
1d 13h
Merged PRs (30d)
39

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from streamlit/docs

All issues in streamlit/docs

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.