File size: 2,050 Bytes
50b7a69
 
 
 
9a82c5c
 
50b7a69
9a82c5c
 
50b7a69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a82c5c
 
b4443a9
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
55
56
57
58
59
60
61
from flask import Flask, render_template, request, redirect, url_for, session
import json
import random
import os

app = Flask(__name__)
app.secret_key = 'supersecretkey'

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/start', methods=['POST'])
def start():
    session['name'] = request.form['name']
    session['questions'] = []
    session['answers'] = []
    session['score'] = 0
    session['current_question'] = 0

    file = request.files['file']
    if file:
        questions = json.load(file)
        random.shuffle(questions)
        session['questions'] = questions
        return redirect(url_for('quiz'))
    return redirect(url_for('index'))

@app.route('/quiz', methods=['GET', 'POST'])
def quiz():
    if 'questions' not in session or 'current_question' not in session:
        return redirect(url_for('index'))

    if request.method == 'POST':
        answer = request.form.get('answer')
        if answer:
            session['answers'].append(answer)
            if answer in session['questions'][session['current_question']]['correct']:
                session['score'] += 1

        session['current_question'] += 1
        if session['current_question'] >= len(session['questions']):
            return redirect(url_for('results'))

    return render_template('quiz.html', question=session['questions'][session['current_question']],
                           question_number=session['current_question'] + 1,
                           total_questions=len(session['questions']))

@app.route('/results')
def results():
    if 'questions' not in session or 'answers' not in session:
        return redirect(url_for('index'))

    total_questions = len(session['questions'])
    score_percentage = (session['score'] / total_questions) * 100

    return render_template('results.html', name=session['name'], score=session['score'],
                           total_questions=total_questions, score_percentage=score_percentage)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860)