import streamlit as st import importlib # List of available modules with shorter names and icons module_names = { "Bases": "presentation_bases", "Validity": "valid_invalid_numbers", "Conversion": "conversion_bases", "Grouping": "grouping_techniques", "Addition": "addition_bases", "2's Complement": "twos_complement", "Negative Numbers": "negative_binary", "Subtraction": "subtraction_bases", } # Initialize session state for tracking answers and progress if 'correct_count' not in st.session_state: st.session_state.correct_count = 0 if 'question_count' not in st.session_state: st.session_state.question_count = 0 if 'current_module' not in st.session_state: st.session_state.current_module = None # Streamlit interface st.sidebar.title("Quiz Modules") module_name = st.sidebar.radio("Choose a module:", list(module_names.keys()), index=0) if module_name: module_file = module_names[module_name] try: module = importlib.import_module(f'modules.{module_file}') generate_question = module.generate_question title = getattr(module, 'title', f"{module_name} Module") description = getattr(module, 'description', "Description is not available") # Show module title and description st.title(title) st.write(description) # Add spacing to ensure question visibility on Hugging Face st.markdown("

", unsafe_allow_html=True) # Generate and display the question question, options, correct_answer, explanation = generate_question() st.write(f"**Question:** {question}") answer = st.radio("Choose an answer:", options) # Track user's response if st.button("Submit"): st.session_state.question_count += 1 if answer == correct_answer: st.session_state.correct_count += 1 st.success("Correct!", icon="✅") else: st.error("Incorrect.", icon="❌") st.write(f"**Explanation:** {explanation}") # Option to go to the next or previous question col1, col2 = st.columns(2) if col1.button("Previous Question"): st.experimental_rerun() # To simulate going back (if history is managed) if col2.button("Next Question"): st.experimental_rerun() # To simulate moving forward # Show percentage of correct answers if st.session_state.question_count > 0: correct_percentage = (st.session_state.correct_count / st.session_state.question_count) * 100 st.write(f"**Correct Answers:** {st.session_state.correct_count}/{st.session_state.question_count} ({correct_percentage:.2f}%)") except ModuleNotFoundError: st.error(f"The module '{module_name}' was not found. Please select another module.")