-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal_flask_app.py
More file actions
72 lines (46 loc) · 2.21 KB
/
final_flask_app.py
File metadata and controls
72 lines (46 loc) · 2.21 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
from flask import Flask, render_template, request
import random
from trivia import Trivia
app = Flask(__name__)
# flask app trivia game
# homepage that asks for player to select a category or random.
@app.route('/', methods=["GET","POST"])
def index():
# player_name = request.args.get("query")
category = request.form.get('category')
# print(category)
return render_template('index.html')
# route if player selects random category. API call for random is initiated.The question and answer submit form is rendered in the html
@app.route('/random/', methods=["GET", "POST"])
def random():
q1 = Trivia()
response_data = q1.randomtrivia()
q1.trivia_question = response_data['contents'][0]['question']
q1.trivia_correct_answer = response_data['contents'][0]['answer'][0]
answer = request.args.get("query")
return render_template('question.html', trivia_question=q1.trivia_question,trivia_correct_answer=q1.trivia_correct_answer,query=answer)
# route if player selects a category other than random. API call for category is initiated. The question and answer submit form is rendered in the html
@app.route('/categories/',methods=["GET","POST"])
def category_q():
q1 = Trivia()
category = request.form.get("category")
# print(category)
response_data = q1.categorytrivia(category)
# print(response_data)
# print(q1.trivia_question)
# print(q1.trivia_correct_answer)
q1.trivia_question = response_data[0]
q1.trivia_correct_answer = response_data[1]
answer = request.args.get("query")
return render_template('question.html', trivia_question=q1.trivia_question,trivia_correct_answer=q1.trivia_correct_answer, category=category, query=answer)
# route where user answer is validated against API call response answer. The user should be presented with conditional html based on answer.
@app.route('/validate/',methods=["GET","POST"])
def validate():
answer = request.args.get("query")
# validatetrivia(answer, trivia_correct_answer)
# answer = True
# answer = False
return render_template('validatetrivia.html', query=answer)
# Run the app when the program starts!
if __name__ == '__main__':
app.run(debug=True)