| 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) |