File size: 1,361 Bytes
d353aa8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
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)