Sina Media Lab
commited on
Commit
Β·
bad6a07
1
Parent(s):
32f2e18
Add application file 5
Browse files- app.py +73 -61
- requirements.txt +2 -1
app.py
CHANGED
@@ -1,71 +1,83 @@
|
|
1 |
import streamlit as st
|
2 |
-
import
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
"2's Complement": "twos_complement",
|
12 |
-
"Negative Numbers": "negative_binary",
|
13 |
-
"Subtraction": "subtraction_bases",
|
14 |
-
}
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
st.session_state.question_count = 0
|
21 |
-
if 'current_module' not in st.session_state:
|
22 |
-
st.session_state.current_module = None
|
23 |
|
24 |
-
#
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
st.write(description)
|
39 |
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
47 |
|
48 |
-
|
49 |
-
if st.button("
|
50 |
-
st.session_state.
|
51 |
-
|
52 |
-
st.session_state.correct_count += 1
|
53 |
-
st.success("Correct!", icon="β
")
|
54 |
-
else:
|
55 |
-
st.error("Incorrect.", icon="β")
|
56 |
-
st.write(f"**Explanation:** {explanation}")
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
st.
|
62 |
-
if col2.button("Next Question"):
|
63 |
-
st.experimental_rerun() # To simulate moving forward
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from fpdf import FPDF
|
3 |
|
4 |
+
# Initialize session state to keep track of questions
|
5 |
+
if 'questions' not in st.session_state:
|
6 |
+
st.session_state.questions = []
|
7 |
+
if 'current_question' not in st.session_state:
|
8 |
+
st.session_state.current_question = 0
|
9 |
+
if 'pdf_report' not in st.session_state:
|
10 |
+
st.session_state.pdf_report = None
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
# Emojis for correct/incorrect and PDF
|
13 |
+
correct_emoji = "β
"
|
14 |
+
incorrect_emoji = "β"
|
15 |
+
pdf_emoji = "π"
|
|
|
|
|
|
|
16 |
|
17 |
+
# Example questions
|
18 |
+
questions = [
|
19 |
+
{
|
20 |
+
"question": "What is the binary equivalent of the decimal number 13?",
|
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 |
+
# Function to display a question
|
33 |
+
def display_question():
|
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 |
+
if st.button("Submit Answer"):
|
39 |
+
correct_answer = current_q['answer']
|
40 |
+
status = "Correct" if user_answer == correct_answer else "Incorrect"
|
41 |
+
emoji = correct_emoji if status == "Correct" else incorrect_emoji
|
|
|
42 |
|
43 |
+
st.session_state.questions.append({
|
44 |
+
'question': current_q['question'],
|
45 |
+
'user_answer': user_answer,
|
46 |
+
'correct_answer': correct_answer,
|
47 |
+
'status': status
|
48 |
+
})
|
49 |
|
50 |
+
st.markdown(f"**You answered:** {user_answer} {emoji}")
|
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 |
+
if len(st.session_state.questions) > 0 and st.session_state.current_question < len(questions) - 1:
|
57 |
+
if st.button("Next Question"):
|
58 |
+
st.session_state.current_question += 1
|
59 |
+
st.experimental_rerun()
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
+
if len(st.session_state.questions) > 0:
|
62 |
+
if st.button(f"{pdf_emoji} Download PDF Report"):
|
63 |
+
generate_pdf_report(st.session_state.questions)
|
64 |
+
st.download_button(label=f"{pdf_emoji} Download PDF", data=st.session_state.pdf_report, file_name="quiz_report.pdf")
|
|
|
|
|
65 |
|
66 |
+
# Function to generate a PDF report
|
67 |
+
def generate_pdf_report(questions):
|
68 |
+
pdf = FPDF()
|
69 |
+
pdf.add_page()
|
70 |
+
pdf.set_font("Arial", size=12)
|
71 |
+
|
72 |
+
for i, q in enumerate(questions, 1):
|
73 |
+
pdf.cell(200, 10, txt=f"Question {i}: {q['question']}", ln=True)
|
74 |
+
pdf.cell(200, 10, txt=f"Your Answer: {q['user_answer']}", ln=True)
|
75 |
+
pdf.cell(200, 10, txt=f"Correct Answer: {q['correct_answer']}", ln=True)
|
76 |
+
pdf.cell(200, 10, txt=f"Status: {q['status']}", ln=True)
|
77 |
+
pdf.cell(200, 10, txt="---------------------------------", ln=True)
|
78 |
+
|
79 |
+
st.session_state.pdf_report = pdf.output(dest='S').encode('latin1')
|
80 |
+
|
81 |
+
# Main app logic
|
82 |
+
st.title("Multiple Choice Quiz")
|
83 |
+
display_question()
|
requirements.txt
CHANGED
@@ -1,2 +1,3 @@
|
|
1 |
streamlit
|
2 |
-
huggingface_hub
|
|
|
|
1 |
streamlit
|
2 |
+
huggingface_hub
|
3 |
+
fpdf
|