Sina Media Lab
		
	commited on
		
		
					Commit 
							
							·
						
						bbc7ac2
	
1
								Parent(s):
							
							62b8133
								
Updates
Browse files
    	
        app.py
    CHANGED
    
    | @@ -2,6 +2,7 @@ import streamlit as st | |
| 2 | 
             
            import os
         | 
| 3 | 
             
            from fpdf import FPDF
         | 
| 4 | 
             
            import uuid
         | 
|  | |
| 5 |  | 
| 6 | 
             
            # Initialize session state variables
         | 
| 7 | 
             
            if 'session_id' not in st.session_state:
         | 
| @@ -38,12 +39,10 @@ def generate_pdf_report(): | |
| 38 | 
             
                pdf.ln(10)
         | 
| 39 |  | 
| 40 | 
             
                for module, data in st.session_state.module_question_count.items():
         | 
| 41 | 
            -
                     | 
| 42 | 
            -
                     | 
| 43 | 
             
                    pdf.cell(200, 10, txt=f"Module: {module}", ln=True, align="L")
         | 
| 44 | 
             
                    pdf.ln(5)
         | 
| 45 | 
            -
                    pdf.cell(200, 10, txt=f"Correct Answers: {correct_count}/{total_count}", ln=True, align="L")
         | 
| 46 | 
            -
                    pdf.ln(5)
         | 
| 47 |  | 
| 48 | 
             
                    for entry in st.session_state.questions:
         | 
| 49 | 
             
                        if entry['module'] == module:
         | 
| @@ -55,23 +54,43 @@ def generate_pdf_report(): | |
| 55 | 
             
                                entry['explanation'],
         | 
| 56 | 
             
                                entry['step_by_step_solution']
         | 
| 57 | 
             
                            )
         | 
| 58 | 
            -
                             | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 59 | 
             
                            for option in options:
         | 
| 60 | 
             
                                if option == correct:
         | 
| 61 | 
             
                                    pdf.set_text_color(0, 128, 0)  # Green for correct
         | 
| 62 | 
            -
                                    pdf. | 
|  | |
| 63 | 
             
                                elif option == selected:
         | 
| 64 | 
             
                                    pdf.set_text_color(255, 0, 0)  # Red for incorrect
         | 
| 65 | 
            -
                                    pdf. | 
|  | |
| 66 | 
             
                                else:
         | 
| 67 | 
             
                                    pdf.set_text_color(0, 0, 0)  # Default color for others
         | 
| 68 | 
            -
                                    pdf. | 
| 69 | 
            -
             | 
| 70 | 
            -
                            pdf. | 
|  | |
|  | |
|  | |
|  | |
|  | |
| 71 | 
             
                            pdf.ln(5)
         | 
| 72 | 
            -
             | 
|  | |
|  | |
|  | |
| 73 | 
             
                            for step in step_by_step_solution:
         | 
| 74 | 
            -
                                pdf.multi_cell(0, 10, step)
         | 
| 75 | 
             
                            pdf.ln(10)
         | 
| 76 |  | 
| 77 | 
             
                    pdf.ln(10)  # Add space after each module
         | 
| @@ -85,7 +104,7 @@ def load_modules(): | |
| 85 | 
             
                    if filename.endswith(".py") and filename != "__init__.py":
         | 
| 86 | 
             
                        module_name = filename[:-3]
         | 
| 87 | 
             
                        # Dynamically import the module only when needed
         | 
| 88 | 
            -
                        module =  | 
| 89 | 
             
                        modules[module_name] = {
         | 
| 90 | 
             
                            "title": getattr(module, "title", module_name),
         | 
| 91 | 
             
                            "description": getattr(module, "description", "No description available."),
         | 
| @@ -160,7 +179,83 @@ with col3: | |
| 160 | 
             
                        mime="application/pdf"
         | 
| 161 | 
             
                    )
         | 
| 162 |  | 
| 163 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 164 |  | 
| 165 | 
             
            # Disable the radio buttons if the question is already answered
         | 
| 166 | 
             
            disabled_options = current_question['answered']
         | 
| @@ -203,16 +298,4 @@ if submit_button: | |
| 203 |  | 
| 204 | 
             
            # Show correct/incorrect feedback after submission
         | 
| 205 | 
             
            if current_question.get('answered', False):
         | 
| 206 | 
            -
                 | 
| 207 | 
            -
                    if option == current_question['correct_answer']:
         | 
| 208 | 
            -
                        st.markdown(f"<span style='color:green;'>{option} ✅</span>", unsafe_allow_html=True)
         | 
| 209 | 
            -
                    elif option == current_question['selected']:
         | 
| 210 | 
            -
                        st.markdown(f"<span style='color:red;'>{option} ❌</span>", unsafe_allow_html=True)
         | 
| 211 | 
            -
                    else:
         | 
| 212 | 
            -
                        st.markdown(f"{option}")
         | 
| 213 | 
            -
                
         | 
| 214 | 
            -
                # Show explanation and step-by-step solution
         | 
| 215 | 
            -
                st.write(f"**Explanation:** {current_question['explanation']}")
         | 
| 216 | 
            -
                st.write("**Step-by-Step Solution:**")
         | 
| 217 | 
            -
                for step in current_question['step_by_step_solution']:
         | 
| 218 | 
            -
                    st.write(step)
         | 
|  | |
| 2 | 
             
            import os
         | 
| 3 | 
             
            from fpdf import FPDF
         | 
| 4 | 
             
            import uuid
         | 
| 5 | 
            +
            import importlib
         | 
| 6 |  | 
| 7 | 
             
            # Initialize session state variables
         | 
| 8 | 
             
            if 'session_id' not in st.session_state:
         | 
|  | |
| 39 | 
             
                pdf.ln(10)
         | 
| 40 |  | 
| 41 | 
             
                for module, data in st.session_state.module_question_count.items():
         | 
| 42 | 
            +
                    pdf.set_text_color(0, 0, 0)  # Reset to default color
         | 
| 43 | 
            +
                    pdf.set_font("Arial", size=12, style='B')
         | 
| 44 | 
             
                    pdf.cell(200, 10, txt=f"Module: {module}", ln=True, align="L")
         | 
| 45 | 
             
                    pdf.ln(5)
         | 
|  | |
|  | |
| 46 |  | 
| 47 | 
             
                    for entry in st.session_state.questions:
         | 
| 48 | 
             
                        if entry['module'] == module:
         | 
|  | |
| 54 | 
             
                                entry['explanation'],
         | 
| 55 | 
             
                                entry['step_by_step_solution']
         | 
| 56 | 
             
                            )
         | 
| 57 | 
            +
                            
         | 
| 58 | 
            +
                            # Draw a border around the question and options with a light background
         | 
| 59 | 
            +
                            pdf.set_draw_color(0, 0, 0)  # Black border
         | 
| 60 | 
            +
                            pdf.set_fill_color(240, 240, 240)  # Light gray background for question
         | 
| 61 | 
            +
                            pdf.set_line_width(0.3)
         | 
| 62 | 
            +
                            
         | 
| 63 | 
            +
                            # Question
         | 
| 64 | 
            +
                            pdf.multi_cell(0, 10, f"Q: {question}", border=1, align='L', fill=True)
         | 
| 65 | 
            +
                            pdf.ln(5)
         | 
| 66 | 
            +
             | 
| 67 | 
            +
                            # Options with distinct background
         | 
| 68 | 
             
                            for option in options:
         | 
| 69 | 
             
                                if option == correct:
         | 
| 70 | 
             
                                    pdf.set_text_color(0, 128, 0)  # Green for correct
         | 
| 71 | 
            +
                                    pdf.set_fill_color(200, 255, 200)  # Light green background
         | 
| 72 | 
            +
                                    pdf.multi_cell(0, 10, f"{option}", border=1, align='L', fill=True)
         | 
| 73 | 
             
                                elif option == selected:
         | 
| 74 | 
             
                                    pdf.set_text_color(255, 0, 0)  # Red for incorrect
         | 
| 75 | 
            +
                                    pdf.set_fill_color(255, 200, 200)  # Light red background
         | 
| 76 | 
            +
                                    pdf.multi_cell(0, 10, f"{option}", border=1, align='L', fill=True)
         | 
| 77 | 
             
                                else:
         | 
| 78 | 
             
                                    pdf.set_text_color(0, 0, 0)  # Default color for others
         | 
| 79 | 
            +
                                    pdf.set_fill_color(240, 240, 240)  # Light gray background
         | 
| 80 | 
            +
                                    pdf.multi_cell(0, 10, f"   {option}", border=1, align='L', fill=True)
         | 
| 81 | 
            +
                            pdf.ln(5)
         | 
| 82 | 
            +
             | 
| 83 | 
            +
                            # Explanation with background
         | 
| 84 | 
            +
                            pdf.set_text_color(0, 0, 0)  # Reset to black
         | 
| 85 | 
            +
                            pdf.set_fill_color(230, 230, 250)  # Light blue background
         | 
| 86 | 
            +
                            pdf.multi_cell(0, 10, f"Explanation: {explanation}", border=1, align='L', fill=True)
         | 
| 87 | 
             
                            pdf.ln(5)
         | 
| 88 | 
            +
             | 
| 89 | 
            +
                            # Step-by-step solution with background
         | 
| 90 | 
            +
                            pdf.set_fill_color(250, 235, 215)  # Light tan background
         | 
| 91 | 
            +
                            pdf.multi_cell(0, 10, "Step-by-Step Solution:", border=1, align='L', fill=True)
         | 
| 92 | 
             
                            for step in step_by_step_solution:
         | 
| 93 | 
            +
                                pdf.multi_cell(0, 10, step, border=1, align='L', fill=True)
         | 
| 94 | 
             
                            pdf.ln(10)
         | 
| 95 |  | 
| 96 | 
             
                    pdf.ln(10)  # Add space after each module
         | 
|  | |
| 104 | 
             
                    if filename.endswith(".py") and filename != "__init__.py":
         | 
| 105 | 
             
                        module_name = filename[:-3]
         | 
| 106 | 
             
                        # Dynamically import the module only when needed
         | 
| 107 | 
            +
                        module = importlib.import_module(f"modules.{module_name}")
         | 
| 108 | 
             
                        modules[module_name] = {
         | 
| 109 | 
             
                            "title": getattr(module, "title", module_name),
         | 
| 110 | 
             
                            "description": getattr(module, "description", "No description available."),
         | 
|  | |
| 179 | 
             
                        mime="application/pdf"
         | 
| 180 | 
             
                    )
         | 
| 181 |  | 
| 182 | 
            +
            # Web Page Presentation Styling
         | 
| 183 | 
            +
            def display_question_with_styles(question_data):
         | 
| 184 | 
            +
                st.markdown(f"""
         | 
| 185 | 
            +
                <style>
         | 
| 186 | 
            +
                .question-box {{
         | 
| 187 | 
            +
                    border: 2px solid #000;
         | 
| 188 | 
            +
                    padding: 10px;
         | 
| 189 | 
            +
                    margin-bottom: 20px;
         | 
| 190 | 
            +
                    background-color: #f0f0f0;
         | 
| 191 | 
            +
                }}
         | 
| 192 | 
            +
                .option {{
         | 
| 193 | 
            +
                    border: 1px solid #000;
         | 
| 194 | 
            +
                    padding: 5px;
         | 
| 195 | 
            +
                    margin-bottom: 5px;
         | 
| 196 | 
            +
                }}
         | 
| 197 | 
            +
                .correct {{
         | 
| 198 | 
            +
                    background-color: #c8e6c9;
         | 
| 199 | 
            +
                    color: green;
         | 
| 200 | 
            +
                }}
         | 
| 201 | 
            +
                .incorrect {{
         | 
| 202 | 
            +
                    background-color: #ffcdd2;
         | 
| 203 | 
            +
                    color: red;
         | 
| 204 | 
            +
                }}
         | 
| 205 | 
            +
                .explanation-box {{
         | 
| 206 | 
            +
                    border: 2px solid #000;
         | 
| 207 | 
            +
                    padding: 10px;
         | 
| 208 | 
            +
                    background-color: #e3f2fd;
         | 
| 209 | 
            +
                    margin-top: 20px;
         | 
| 210 | 
            +
                }}
         | 
| 211 | 
            +
                .step-box {{
         | 
| 212 | 
            +
                    border: 1px solid #000;
         | 
| 213 | 
            +
                    padding: 5px;
         | 
| 214 | 
            +
                    background-color: #fff3e0;
         | 
| 215 | 
            +
                    margin-top: 10px;
         | 
| 216 | 
            +
                }}
         | 
| 217 | 
            +
                </style>
         | 
| 218 | 
            +
                """, unsafe_allow_html=True)
         | 
| 219 | 
            +
             | 
| 220 | 
            +
                st.markdown(f"""
         | 
| 221 | 
            +
                <div class="question-box">
         | 
| 222 | 
            +
                    <strong>Q:</strong> {question_data['question']}
         | 
| 223 | 
            +
                </div>
         | 
| 224 | 
            +
                """, unsafe_allow_html=True)
         | 
| 225 | 
            +
             | 
| 226 | 
            +
                for option in question_data['options']:
         | 
| 227 | 
            +
                    option_class = ''
         | 
| 228 | 
            +
                    if option == question_data['correct_answer']:
         | 
| 229 | 
            +
                        option_class = 'correct'
         | 
| 230 | 
            +
                    elif option == question_data['selected']:
         | 
| 231 | 
            +
                        option_class = 'incorrect'
         | 
| 232 | 
            +
             | 
| 233 | 
            +
                    st.markdown(f"""
         | 
| 234 | 
            +
                    <div class="option {option_class}">
         | 
| 235 | 
            +
                        {option}
         | 
| 236 | 
            +
                    </div>
         | 
| 237 | 
            +
                    """, unsafe_allow_html=True)
         | 
| 238 | 
            +
             | 
| 239 | 
            +
                if question_data.get('answered', False):
         | 
| 240 | 
            +
                    st.markdown(f"""
         | 
| 241 | 
            +
                    <div class="explanation-box">
         | 
| 242 | 
            +
                        <strong>Explanation:</strong> {question_data['explanation']}
         | 
| 243 | 
            +
                    </div>
         | 
| 244 | 
            +
                    """, unsafe_allow_html=True)
         | 
| 245 | 
            +
             | 
| 246 | 
            +
                    st.markdown(f"""
         | 
| 247 | 
            +
                    <div class="step-box">
         | 
| 248 | 
            +
                        <strong>Step-by-Step Solution:</strong>
         | 
| 249 | 
            +
                        <ul>
         | 
| 250 | 
            +
                    """, unsafe_allow_html=True)
         | 
| 251 | 
            +
             | 
| 252 | 
            +
                    for step in question_data['step_by_step_solution']:
         | 
| 253 | 
            +
                        st.markdown(f"<li>{step}</li>", unsafe_allow_html=True)
         | 
| 254 | 
            +
             | 
| 255 | 
            +
                    st.markdown("</ul></div>", unsafe_allow_html=True)
         | 
| 256 | 
            +
             | 
| 257 | 
            +
            # Display the current question with styles
         | 
| 258 | 
            +
            display_question_with_styles(current_question)
         | 
| 259 |  | 
| 260 | 
             
            # Disable the radio buttons if the question is already answered
         | 
| 261 | 
             
            disabled_options = current_question['answered']
         | 
|  | |
| 298 |  | 
| 299 | 
             
            # Show correct/incorrect feedback after submission
         | 
| 300 | 
             
            if current_question.get('answered', False):
         | 
| 301 | 
            +
                display_question_with_styles(current_question)
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
