Read Text input from user in for loop

Hey Team,

We are trying to generate a quiz application using streamlit. Please find the code attached here:

fill_in_the_blanks = {'questions': ['question1','question2'], 'answers': ['answer1','answer2']}
ans = []
mark = 0
with st.form(key = id_generator()):
	for i,quest in enumerate(fill_in_the_blanks["questions"]):
		st.write(i+1,quest)
		x = st.text_input("Enter your answer here",key=id_generator())
		ans.append(x)
	submitted = st.form_submit_button(label='Submit')
	if submitted:
		# if answers are correct then
			mark = mark+1
	st.success("Test Score - "+str(mark))

Here, i am able to populate questions within the form widget. But, am not able to read text_input answers which is created with the help of for loop. How to read text_input value from user in this case. Please do the needful here.

Thanks,
Prakash
mail: prakash@deepsphere.ai

I am having a similar problem. Did you ever figure out how to read user input in a form, in a loop like this?

I am trying to loop over a list of images and their respective predicted labels, and ask the user if they think the label is correct. But I am unable to save the user input.

Here is my code snippet:

def form_callback(labels):
     st.write(labels)

images = [np.random.randn(100, 100, 3) for i in range(3)]   # example images
labels = []
with st.form("my_form"):
     for i,img in enumerate(images):
          st.image(img, clamp=True)
          chk = st.checkbox("Correct?", key=str(i))
          labels.append(chk)
     submit_button = st.form_submit_button(label='Submit', on_click=form_callback, args=(labels,))