# ───────────────────────────────────────────── # app/components.py # Reusable Streamlit UI building blocks. # Keeps main.py clean and focused on flow logic. # ───────────────────────────────────────────── import streamlit as st from src.mcq_builder import MCQ def render_question_card(mcq: MCQ, index: int) -> str | None: """ Render a question with labelled radio button options. Returns the selected option label ("A"/"B"/"C"/"D") or None. """ st.markdown(f"### Q{index + 1}. {mcq.question}") # Build labelled options: ["A. Paris", "B. London", ...] labelled_options = [f"{chr(65+i)}. {opt}" for i, opt in enumerate(mcq.options)] # Restore previous selection if user came back to this question prev_index = st.session_state.user_answers[index] default = prev_index if prev_index >= 0 else 0 selected = st.radio( label = "Select your answer:", options = labelled_options, index = default, key = f"q_{index}", label_visibility = "collapsed", ) # Return just the letter ("A", "B", etc.) return selected[0] if selected else None def render_result_card(result: dict, question_num: int): """ Render a single question's result with colour coding. Green = correct, Red = wrong. """ is_correct = result["is_correct"] icon = "✅" if is_correct else "❌" color = "#d4edda" if is_correct else "#f8d7da" border = "#28a745" if is_correct else "#dc3545" with st.container(): st.markdown( f"""
{feedback}