First version quasi working
Browse files- __pycache__/backend.cpython-312.pyc +0 -0
- app.py +14 -14
- static/script.js +28 -24
- templates/host.html +3 -1
__pycache__/backend.cpython-312.pyc
CHANGED
Binary files a/__pycache__/backend.cpython-312.pyc and b/__pycache__/backend.cpython-312.pyc differ
|
|
app.py
CHANGED
@@ -45,8 +45,8 @@ def on_leave():
|
|
45 |
emit('update_participants', {"participants": participants, "count": len(participants)}, room='quiz')
|
46 |
print(f"{username} left the quiz.")
|
47 |
|
48 |
-
@socketio.on('
|
49 |
-
def
|
50 |
global selected_questions
|
51 |
exam_name = data['exam_name']
|
52 |
start_question = data['start_question'] - 1 # Adjust for 0-based indexing
|
@@ -54,23 +54,23 @@ def select_exam(data):
|
|
54 |
if selected_questions:
|
55 |
num_questions = len(selected_questions)
|
56 |
current_question['index'] = start_question
|
57 |
-
emit('
|
58 |
else:
|
59 |
-
emit('
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
@socketio.on('restart_quiz')
|
62 |
def restart_quiz():
|
63 |
reset_quiz()
|
|
|
64 |
start_quiz()
|
65 |
|
66 |
-
def start_quiz():
|
67 |
-
current_question['started'] = True
|
68 |
-
index = current_question['index']
|
69 |
-
if index < len(selected_questions):
|
70 |
-
question = selected_questions[index]
|
71 |
-
emit('new_question', question, room='quiz')
|
72 |
-
emit('enable_end_quiz', room='quiz')
|
73 |
-
|
74 |
@socketio.on('submit_answer')
|
75 |
def receive_answer(data):
|
76 |
username = participants[request.sid]["username"]
|
@@ -115,10 +115,10 @@ def end_quiz():
|
|
115 |
emit('display_final_results', final_results, room='quiz')
|
116 |
|
117 |
def generate_chart(answers, options):
|
118 |
-
letters = [
|
119 |
counts = [list(answers.values()).count(option) for option in options]
|
120 |
plt.figure(figsize=(6, 4))
|
121 |
-
plt.bar(letters
|
122 |
plt.xlabel('Options')
|
123 |
plt.ylabel('Number of Votes')
|
124 |
plt.title('Results')
|
|
|
45 |
emit('update_participants', {"participants": participants, "count": len(participants)}, room='quiz')
|
46 |
print(f"{username} left the quiz.")
|
47 |
|
48 |
+
@socketio.on('load_quiz')
|
49 |
+
def load_quiz(data):
|
50 |
global selected_questions
|
51 |
exam_name = data['exam_name']
|
52 |
start_question = data['start_question'] - 1 # Adjust for 0-based indexing
|
|
|
54 |
if selected_questions:
|
55 |
num_questions = len(selected_questions)
|
56 |
current_question['index'] = start_question
|
57 |
+
emit('quiz_loaded', {"success": True, "num_questions": num_questions, "start_question": start_question + 1}, room=request.sid)
|
58 |
else:
|
59 |
+
emit('quiz_loaded', {"success": False}, room=request.sid)
|
60 |
+
|
61 |
+
@socketio.on('start_quiz')
|
62 |
+
def start_quiz():
|
63 |
+
if participants and selected_questions:
|
64 |
+
current_question['started'] = True
|
65 |
+
emit('new_question', selected_questions[current_question['index']], room='quiz')
|
66 |
+
emit('enable_end_quiz', room='quiz')
|
67 |
|
68 |
@socketio.on('restart_quiz')
|
69 |
def restart_quiz():
|
70 |
reset_quiz()
|
71 |
+
emit('quiz_reset', room='quiz')
|
72 |
start_quiz()
|
73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
@socketio.on('submit_answer')
|
75 |
def receive_answer(data):
|
76 |
username = participants[request.sid]["username"]
|
|
|
115 |
emit('display_final_results', final_results, room='quiz')
|
116 |
|
117 |
def generate_chart(answers, options):
|
118 |
+
letters = [chr(65 + i) for i in range(len(options))]
|
119 |
counts = [list(answers.values()).count(option) for option in options]
|
120 |
plt.figure(figsize=(6, 4))
|
121 |
+
plt.bar(letters, counts)
|
122 |
plt.xlabel('Options')
|
123 |
plt.ylabel('Number of Votes')
|
124 |
plt.title('Results')
|
static/script.js
CHANGED
@@ -26,39 +26,24 @@ function submitForm(event) {
|
|
26 |
function selectExam() {
|
27 |
const examName = document.getElementById('exam-selector').value;
|
28 |
const startQuestion = document.getElementById('start-question-number').value;
|
29 |
-
socket.emit('select_exam', { exam_name: examName, start_question: parseInt(startQuestion) });
|
30 |
document.getElementById('question-start-display').textContent = `Starting from question ${startQuestion}.`;
|
31 |
}
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
function updateSliderValue(value) {
|
34 |
document.getElementById('start-question').value = value;
|
35 |
document.getElementById('start-question-number').value = value;
|
36 |
document.getElementById('question-start-display').textContent = `Starting from question ${value}.`;
|
37 |
}
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
} else {
|
43 |
-
alert(`Failed to load exam "${data.exam_name}".`);
|
44 |
-
}
|
45 |
-
});
|
46 |
-
|
47 |
-
socket.on('update_participants', (data) => {
|
48 |
-
document.getElementById('participant-count').textContent = data.count;
|
49 |
-
});
|
50 |
-
|
51 |
-
socket.on('new_question', (data) => {
|
52 |
-
document.getElementById('waiting-message').style.display = 'none';
|
53 |
-
document.getElementById('question-text').innerText = data.question;
|
54 |
-
const letters = ['a', 'b', 'c', 'd'];
|
55 |
-
const options = data.options.map((opt, index) =>
|
56 |
-
`<input type="radio" id="${letters[index]}" name="answer" value="${opt}">
|
57 |
-
<label for="${letters[index]}">${letters[index]}) ${opt}</label><br>`
|
58 |
-
).join('');
|
59 |
-
document.getElementById('options').innerHTML = options;
|
60 |
-
document.getElementById('end-quiz').disabled = false;
|
61 |
-
});
|
62 |
|
63 |
function checkAnswers() {
|
64 |
socket.emit('check_answers');
|
@@ -76,6 +61,25 @@ function restartQuiz() {
|
|
76 |
socket.emit('restart_quiz');
|
77 |
}
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
socket.on('display_results', (data) => {
|
80 |
const img = `<img src="data:image/png;base64,${data.chart}" alt="Results Chart" />`;
|
81 |
const resultText = `<p>Correct Answer: ${data.results.correct_answer}</p>`;
|
|
|
26 |
function selectExam() {
|
27 |
const examName = document.getElementById('exam-selector').value;
|
28 |
const startQuestion = document.getElementById('start-question-number').value;
|
|
|
29 |
document.getElementById('question-start-display').textContent = `Starting from question ${startQuestion}.`;
|
30 |
}
|
31 |
|
32 |
+
function loadQuiz() {
|
33 |
+
const examName = document.getElementById('exam-selector').value;
|
34 |
+
const startQuestion = document.getElementById('start-question-number').value;
|
35 |
+
socket.emit('load_quiz', { exam_name: examName, start_question: parseInt(startQuestion) });
|
36 |
+
}
|
37 |
+
|
38 |
function updateSliderValue(value) {
|
39 |
document.getElementById('start-question').value = value;
|
40 |
document.getElementById('start-question-number').value = value;
|
41 |
document.getElementById('question-start-display').textContent = `Starting from question ${value}.`;
|
42 |
}
|
43 |
|
44 |
+
function startQuiz() {
|
45 |
+
socket.emit('start_quiz');
|
46 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
function checkAnswers() {
|
49 |
socket.emit('check_answers');
|
|
|
61 |
socket.emit('restart_quiz');
|
62 |
}
|
63 |
|
64 |
+
socket.on('quiz_loaded', (data) => {
|
65 |
+
if (data.success) {
|
66 |
+
alert(`Quiz loaded with ${data.num_questions} questions, starting from question ${data.start_question}.`);
|
67 |
+
} else {
|
68 |
+
alert(`Failed to load quiz.`);
|
69 |
+
}
|
70 |
+
});
|
71 |
+
|
72 |
+
socket.on('new_question', (data) => {
|
73 |
+
document.getElementById('waiting-message').style.display = 'none';
|
74 |
+
document.getElementById('question-text').innerText = data.question;
|
75 |
+
const letters = ['a', 'b', 'c', 'd'];
|
76 |
+
const options = data.options.map((opt, index) =>
|
77 |
+
`<input type="radio" id="${letters[index]}" name="answer" value="${opt}">
|
78 |
+
<label for="${letters[index]}">${letters[index]}) ${opt}</label><br>`
|
79 |
+
).join('');
|
80 |
+
document.getElementById('options').innerHTML = options;
|
81 |
+
});
|
82 |
+
|
83 |
socket.on('display_results', (data) => {
|
84 |
const img = `<img src="data:image/png;base64,${data.chart}" alt="Results Chart" />`;
|
85 |
const resultText = `<p>Correct Answer: ${data.results.correct_answer}</p>`;
|
templates/host.html
CHANGED
@@ -21,7 +21,9 @@
|
|
21 |
<input type="range" id="start-question" min="1" max="10" value="1" class="form-range mt-2 mb-2" oninput="updateSliderValue(this.value)">
|
22 |
<input type="number" id="start-question-number" min="1" max="10" value="1" class="form-control" oninput="updateSliderValue(this.value)">
|
23 |
<p id="question-start-display" class="mt-2">Starting from question 1.</p>
|
24 |
-
<button onclick="
|
|
|
|
|
25 |
<button onclick="checkAnswers()" class="btn btn-primary mt-2">Check Answers</button><br><br>
|
26 |
<button onclick="nextQuestion()" class="btn btn-secondary mt-2">Next Question</button><br><br>
|
27 |
<button onclick="endQuiz()" id="end-quiz" class="btn btn-danger mt-2" disabled>End Quiz</button>
|
|
|
21 |
<input type="range" id="start-question" min="1" max="10" value="1" class="form-range mt-2 mb-2" oninput="updateSliderValue(this.value)">
|
22 |
<input type="number" id="start-question-number" min="1" max="10" value="1" class="form-control" oninput="updateSliderValue(this.value)">
|
23 |
<p id="question-start-display" class="mt-2">Starting from question 1.</p>
|
24 |
+
<button onclick="loadQuiz()" class="btn btn-info mt-3">Load Quiz</button><br><br>
|
25 |
+
<button onclick="startQuiz()" class="btn btn-success mt-3">Start Quiz</button><br><br>
|
26 |
+
<button onclick="restartQuiz()" class="btn btn-warning mt-3">Restart</button><br><br>
|
27 |
<button onclick="checkAnswers()" class="btn btn-primary mt-2">Check Answers</button><br><br>
|
28 |
<button onclick="nextQuestion()" class="btn btn-secondary mt-2">Next Question</button><br><br>
|
29 |
<button onclick="endQuiz()" id="end-quiz" class="btn btn-danger mt-2" disabled>End Quiz</button>
|