-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflask_trivia.py
More file actions
86 lines (70 loc) · 2.79 KB
/
flask_trivia.py
File metadata and controls
86 lines (70 loc) · 2.79 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from flask import Flask
from flask import render_template
from flask import request
from flask import redirect
from flask import url_for
import random
import html
import requests
app = Flask(__name__)
def build_url(amount, category, difficulty, q_type):
return f"https://opentdb.com/api.php?amount={amount}&category={category}&difficulty={difficulty}&type={q_type}"
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
questions = request.form.getlist('question_index')
answers = [request.form[f'answer_{index}'] for index in questions]
# Now 'answers' list contains the user's selected answers for each question
# You can compare them with the correct answers and process the results accordingly
# Ensure 'categories' is defined for both GET and POST requests
categories = {
9: "General Knowledge",
10: "Entertainment- Books",
11: "Entertainment- Film",
12: "Entertainment- Music",
13: "Entertainment- Musicals & Theater",
14: "Entertainment- Television",
15: "Entertainment- Video Games",
16: "Entertainment- Board Games",
17: "Science- Nature",
18: "Science- Computers",
19: "Science- Mathematics",
20: "Mythology",
21: "Sports",
22: "Geography",
23: "History",
24: "Politics",
25: "Art",
26: "Celebrities",
27: "Animals",
28: "Vehicles",
29: "Entertainment- Comics",
30: "Science- Gadgets",
31: "Entertainment- Japanese Anime & Manga",
32: "Entertainment- Cartoon Animations"
}
if request.method == 'POST':
amount = request.form['amount']
category = request.form['category']
difficulty = request.form['difficulty']
q_type = request.form['q_type']
url = build_url(amount, category, difficulty, q_type)
data = requests.get(url).json()
if 'results' in data:
results = data['results']
user_answers = request.form.getlist('answer')
for question_data in results:
question = html.unescape(question_data['question'])
all_answers = [html.unescape(answer) for answer in question_data['incorrect_answers']]
correct_answer = html.unescape(question_data['correct_answer'])
all_answers.append(correct_answer)
random.shuffle(all_answers)
questions.append({
'question': question,
'answers': all_answers,
'correct_answer': correct_answer
})
return render_template('index02.html', questions=questions, user_answers=user_answers, results=results, categories=categories)
return render_template('index02.html', categories=categories)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=2224)