Sina Media Lab commited on
Commit
795b9ba
Β·
1 Parent(s): bad6a07

Add application file 5

Browse files
Files changed (1) hide show
  1. app.py +96 -69
app.py CHANGED
@@ -1,83 +1,110 @@
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()
 
 
 
 
 
 
 
 
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.")