Fausto Busuito
commited on
Commit
·
372cd48
1
Parent(s):
d353aa8
Application changes
Browse files- .gitignore +1 -0
- app.py +0 -160
- backend.py +0 -23
.gitignore
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
|
| 2 |
/.venv
|
|
|
|
|
|
| 1 |
|
| 2 |
/.venv
|
| 3 |
+
/.DS_Store
|
app.py
DELETED
|
@@ -1,160 +0,0 @@
|
|
| 1 |
-
import eventlet
|
| 2 |
-
# Force eventlet to be used as the async mode for Flask-SocketIO
|
| 3 |
-
eventlet.monkey_patch()
|
| 4 |
-
from flask import Flask, render_template, request
|
| 5 |
-
from flask_socketio import SocketIO, emit, join_room, leave_room
|
| 6 |
-
import backend # Import backend functions
|
| 7 |
-
import matplotlib.pyplot as plt
|
| 8 |
-
import base64
|
| 9 |
-
from io import BytesIO
|
| 10 |
-
import random
|
| 11 |
-
import logging
|
| 12 |
-
|
| 13 |
-
# Configure the logging level
|
| 14 |
-
logging.getLogger('eventlet.wsgi.server').setLevel(logging.ERROR)
|
| 15 |
-
app = Flask(__name__)
|
| 16 |
-
app.config['SECRET_KEY'] = 'your_secret_key'
|
| 17 |
-
socketio = SocketIO(app, async_mode='eventlet')
|
| 18 |
-
|
| 19 |
-
exams = backend.load_question_sets() # Load available exams
|
| 20 |
-
selected_questions = [] # Global variable to store the selected questions
|
| 21 |
-
current_question = {"index": 0, "answers": {}, "started": False}
|
| 22 |
-
participants = {}
|
| 23 |
-
|
| 24 |
-
@app.route('/')
|
| 25 |
-
def index():
|
| 26 |
-
return render_template('index.html')
|
| 27 |
-
|
| 28 |
-
@app.route('/client')
|
| 29 |
-
def client():
|
| 30 |
-
return render_template('client.html')
|
| 31 |
-
|
| 32 |
-
@app.route('/host')
|
| 33 |
-
def host():
|
| 34 |
-
return render_template('host.html', exams=exams)
|
| 35 |
-
|
| 36 |
-
@socketio.on('join')
|
| 37 |
-
def on_join(data):
|
| 38 |
-
username = data['username']
|
| 39 |
-
user_id_number = random.randint(1000, 9999)
|
| 40 |
-
participants[request.sid] = {"user_id_number": user_id_number, "username": username, "score": 0}
|
| 41 |
-
join_room('quiz')
|
| 42 |
-
emit('update_participants', {"participants": participants, "count": len(participants)}, room='quiz')
|
| 43 |
-
print(f"{username} (ID: {user_id_number}) joined the quiz.")
|
| 44 |
-
|
| 45 |
-
@socketio.on('disconnect')
|
| 46 |
-
def on_leave():
|
| 47 |
-
if request.sid in participants:
|
| 48 |
-
username = participants[request.sid]["username"]
|
| 49 |
-
leave_room('quiz')
|
| 50 |
-
del participants[request.sid]
|
| 51 |
-
emit('update_participants', {"participants": participants, "count": len(participants)}, room='quiz')
|
| 52 |
-
print(f"{username} left the quiz.")
|
| 53 |
-
|
| 54 |
-
@socketio.on('load_quiz')
|
| 55 |
-
def load_quiz(data):
|
| 56 |
-
global selected_questions, current_question
|
| 57 |
-
exam_name = data['exam_name']
|
| 58 |
-
start_question = data.get('start_question', 1) - 1 # Default to question 1 if not provided
|
| 59 |
-
selected_questions = backend.select_exam(exam_name)
|
| 60 |
-
if selected_questions:
|
| 61 |
-
num_questions = len(selected_questions)
|
| 62 |
-
current_question['index'] = start_question # Set the starting question index
|
| 63 |
-
emit('quiz_loaded', {"success": True, "num_questions": num_questions, "start_question": start_question + 1}, room=request.sid)
|
| 64 |
-
else:
|
| 65 |
-
emit('quiz_loaded', {"success": False}, room=request.sid)
|
| 66 |
-
|
| 67 |
-
@socketio.on('start_quiz')
|
| 68 |
-
def start_quiz():
|
| 69 |
-
if participants and selected_questions:
|
| 70 |
-
current_question['started'] = True
|
| 71 |
-
index = current_question['index']
|
| 72 |
-
question = selected_questions[index]
|
| 73 |
-
# Send the starting question to all clients
|
| 74 |
-
emit('new_question', {"question": question["question"], "options": question["options"], "index": index + 1}, room='quiz')
|
| 75 |
-
emit('enable_end_quiz', room=request.sid) # Enable "End Quiz" for the host
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
@socketio.on('restart_quiz')
|
| 81 |
-
def restart_quiz():
|
| 82 |
-
reset_quiz()
|
| 83 |
-
emit('quiz_reset', room='quiz')
|
| 84 |
-
start_quiz()
|
| 85 |
-
|
| 86 |
-
@socketio.on('submit_answer')
|
| 87 |
-
def receive_answer(data):
|
| 88 |
-
username = participants[request.sid]["username"]
|
| 89 |
-
answer = data['answer']
|
| 90 |
-
current_question['answers'][username] = answer
|
| 91 |
-
print(f"{username} submitted an answer: {answer}")
|
| 92 |
-
|
| 93 |
-
@socketio.on('check_answers')
|
| 94 |
-
def check_answers():
|
| 95 |
-
index = current_question['index']
|
| 96 |
-
if index < len(selected_questions):
|
| 97 |
-
question = selected_questions[index]
|
| 98 |
-
correct_answer = question['correct']
|
| 99 |
-
results = {
|
| 100 |
-
"question": question["question"],
|
| 101 |
-
"answers": current_question["answers"],
|
| 102 |
-
"correct_answer": correct_answer
|
| 103 |
-
}
|
| 104 |
-
|
| 105 |
-
chart_base64 = generate_chart(current_question["answers"], question["options"])
|
| 106 |
-
emit('display_results', {"results": results, "chart": chart_base64}, room='quiz')
|
| 107 |
-
|
| 108 |
-
for sid, participant in participants.items():
|
| 109 |
-
if current_question['answers'].get(participant["username"]) == correct_answer:
|
| 110 |
-
participants[sid]["score"] += 1
|
| 111 |
-
|
| 112 |
-
@socketio.on('next_question')
|
| 113 |
-
def next_question():
|
| 114 |
-
current_question['index'] += 1
|
| 115 |
-
current_question['answers'] = {}
|
| 116 |
-
if current_question['index'] < len(selected_questions):
|
| 117 |
-
question = selected_questions[current_question['index']]
|
| 118 |
-
emit('clear_results', room='quiz')
|
| 119 |
-
emit('new_question', {"question": question["question"], "options": question["options"], "index": current_question['index'] + 1}, room='quiz')
|
| 120 |
-
else:
|
| 121 |
-
final_results = calculate_final_results()
|
| 122 |
-
emit('display_final_results', final_results, room='quiz')
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
@socketio.on('end_quiz')
|
| 126 |
-
def end_quiz():
|
| 127 |
-
if current_question['started']: # Ensure the quiz has started before ending it
|
| 128 |
-
final_results = calculate_final_results()
|
| 129 |
-
emit('display_final_results', final_results, room='quiz')
|
| 130 |
-
reset_quiz() # Reset the quiz state
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
def generate_chart(answers, options):
|
| 134 |
-
letters = [chr(65 + i) for i in range(len(options))] # Dynamically generate letters for options
|
| 135 |
-
counts = [list(answers.values()).count(option) for option in options]
|
| 136 |
-
plt.figure(figsize=(6, 4))
|
| 137 |
-
plt.bar(letters, counts)
|
| 138 |
-
plt.xlabel('Options')
|
| 139 |
-
plt.ylabel('Number of Votes')
|
| 140 |
-
plt.title('Results')
|
| 141 |
-
buf = BytesIO()
|
| 142 |
-
plt.savefig(buf, format='png')
|
| 143 |
-
buf.seek(0)
|
| 144 |
-
chart_base64 = base64.b64encode(buf.read()).decode('utf-8')
|
| 145 |
-
buf.close()
|
| 146 |
-
plt.close()
|
| 147 |
-
return chart_base64
|
| 148 |
-
|
| 149 |
-
def calculate_final_results():
|
| 150 |
-
sorted_scores = sorted(participants.values(), key=lambda x: x['score'], reverse=True)
|
| 151 |
-
return [{"username": p["username"], "score": p["score"]} for p in sorted_scores]
|
| 152 |
-
|
| 153 |
-
def reset_quiz():
|
| 154 |
-
global selected_questions, current_question
|
| 155 |
-
current_question = {"index": 0, "answers": {}, "started": False}
|
| 156 |
-
for participant in participants.values():
|
| 157 |
-
participant["score"] = 0
|
| 158 |
-
|
| 159 |
-
if __name__ == '__main__':
|
| 160 |
-
socketio.run(app, host='0.0.0.0', port=7860, debug=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
backend.py
DELETED
|
@@ -1,23 +0,0 @@
|
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|