Form to settle another form's tiebreaker

Summary

I have a form that a user enters their values into. Following submission, the code calculates values and displays results. Only the top two should be displayed, but sometimes there can be more than two top results. I have built a second form to display for the user to uncheck the value they disagree with and only pick the top two results.

Steps to reproduce

Code snippet:

placeholder = st.empty()

def finalResultsSelection(results):
    with placeholderFinal.form('form-results-tie'):
        st.write('You have more than two top results. Please select the top two which you feel fit you best.')
        for result in results:
            finalResults = st.checkbox(f'{result[0]}', [x[0] for x in results])
            st.write(f'Score: {result[1]}')
            st.write(f'Description: {result[2]}')
        submittedFinal = st.form_submit_button('Submit Final Results')

        if submittedFinal:
            st.write('Hello from the end of the second form submission.')
            st.write('''
            Here are your results:
            ''')
            finalResults = results
            for result in results:
                st.write(f'{result[0]}')
                st.write(f'Gift description: {result[2]}')
                st.write(f'Score: {result[1]}')

with placeholder.form('form1'):

    firstName = st.text_input('First name', '')
    lastName = st.text_input('Last name', '')
    email = st.text_input('Email', '')

    for question in questions:
        question['result'] = st.radio(f'{question["id"]}\) {question["question"]}', (0, 1, 2, 3), horizontal=True)
    
    submitted = st.form_submit_button('Submit')

if submitted:
    results = aggregator(questions)
    placeholder.empty()
    placeholderFinal = st.empty()
    if len(results)==2:
        st.write('''
        Here are your results:
        ''')
        finalResults = results
        for result in results:
            st.write(f'{result[0]}')
            st.write(f'Description: {result[2]}')
            st.write(f'Score: {result[1]}')
    elif len(results)>2:
        finalResultsSelection(results)

However, after submitting the final form to settle the tie, the page resets and all data is wiped out. No final results are displayed. Nothing under the if submittedFinal statement is run.

Debug info

  • Streamlit version: 1.22.0
  • Python version: 3.9.13

I appreciate any help or guidance in advance!

Hi @anchorrust

Owing to the page logic, upon evaluating changes or clicks to input widgets, the page reloads and reruns from top-to-bottom. To circumvent this and retain the data and state of the app, you’ll need to use Session State.

Here are relevant links to the Streamlit Docs:

And a video on Session State from the Streamlit YouTube channel:

Hope this helps!

Best regards,
Chanin

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.