File size: 1,296 Bytes
1677ecc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
from flask import Flask, render_template, request, jsonify
import os
import json
import random
from datetime import datetime
app = Flask(__name__)
# Load the list of question files
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) |