|
from flask import Flask, render_template, request, jsonify |
|
import os |
|
import json |
|
import random |
|
from datetime import datetime |
|
|
|
app = Flask(__name__) |
|
|
|
|
|
QUESTIONS_DIR = "app/questions" |
|
question_files = [f for f in os.listdir(QUESTIONS_DIR) if f.endswith('.json')] |
|
|
|
@app.route('/') |
|
def index(): |
|
return render_template('index.html', files=question_files) |
|
|
|
@app.route('/load_questions', methods=['POST']) |
|
def load_questions(): |
|
file_name = request.json.get('file_name') |
|
file_path = os.path.join(QUESTIONS_DIR, file_name) |
|
if not os.path.exists(file_path): |
|
return jsonify({"error": "File not found"}), 404 |
|
|
|
with open(file_path, 'r') as f: |
|
questions = json.load(f) |
|
random.shuffle(questions) |
|
return jsonify(questions) |
|
|
|
@app.route('/submit_results', methods=['POST']) |
|
def submit_results(): |
|
data = request.json |
|
questions = data.get('questions') |
|
user_answers = data.get('user_answers') |
|
|
|
correct_count = 0 |
|
total_questions = len(questions) |
|
for q, ua in zip(questions, user_answers): |
|
if set(q['correct']) == set(ua): |
|
correct_count += 1 |
|
|
|
score = (correct_count / total_questions) * 100 |
|
return jsonify({"score": score}) |
|
|
|
if __name__ == '__main__': |
|
app.run(host='0.0.0.0', port=7860) |