-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
67 lines (54 loc) · 2.09 KB
/
app.py
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
62
63
64
65
66
67
from flask import Flask, render_template, request, redirect, url_for, session
import db
from main import generate_quiz
app = Flask(__name__)
app.secret_key = 'supersecretkey'
@app.route('/')
def home():
return render_template('home.html')
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/quizzes')
def quizzes():
topics = db.get_topics()
return render_template('quizzes.html', topics=topics)
@app.route('/quiz/<topic>', methods=['GET', 'POST'])
def quiz(topic):
questions = db.get_quiz(topic)
if request.method == 'POST':
answers = request.form.to_dict()
session['answers'] = answers
return redirect(url_for('quiz_summary', topic=topic))
if questions:
return render_template('quiz.html', topic=topic, questions=questions)
else:
return render_template('create_quiz.html', topic=topic)
@app.route('/delete_quiz/<topic>')
def delete_quiz(topic):
db.delete_quiz(topic)
return redirect(url_for('quizzes'))
@app.route('/create_quiz/<topic>', methods=['POST'])
def create_quiz(topic):
new_topic = request.form['new_topic']
quiz_data = generate_quiz(new_topic)
db.create_quiz(new_topic, quiz_data)
return redirect(url_for('quizzes'))
@app.route('/quiz_summary/<topic>')
def quiz_summary(topic):
questions = db.get_quiz(topic)
answers = session.get('answers', {})
correct_count = 0
attempted_count = 0
total_questions = len(questions)
for i, question in enumerate(questions):
correct_answer = question['correct_answer']
user_answer = answers.get(f'question{i+1}')
if user_answer:
attempted_count += 1
if user_answer == correct_answer:
correct_count += 1
percentage = (correct_count / total_questions) * 100 if total_questions > 0 else 0
return render_template('quiz_summary.html', topic=topic, questions=questions, answers=answers, correct_count=correct_count, attempted_count=attempted_count, total_questions=total_questions, percentage=percentage)
if __name__ == '__main__':
app.run(debug=True)