Hello!
I’m trying to create an interface where users can cycle through a set of responses
(in this case they are 0/1 responses, so we use checkboxes to represent them) and change them. I created a parallel correction
dictionary to capture the (potentially) changed data. The idea is, someone previously provided a set of responses and the current user is simply validating them, making any changes necessary. In the current setup, we would simply compare responses and corrections to see any changes after the fact.
I currently have the following app which does not store the corrections properly. When I provide unique keys for st.checkbox
it stores those in session state appropriately, but not in the session state dictionary. For example, if I “correct” the first example to be true/true (instead of true/false) here’s how the session_state
looks like this:
{
"index 0:question 2": true,
"index 0:question 1": true,
"correction": {
"0": [
true,
false
]
"count": 1,
"FormSubmitter:My form-Next": true,
"max_index": 3,
}
}
I have a feeling my approach is a bit off – help! How should I be storing these corrections? I may doing something wrong with the on_click
callback. But have no idea how to start. Thanks for your help!
import pandas as pd
import streamlit as st
def next_point(ci):
# store responses in session
st.session_state.correction[st.session_state.count ] = ci
# increment count
if st.session_state.count < st.session_state.max_index:
st.session_state.count += 1
st.sidebar.write(st.session_state)
responses = pd.DataFrame.from_dict({
"question 1": [1, 0, 0],
"question 2": [0, 1, 0],
}).to_dict(orient='index')
st.session_state.max_index = len(responses) - 1
if "count" not in st.session_state:
st.session_state.count = 0
st.session_state.correction = {}
with st.form("My form", clear_on_submit=True):
count = st.session_state.count
# extract responses
ri = responses[count]
# create two checkboxes filled with existing responses
ci = [st.checkbox(var, ri[var], key = 'index '+str(count)+":"+str(var)) for var in ri.keys()]
submit = st.form_submit_button(label="Next", on_click=next_point, kwargs={"ci": ci})