I’m trying to make an annotation tool using streamlit, my problem boils down to this:
import streamlit as st
if 'idx' not in st.session_state:
st.session_state.idx = 0
some_people = ['Anders', 'Anna', 'Gustav']
def get_next_item():
st.session_state.idx += 1
with st.form(key='person', clear_on_submit=True):
person = some_people[st.session_state.idx]
st.write(f"Person is {person}")
person_checks_box = st.checkbox(f"{person} checks box?")
submitted = st.form_submit_button('next')
if submitted:
st.write(person_checks_box) # in reality this is a call to a database
get_next_item()
I iterate over items in a list and try to gather data about every item (in this case only a checkbox, but it could be a text field or multiple checkboxes). The problem is that after being presented with “Person is Anders” and I click “next” for the first time, Anders appears again. But his only happens once, subsequent clicks on “next” actually gives the next person.
I instead tried to make get_next_item
a callback on the submit button, like this
import streamlit as st
if 'idx' not in st.session_state:
st.session_state.idx = 0
some_people = ['Anders', 'Anna', 'Gustav']
def get_next_item():
st.session_state.idx += 1
with st.form(key='person', clear_on_submit=True):
person = some_people[st.session_state.idx]
st.write(f"Person is {person}")
person_checks_box = st.checkbox(f"{person} checks box?")
submitted = st.form_submit_button('next', on_click=get_next_item)
if submitted:
st.write(person_checks_box) # in reality this is a call to a database
That solves the problem of showing Anders twice, but now person_checks_box
is always false, even if I have checked the box.
I assume there is something I’m missing here?