英文:
Difficulty initializing complex session_state for inputs in Streamlit
问题
这是我的代码,尽管我初始化了会话状态,但我遇到了冲突。
import streamlit as st
if "user_inputs" not in st.session_state:
st.session_state["user_inputs"] = True
# 遍历8个问题
for i in range(8):
# 获取问题
question = f'Question {i+1}'
# 添加到页面
st.subheader(question)
# 创建3个复选框选项
checkbox1 = st.checkbox('Option 1')
checkbox2 = st.checkbox('Option 2')
checkbox3 = st.checkbox('Option 3')
# 将复选框输入保存在会话状态对象中
st.session_state.user_inputs[f'{question}_checkbox_1'] = checkbox1
st.session_state.user_inputs[f'{question}_checkbox_2'] = checkbox2
st.session_state.user_inputs[f'{question}_checkbox_3'] = checkbox3
# 创建整数输入
integer_input = st.number_input('Integer Input')
# 将整数输入保存在会话状态对象中
st.session_state.user_inputs[f'{question}_integer_input'] = integer_input
# 创建滑块
slider = st.slider('Slider', 0, 100)
# 将滑块值保存在会话状态对象中
st.session_state.user_inputs['slider'] = slider
# 添加提交按钮
if st.button('Submit'):
st.success('表单已提交!')
我尝试了不同的初始化方式,但它们都没有起作用。当我尝试像st.session_state.user_inputs = "test"
或st.session_state["user_inputs"] = "test"
这样的方式时,我仍然遇到相同的错误:
错误信息如下:
st.session_state没有属性"user_inputs"。您是否忘记初始化它?更多信息:
https://docs.streamlit.io/library/advanced-features/session-state#initialization
我尝试创建一种表单,但我收到了一个重复的错误。
英文:
This is my code and although I initialize the session state, I'm having a conflict with it.
import streamlit as st
if "user_inputs" not in st.session_state:
st.session_state["user_inputs"] = True
# Loop over the 8 questions
for i in range(8):
# Get the question
question = f'Question {i+1}'
# Add it to the page
st.subheader(question)
# Create 3 checkbox options
checkbox1 = st.checkbox('Option 1')
checkbox2 = st.checkbox('Option 2')
checkbox3 = st.checkbox('Option 3')
# Save the checkbox inputs in the session state object
st.session_state.user_inputs[f'{question}_checkbox_1'] = checkbox1
st.session_state.user_inputs[f'{question}_checkbox_2'] = checkbox2
st.session_state.user_inputs[f'{question}_checkbox_3'] = checkbox3
# Create an integer input
integer_input = st.number_input('Integer Input')
# Save the integer input in the session state object
st.session_state.user_inputs[f'{question}_integer_input'] = integer_input
# Create a slider
slider = st.slider('Slider', 0, 100)
# Save the slider value in the session state object
st.session_state.user_inputs['slider'] = slider
# Add a submit button
if st.button('Submit'):
st.success('Form submitted!')
I tried different ways to initialize but they didn't work. When I try something like st.session_state.user_inputs = "test"
or st.session_state["user_inputs"] = "test"
, again I have same error:
The error is:
st.session_state has no attribute "user_inputs". Did you forget to
initialize it? More info:
https://docs.streamlit.io/library/advanced-features/session-state#initialization
I tried to create a kind of form but I received a repetitive error.
答案1
得分: 1
代码部分已被排除,以下是内容的中文翻译:
你的主要代码块出现错误,错误信息为TypeError: 'bool' object does not support item assignment
。
这是有道理的,因为st.session_state["user_inputs"] = True
意味着你设置了一个布尔值,而不是一个字典,因此当你执行以下操作时:
st.session_state.user_inputs[f'{question}_checkbox_1'] = checkbox1
你实际上在做:
True[f'{question}_checkbox_1'] = checkbox1
从而引发错误。相反,可以使用:
if "user_inputs" not in st.session_state:
st.session_state["user_inputs"] = {}
另外,你有重复的键输入。尝试为这些添加唯一的键:
checkbox1 = st.checkbox('Option 1', key=f'{i}-1')
checkbox2 = st.checkbox('Option 2', key=f'{i}-2')
checkbox3 = st.checkbox('Option 3', key=f'{i}-3')
# ...
integer_input = st.number_input('整数输入', key=i)
还要注意,带键的用户输入会被自动添加到状态,这有点令人意外,但这就是Streamlit。如果你更喜欢显式设置它们,我建议使用实际的嵌套结构,如二维列表或字典,而不是带有连接标识符的键。
最后,你可以使用st.form
来将所有输入项分组为一个集合,以避免触发重新渲染。
英文:
The error I get for your main block of code is TypeError: 'bool' object does not support item assignment
.
This makes sense, because st.session_state["user_inputs"] = True
means you've set up a boolean, not a dictionary, so when you do:
st.session_state.user_inputs[f'{question}_checkbox_1'] = checkbox1
you're basically doing:
True[f'{question}_checkbox_1'] = checkbox1
giving the error. Instead, use:
if "user_inputs" not in st.session_state:
st.session_state["user_inputs"] = {}
Next, you have duplicate keyed inputs. Try adding unique keys to these:
checkbox1 = st.checkbox('Option 1', key=f'{i}-1')
checkbox2 = st.checkbox('Option 2', key=f'{i}-2')
checkbox3 = st.checkbox('Option 3', key=f'{i}-3')
# ...
integer_input = st.number_input('Integer Input', key=i)
Note also that keyed user inputs are automatically added to state, which is a bit surprising, but that's Streamlit for you. If you do prefer to explicitly set them, I'd use actual nested structures like 2d lists or dicts rather than keys with concatenated identifiers.
Finally, you may want to use st.form
to group all of your inputs into a collection, avoiding triggering rerenders.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论