My app resets when i click a button

Hello guys

So i have this code

import streamlit as st
from streamlit.runtime.state import session_state
from backend import *

if 'generate' not in st.session_state:
    st.session_state.generate = False

if 'submit_quiz' not in st.session_state:
    st.session_state.submit_quiz = False

def callback():
    st.session_state.generate = True

def stop():
    st.session_state.submit_quiz = True

def validate_input(user_input, num_of_questions):
    valid = True
    if user_input == "":
        st.error("You entered an invalid input. Try again.")
        valid = False

    if num_of_questions <= 0 or not num_of_questions.is_integer():
        st.error("You entered an invalid number of questions. Try again.")
        valid = False

    return valid

def create_quiz(generated_questions):
    generated_questions = generated_questions.strip().split("\n")
    # st.write(f"len {generated_questions}")
    i = 0
    answers = []
    while i < len(generated_questions):
        if generated_questions[i + 1] == '':
            answer =[i],
                              [generated_questions[i + 2], generated_questions[i + 3], generated_questions[i + 4],
                               generated_questions[i + 5]]
            i = i + 7
            answer =[i],
                              [generated_questions[i + 1], generated_questions[i + 2], generated_questions[i + 3],
                               generated_questions[i + 4]]

            i = i + 6

    return answers

def main():
    user_input = st.text_input("What is your interest?")
    num_of_questions = st.number_input("How many Questions you'd like to be asked?",
                                       placeholder="Enter a number...",
    generate_quiz = st.button("Take Quiz", on_click=callback)
    passed = False

    if generate_quiz:
        "state generate ", st.session_state
        if validate_input(user_input, num_of_questions):
            passed = True
    if passed:
        "state passed ", st.session_state
        with st.form("Quiz"):
            questions = generate_questions(user_input, num_of_questions)
            answers = create_quiz(questions)
            st.session_state.generate = False
            quiz_submitted = st.form_submit_button("Submit Quiz",on_click=stop)
            "state test ",st.session_state
            if quiz_submitted:
                "state submit ",st.session_state
                correct_answers = evaluate_answers(questions, answers)
                st.write("Quiz Results:")
                for i in range(len(questions)):
                    st.write(f"Question {i + 1}: {questions[i]}")
                    st.write(f"Your Answer: {answers[i]}")
                    st.write(f"Correct Answer: {correct_answers[i]}")
                    st.write(f"Result: {'Correct' if correct_answers[i] == answers[i] else 'Incorrect'}")

if __name__ == "__main__":

now whenever i click on the submit quiz button the app reset, did some debugging turned out the if statement for the submit quiz is unreachable i tried everything but nothing is working

Hi @aseel_wajieh,

Thanks for posting!

Can you share the link to your repo? I’m interested in troubleshooting the whole app for you to find a working solution. I need the code for the backend: from backend import *

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