Sina Media Lab
commited on
Commit
Β·
795b9ba
1
Parent(s):
bad6a07
Add application file 5
Browse files
app.py
CHANGED
@@ -1,83 +1,110 @@
|
|
1 |
import streamlit as st
|
|
|
2 |
from fpdf import FPDF
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
"options": ["1100", "1101", "1011", "1111", "1001"],
|
22 |
-
"answer": "1101"
|
23 |
-
},
|
24 |
-
{
|
25 |
-
"question": "Which of the following is a valid hexadecimal number?",
|
26 |
-
"options": ["1G4", "FACE", "XYZ", "1.5", "0x23G5"],
|
27 |
-
"answer": "FACE"
|
28 |
-
}
|
29 |
-
# Add more questions here
|
30 |
-
]
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
current_q = questions[st.session_state.current_question]
|
35 |
-
st.write(f"**Question {st.session_state.current_question + 1}:** {current_q['question']}")
|
36 |
-
user_answer = st.radio("Choose an answer:", current_q['options'])
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
'user_answer': user_answer,
|
46 |
-
'correct_answer': correct_answer,
|
47 |
-
'status': status
|
48 |
-
})
|
49 |
|
50 |
-
|
51 |
-
if status == "Correct":
|
52 |
-
st.success(f"Correct! {correct_emoji}")
|
53 |
-
else:
|
54 |
-
st.error(f"Incorrect. The correct answer is {correct_answer} {incorrect_emoji}")
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
pdf.cell(200, 10, txt=f"Status: {q['status']}", ln=True)
|
77 |
-
pdf.cell(200, 10, txt="---------------------------------", ln=True)
|
78 |
|
79 |
-
|
|
|
|
|
|
|
80 |
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import importlib
|
3 |
from fpdf import FPDF
|
4 |
|
5 |
+
# List of available modules with shorter names and icons
|
6 |
+
module_names = {
|
7 |
+
"Bases": "presentation_bases",
|
8 |
+
"Validity": "valid_invalid_numbers",
|
9 |
+
"Conversion": "conversion_bases",
|
10 |
+
"Grouping": "grouping_techniques",
|
11 |
+
"Addition": "addition_bases",
|
12 |
+
"2's Complement": "twos_complement",
|
13 |
+
"Negative Numbers": "negative_binary",
|
14 |
+
"Subtraction": "subtraction_bases",
|
15 |
+
}
|
16 |
|
17 |
+
# Initialize session state for tracking answers, progress, and questions queue
|
18 |
+
if 'correct_count' not in st.session_state:
|
19 |
+
st.session_state.correct_count = 0
|
20 |
+
if 'question_count' not in st.session_state:
|
21 |
+
st.session_state.question_count = 0
|
22 |
+
if 'current_module' not in st.session_state:
|
23 |
+
st.session_state.current_module = None
|
24 |
+
if 'question_queue' not in st.session_state:
|
25 |
+
st.session_state.question_queue = []
|
26 |
+
if 'submitted' not in st.session_state:
|
27 |
+
st.session_state.submitted = False
|
28 |
|
29 |
+
def generate_pdf_report():
|
30 |
+
pdf = FPDF()
|
31 |
+
pdf.add_page()
|
32 |
+
pdf.set_font("Arial", size=12)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
+
pdf.cell(200, 10, txt="Quiz Report", ln=True, align="C")
|
35 |
+
pdf.ln(10)
|
|
|
|
|
|
|
36 |
|
37 |
+
for idx, entry in enumerate(st.session_state.question_queue):
|
38 |
+
question, selected, correct, explanation = entry
|
39 |
+
pdf.multi_cell(0, 10, f"Q{idx+1}: {question}")
|
40 |
+
pdf.multi_cell(0, 10, f"Selected Answer: {selected}")
|
41 |
+
pdf.multi_cell(0, 10, f"Correct Answer: {correct}")
|
42 |
+
pdf.multi_cell(0, 10, f"Explanation: {explanation}")
|
43 |
+
pdf.ln(10)
|
|
|
|
|
|
|
|
|
44 |
|
45 |
+
return pdf.output(dest='S').encode('latin1')
|
|
|
|
|
|
|
|
|
46 |
|
47 |
+
# Streamlit interface
|
48 |
+
st.sidebar.title("Quiz Modules")
|
49 |
+
module_name = st.sidebar.radio("Choose a module:", list(module_names.keys()), index=0)
|
50 |
+
|
51 |
+
if module_name:
|
52 |
+
module_file = module_names[module_name]
|
53 |
+
try:
|
54 |
+
module = importlib.import_module(f'modules.{module_file}')
|
55 |
+
generate_question = module.generate_question
|
56 |
+
title = getattr(module, 'title', f"{module_name} Module")
|
57 |
+
description = getattr(module, 'description', "Description is not available")
|
58 |
+
|
59 |
+
# Show module title and description
|
60 |
+
st.title(title)
|
61 |
+
st.write(description)
|
62 |
+
|
63 |
+
# Add spacing to ensure question visibility on Hugging Face
|
64 |
+
st.markdown("<br><br>", unsafe_allow_html=True)
|
65 |
|
66 |
+
if st.session_state.submitted:
|
67 |
+
st.session_state.submitted = False
|
68 |
+
st.session_state.question_count += 1
|
69 |
+
st.session_state.question_queue.append((
|
70 |
+
st.session_state.current_question,
|
71 |
+
st.session_state.selected_answer,
|
72 |
+
st.session_state.correct_answer,
|
73 |
+
st.session_state.explanation
|
74 |
+
))
|
75 |
|
76 |
+
if st.session_state.selected_answer == st.session_state.correct_answer:
|
77 |
+
st.session_state.correct_count += 1
|
78 |
+
st.success("β
Correct!")
|
79 |
+
else:
|
80 |
+
st.error("β Incorrect.")
|
81 |
+
st.write(f"**Explanation:** {st.session_state.explanation}")
|
82 |
+
|
83 |
+
if st.session_state.question_count > 0:
|
84 |
+
correct_percentage = (st.session_state.correct_count / st.session_state.question_count) * 100
|
85 |
+
st.write(f"**Correct Answers:** {st.session_state.correct_count}/{st.session_state.question_count} ({correct_percentage:.2f}%)")
|
86 |
+
|
87 |
+
st.session_state.current_question, options, st.session_state.correct_answer, st.session_state.explanation = generate_question()
|
88 |
+
st.write(f"**Question {st.session_state.question_count + 1}:** {st.session_state.current_question}")
|
89 |
+
st.session_state.selected_answer = st.radio("Choose an answer:", options)
|
90 |
|
91 |
+
col1, col2, col3 = st.columns(3)
|
92 |
+
if col1.button("Submit"):
|
93 |
+
st.session_state.submitted = True
|
94 |
+
st.experimental_rerun()
|
|
|
|
|
95 |
|
96 |
+
if st.session_state.question_count > 0:
|
97 |
+
with col2:
|
98 |
+
if st.button("Next Question"):
|
99 |
+
st.experimental_rerun()
|
100 |
|
101 |
+
with col3:
|
102 |
+
pdf = generate_pdf_report()
|
103 |
+
st.download_button(
|
104 |
+
label="Download PDF Report π",
|
105 |
+
data=pdf,
|
106 |
+
file_name="quiz_report.pdf",
|
107 |
+
mime="application/pdf"
|
108 |
+
)
|
109 |
+
except ModuleNotFoundError:
|
110 |
+
st.error(f"The module '{module_name}' was not found. Please select another module.")
|