First exteded exam
Browse files- __pycache__/backend.cpython-312.pyc +0 -0
- app.py +26 -22
- backend.py +23 -0
- questions/CLF-C02-v1.json +0 -0
- static/script.js +24 -6
- templates/host.html +6 -0
__pycache__/backend.cpython-312.pyc
ADDED
Binary file (1.37 kB). View file
|
|
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
from flask import Flask, render_template, request
|
2 |
from flask_socketio import SocketIO, emit, join_room, leave_room
|
|
|
3 |
import matplotlib.pyplot as plt
|
4 |
import base64
|
5 |
from io import BytesIO
|
@@ -9,11 +10,8 @@ app = Flask(__name__)
|
|
9 |
app.config['SECRET_KEY'] = 'your_secret_key'
|
10 |
socketio = SocketIO(app)
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
{"question": "What is the largest planet?", "options": ["Earth", "Mars", "Jupiter", "Saturn"], "correct": "Jupiter"}
|
15 |
-
]
|
16 |
-
initial_questions = questions.copy() # Keep a copy of the original questions to reset later
|
17 |
current_question = {"index": 0, "answers": {}, "started": False}
|
18 |
participants = {}
|
19 |
|
@@ -27,12 +25,12 @@ def client():
|
|
27 |
|
28 |
@app.route('/host')
|
29 |
def host():
|
30 |
-
return render_template('host.html')
|
31 |
|
32 |
@socketio.on('join')
|
33 |
def on_join(data):
|
34 |
username = data['username']
|
35 |
-
user_id_number = random.randint(1000, 9999)
|
36 |
participants[request.sid] = {"user_id_number": user_id_number, "username": username, "score": 0}
|
37 |
join_room('quiz')
|
38 |
emit('update_participants', {"participants": participants, "count": len(participants)}, room='quiz')
|
@@ -47,19 +45,28 @@ def on_leave():
|
|
47 |
emit('update_participants', {"participants": participants, "count": len(participants)}, room='quiz')
|
48 |
print(f"{username} left the quiz.")
|
49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
@socketio.on('restart_quiz')
|
51 |
def restart_quiz():
|
52 |
-
reset_quiz()
|
53 |
emit('quiz_reset', room='quiz')
|
54 |
-
start_quiz()
|
55 |
|
56 |
def start_quiz():
|
57 |
current_question['started'] = True
|
58 |
index = current_question['index']
|
59 |
-
if index < len(
|
60 |
-
question =
|
61 |
emit('new_question', question, room='quiz')
|
62 |
-
emit('enable_end_quiz', room='quiz')
|
63 |
|
64 |
@socketio.on('submit_answer')
|
65 |
def receive_answer(data):
|
@@ -72,8 +79,8 @@ def receive_answer(data):
|
|
72 |
@socketio.on('check_answers')
|
73 |
def check_answers():
|
74 |
index = current_question['index']
|
75 |
-
if index < len(
|
76 |
-
question =
|
77 |
correct_answer = question['correct']
|
78 |
results = {
|
79 |
"question": question["question"],
|
@@ -81,11 +88,9 @@ def check_answers():
|
|
81 |
"correct_answer": correct_answer
|
82 |
}
|
83 |
|
84 |
-
# Generate the chart and encode it as base64
|
85 |
chart_base64 = generate_chart(current_question["answers"], question["options"])
|
86 |
emit('display_results', {"results": results, "chart": chart_base64}, room='quiz')
|
87 |
|
88 |
-
# Update scores based on user_id_number
|
89 |
for sid, participant in participants.items():
|
90 |
if current_question['answers'].get(participant["username"]) == correct_answer:
|
91 |
participants[sid]["score"] += 1
|
@@ -94,9 +99,9 @@ def check_answers():
|
|
94 |
def next_question():
|
95 |
current_question['index'] += 1
|
96 |
current_question['answers'] = {}
|
97 |
-
if current_question['index'] < len(
|
98 |
-
question =
|
99 |
-
emit('clear_results', room='quiz')
|
100 |
emit('new_question', question, room='quiz')
|
101 |
else:
|
102 |
final_results = calculate_final_results()
|
@@ -127,11 +132,10 @@ def calculate_final_results():
|
|
127 |
return [{"username": p["username"], "score": p["score"]} for p in sorted_scores]
|
128 |
|
129 |
def reset_quiz():
|
130 |
-
global
|
131 |
-
questions = initial_questions.copy()
|
132 |
current_question = {"index": 0, "answers": {}, "started": False}
|
133 |
for participant in participants.values():
|
134 |
participant["score"] = 0
|
135 |
|
136 |
if __name__ == '__main__':
|
137 |
-
socketio.run(app, debug=True)
|
|
|
1 |
from flask import Flask, render_template, request
|
2 |
from flask_socketio import SocketIO, emit, join_room, leave_room
|
3 |
+
import backend # Import backend functions
|
4 |
import matplotlib.pyplot as plt
|
5 |
import base64
|
6 |
from io import BytesIO
|
|
|
10 |
app.config['SECRET_KEY'] = 'your_secret_key'
|
11 |
socketio = SocketIO(app)
|
12 |
|
13 |
+
exams = backend.load_question_sets() # Load available exams
|
14 |
+
selected_questions = [] # Global variable to store the selected questions
|
|
|
|
|
|
|
15 |
current_question = {"index": 0, "answers": {}, "started": False}
|
16 |
participants = {}
|
17 |
|
|
|
25 |
|
26 |
@app.route('/host')
|
27 |
def host():
|
28 |
+
return render_template('host.html', exams=exams)
|
29 |
|
30 |
@socketio.on('join')
|
31 |
def on_join(data):
|
32 |
username = data['username']
|
33 |
+
user_id_number = random.randint(1000, 9999)
|
34 |
participants[request.sid] = {"user_id_number": user_id_number, "username": username, "score": 0}
|
35 |
join_room('quiz')
|
36 |
emit('update_participants', {"participants": participants, "count": len(participants)}, room='quiz')
|
|
|
45 |
emit('update_participants', {"participants": participants, "count": len(participants)}, room='quiz')
|
46 |
print(f"{username} left the quiz.")
|
47 |
|
48 |
+
@socketio.on('select_exam')
|
49 |
+
def select_exam(exam_name):
|
50 |
+
global selected_questions
|
51 |
+
selected_questions = backend.select_exam(exam_name)
|
52 |
+
if selected_questions:
|
53 |
+
emit('exam_loaded', {"success": True, "exam_name": exam_name}, room=request.sid)
|
54 |
+
else:
|
55 |
+
emit('exam_loaded', {"success": False, "exam_name": exam_name}, room=request.sid)
|
56 |
+
|
57 |
@socketio.on('restart_quiz')
|
58 |
def restart_quiz():
|
59 |
+
reset_quiz()
|
60 |
emit('quiz_reset', room='quiz')
|
61 |
+
start_quiz()
|
62 |
|
63 |
def start_quiz():
|
64 |
current_question['started'] = True
|
65 |
index = current_question['index']
|
66 |
+
if index < len(selected_questions):
|
67 |
+
question = selected_questions[index]
|
68 |
emit('new_question', question, room='quiz')
|
69 |
+
emit('enable_end_quiz', room='quiz')
|
70 |
|
71 |
@socketio.on('submit_answer')
|
72 |
def receive_answer(data):
|
|
|
79 |
@socketio.on('check_answers')
|
80 |
def check_answers():
|
81 |
index = current_question['index']
|
82 |
+
if index < len(selected_questions):
|
83 |
+
question = selected_questions[index]
|
84 |
correct_answer = question['correct']
|
85 |
results = {
|
86 |
"question": question["question"],
|
|
|
88 |
"correct_answer": correct_answer
|
89 |
}
|
90 |
|
|
|
91 |
chart_base64 = generate_chart(current_question["answers"], question["options"])
|
92 |
emit('display_results', {"results": results, "chart": chart_base64}, room='quiz')
|
93 |
|
|
|
94 |
for sid, participant in participants.items():
|
95 |
if current_question['answers'].get(participant["username"]) == correct_answer:
|
96 |
participants[sid]["score"] += 1
|
|
|
99 |
def next_question():
|
100 |
current_question['index'] += 1
|
101 |
current_question['answers'] = {}
|
102 |
+
if current_question['index'] < len(selected_questions):
|
103 |
+
question = selected_questions[current_question['index']]
|
104 |
+
emit('clear_results', room='quiz')
|
105 |
emit('new_question', question, room='quiz')
|
106 |
else:
|
107 |
final_results = calculate_final_results()
|
|
|
132 |
return [{"username": p["username"], "score": p["score"]} for p in sorted_scores]
|
133 |
|
134 |
def reset_quiz():
|
135 |
+
global selected_questions, current_question
|
|
|
136 |
current_question = {"index": 0, "answers": {}, "started": False}
|
137 |
for participant in participants.values():
|
138 |
participant["score"] = 0
|
139 |
|
140 |
if __name__ == '__main__':
|
141 |
+
socketio.run(app, debug=True)
|
backend.py
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
|
4 |
+
# Function to load question sets from the "questions" directory
|
5 |
+
def load_question_sets(directory='questions'):
|
6 |
+
question_sets = []
|
7 |
+
for root, dirs, files in os.walk(directory):
|
8 |
+
for file in files:
|
9 |
+
if file.endswith(".json"):
|
10 |
+
question_sets.append(file[:-5]) # Remove the ".json" extension
|
11 |
+
return question_sets
|
12 |
+
|
13 |
+
# Function to select and load the specified exam
|
14 |
+
def select_exam(exam_name):
|
15 |
+
file_path = os.path.join('questions', f'{exam_name}.json')
|
16 |
+
try:
|
17 |
+
with open(file_path, 'r') as f:
|
18 |
+
questions = json.load(f)
|
19 |
+
print(f"Loaded {len(questions)} questions from {exam_name}")
|
20 |
+
return questions
|
21 |
+
except FileNotFoundError:
|
22 |
+
print(f"File {file_path} not found.")
|
23 |
+
return [] # Return an empty list if the file is not found
|
questions/CLF-C02-v1.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
static/script.js
CHANGED
@@ -12,6 +12,30 @@ function joinQuiz() {
|
|
12 |
document.getElementById('join-title').style.display = 'none';
|
13 |
}
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
socket.on('update_participants', (data) => {
|
16 |
document.getElementById('participant-count').textContent = data.count;
|
17 |
});
|
@@ -27,12 +51,6 @@ socket.on('new_question', (data) => {
|
|
27 |
document.getElementById('options').innerHTML = options;
|
28 |
});
|
29 |
|
30 |
-
function submitForm(event) {
|
31 |
-
event.preventDefault();
|
32 |
-
const answer = document.querySelector('input[name="answer"]:checked').value;
|
33 |
-
socket.emit('submit_answer', { answer });
|
34 |
-
}
|
35 |
-
|
36 |
function checkAnswers() {
|
37 |
socket.emit('check_answers');
|
38 |
}
|
|
|
12 |
document.getElementById('join-title').style.display = 'none';
|
13 |
}
|
14 |
|
15 |
+
function submitForm(event) {
|
16 |
+
event.preventDefault();
|
17 |
+
const selectedOption = document.querySelector('input[name="answer"]:checked');
|
18 |
+
if (selectedOption) {
|
19 |
+
const answer = selectedOption.value;
|
20 |
+
socket.emit('submit_answer', { answer });
|
21 |
+
} else {
|
22 |
+
alert("Please select an option before submitting.");
|
23 |
+
}
|
24 |
+
}
|
25 |
+
|
26 |
+
function selectExam() {
|
27 |
+
const examName = document.getElementById('exam-selector').value;
|
28 |
+
socket.emit('select_exam', examName);
|
29 |
+
}
|
30 |
+
|
31 |
+
socket.on('exam_loaded', (data) => {
|
32 |
+
if (data.success) {
|
33 |
+
alert(`Exam "${data.exam_name}" loaded successfully!`);
|
34 |
+
} else {
|
35 |
+
alert(`Failed to load exam "${data.exam_name}".`);
|
36 |
+
}
|
37 |
+
});
|
38 |
+
|
39 |
socket.on('update_participants', (data) => {
|
40 |
document.getElementById('participant-count').textContent = data.count;
|
41 |
});
|
|
|
51 |
document.getElementById('options').innerHTML = options;
|
52 |
});
|
53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
function checkAnswers() {
|
55 |
socket.emit('check_answers');
|
56 |
}
|
templates/host.html
CHANGED
@@ -11,6 +11,12 @@
|
|
11 |
<div class="container">
|
12 |
<h2>Quiz Host</h2>
|
13 |
<p>Participants connected: <span id="participant-count">0</span></p>
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
<button onclick="restartQuiz()" class="btn btn-success mt-3">Start New Quiz</button>
|
15 |
<button onclick="checkAnswers()" class="btn btn-primary mt-2">Check Answers</button>
|
16 |
<button onclick="nextQuestion()" class="btn btn-secondary mt-2">Next Question</button>
|
|
|
11 |
<div class="container">
|
12 |
<h2>Quiz Host</h2>
|
13 |
<p>Participants connected: <span id="participant-count">0</span></p>
|
14 |
+
<select id="exam-selector" class="form-control" onchange="selectExam()">
|
15 |
+
<option value="" disabled selected>Select an exam</option>
|
16 |
+
{% for exam in exams %}
|
17 |
+
<option value="{{ exam }}">{{ exam }}</option>
|
18 |
+
{% endfor %}
|
19 |
+
</select>
|
20 |
<button onclick="restartQuiz()" class="btn btn-success mt-3">Start New Quiz</button>
|
21 |
<button onclick="checkAnswers()" class="btn btn-primary mt-2">Check Answers</button>
|
22 |
<button onclick="nextQuestion()" class="btn btn-secondary mt-2">Next Question</button>
|