admin管理员组文章数量:1352015
In the example, Radio 1
and Radio 2
start with Option 2
as default value set via st.session_state
.
When I change the valid options (via button callback),
Radio 2
retains the value while Radio 1
resets itself to the first value in the list of options.
Both the option sets will always have Option 2
as valid value.
I am trying to understnad why the difference in these bhaviors?
Issue is same with selectbox, pills etc. Just easily understandable with radio.
What am I missing.
Here is the code
import streamlit as st
options = {
'sel1': [
['Option 1', 'Option 2'],
['Option 1', 'Option 2', 'Option 3'],
['Option 1', 'Option 2', 'Option 3', 'Option 4']
],
'sel2': [
['Option 1', 'Option 2', 'Option 3', 'Option 4'],
['Option 1', 'Option 2', 'Option 3', 'Option 4'],
['Option 1', 'Option 2', 'Option 3', 'Option 4']
]
}
def showRadio(stCol, radioId):
with stCol:
st.metric(
f'Radio {radioId}',
st.radio(
label='With different options across runs',
options=options[f'sel{radioId}'][st.session_state.idx],
index=0,
key=f'radio{radioId}'
)
)
def updateDIdx():
'Update the session_state.idx upon button click in callback'
st.session_state.idx = (st.session_state.idx + 1) % 3
def showButton(stCol):
with stCol:
st.button('Change Options', on_click=updateDIdx)
st.metric('Options Index: ', st.session_state.idx)
def radio():
'''The main function'''
st.set_page_config(page_title="Radio Example")
# Set the session_state on first run
if 'idx' not in st.session_state:
st.session_state.idx = 0
if 'radio1' not in st.session_state:
st.session_state.radio1 = 'Option 2'
if 'radio2' not in st.session_state:
st.session_state.radio2 = 'Option 2'
cols = st.columns(3)
showRadio(cols.pop(0), 1)
showRadio(cols.pop(0), 2)
showButton(cols.pop(0))
if __name__ == '__main__':
radio()
Here is the sample output .gif
Streamlit version: 1.44.0 Python: 3.12
本文标签: pythonSession state is not respected when the options are not identicalStack Overflow
版权声明:本文标题:python - Session state is not respected when the options are not identical - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743905479a2559442.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论