|
from flask import render_template, request, redirect, url_for, jsonify |
|
from app import create_app |
|
import json |
|
import random |
|
|
|
app = create_app() |
|
|
|
questions = [] |
|
user_answers = [] |
|
|
|
@app.route('/') |
|
def index(): |
|
return render_template('index.html') |
|
|
|
@app.route('/start', methods=['POST']) |
|
def start(): |
|
global questions |
|
file = request.files['file'] |
|
questions = json.load(file) |
|
random.shuffle(questions) |
|
return redirect(url_for('quiz')) |
|
|
|
@app.route('/quiz') |
|
def quiz(): |
|
return render_template('quiz.html') |
|
|
|
@app.route('/questions') |
|
def get_questions(): |
|
return jsonify(questions) |
|
|
|
@app.route('/submit', methods=['POST']) |
|
def submit(): |
|
global user_answers |
|
user_answers = request.json |
|
return jsonify({'status': 'success'}) |
|
|
|
def results(): |
|
correct_answers = 0 |
|
total_questions = len(questions) |
|
|
|
for user_answer in user_answers: |
|
question_index = user_answer['questionIndex'] |
|
selected_options = user_answer['answers'] |
|
correct_options = questions[question_index]['correct'] |
|
|
|
if set(selected_options) == set(correct_options): |
|
correct_answers += 1 |
|
|
|
score = (correct_answers / total_questions) * 100 |
|
return render_template('results.html', score=score, total_questions=total_questions, correct_answers=correct_answers) |
|
|
|
if __name__ == '__main__': |
|
app.run(debug=True) |
|
|