File size: 1,010 Bytes
e4ba7f1
33a2564
e4ba7f1
 
305d870
5492f08
e4ba7f1
ed64977
fec291d
e4ba7f1
 
 
 
fec291d
 
e4ba7f1
 
 
 
 
cc82d6a
1f75665
e4ba7f1
 
 
 
909eff0
e4ba7f1
 
fec291d
e4ba7f1
909eff0
e4ba7f1
 
909eff0
e4ba7f1
909eff0
e4ba7f1
5492f08
1c5cb1e
5492f08
909eff0
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
from flask import Flask, render_template, request, jsonify, session
import json
import os
import random

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


def load_questions():
    json_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'questions.json')
    with open(json_path, 'r', encoding='utf-8') as f:
        return json.load(f)


@app.route('/')
def index():
    questions = load_questions()
    total = len(questions)
    return render_template('index.html', total=total)


@app.route('/quiz', methods=['POST'])
def quiz():
    count = int(request.form.get('count', 30))
    questions = load_questions()

    if count > len(questions):
        count = len(questions)

    selected = random.sample(questions, count)

    for i, q in enumerate(selected):
        q['quiz_number'] = i + 1

    session['total'] = count

    return render_template('quiz.html', questions=selected, total=count)


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=7860, debug=True)