Skip to content

Commit c4a26ac

Browse files
Update widget guide example apps
1 parent 13ccb1e commit c4a26ac

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
lines changed

content/develop/concepts/architecture/widget-behavior.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,12 @@ When Streamlit gets to the end of a script run, it will delete the data for any
230230

231231
### Retain statefulness when changing a widget's identity
232232

233-
If you just need to manipulate identity-affecting parameters without carrying the widget state between pages, you can use a callback to directly maintin a widget's state. Here is a solution to our earlier example of changing a slider's min and max values.
233+
If you just need to manipulate identity-affecting parameters without carrying the widget state between pages, you can use a callback to directly maintin a widget's state. Here is a solution to our earlier example of changing a slider's min and max values. Note that the widget's initial value is set through Session State and not its `value` parameter. When you are programmatically changing a widget, you should just use Session State to maintain the widget's state to avoid unexpected behavior.
234234

235235
```python
236236
import streamlit as st
237237

238+
# Set the default value for the widget
238239
st.session_state.setdefault("a", 5)
239240

240241
cols = st.columns(2)
@@ -250,7 +251,7 @@ def update_value():
250251

251252
# Validate the slider value before rendering
252253
update_value()
253-
st.slider("A", minimum, maximum, value=5, key="a")
254+
st.slider("A", minimum, maximum, key="a")
254255
```
255256

256257
<Cloud name="doc-guide-widgets-change-parameters-solution" height="250px"/>
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import streamlit as st
22

33
cols = st.columns([2, 1, 2])
4-
minimum = cols[0].number_input("Minimum", 1, 5)
5-
maximum = cols[2].number_input("Maximum", 6, 10, 10)
4+
minimum = cols[0].number_input("Minimum", 1, 3)
5+
maximum = cols[2].number_input("Maximum", 8, 10, 10)
6+
value = cols[1].number_input("Default", 4, 7, 5)
67

7-
st.slider("No default, no key", minimum, maximum)
8-
st.slider("No default, with key", minimum, maximum, key="a")
9-
st.slider("With default, no key", minimum, maximum, value=5)
10-
st.slider("With default, with key", minimum, maximum, value=5, key="b")
8+
st.slider("No key", minimum, maximum, value)
9+
st.slider("With a key", minimum, maximum, value, key="a")

python/api-examples-source/guides/widgets.change_parameters_best.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import streamlit as st
22

3-
# Set default value
4-
if "a" not in st.session_state:
5-
st.session_state.a = 5
3+
# Set the default value for the widget
4+
st.session_state.setdefault("a", 5)
65

76
cols = st.columns(2)
87
minimum = cols[0].number_input("Min", 1, 5, key="min")

0 commit comments

Comments
 (0)