-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
84 lines (64 loc) · 2.68 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from flask import Flask, render_template, request, session, redirect
import requests
import pandas as pd
import os
import random
app = Flask(__name__)
app.secret_key = os.urandom(10).hex()
@app.route("/")
def index():
return render_template("index.html") # flight map file
@app.route("/trivia")
def trivia():
# Generate questions and restart if resubmitting
my_category = random.randint(10,32)
if "questions" not in session:
r = requests.get(f"https://opentdb.com/api.php?amount=10&category={my_category}&type=multiple")
trivia_data = r.json()
questions = trivia_data['results']
# Prepare options for each question
for q in questions:
q["options"] = q["incorrect_answers"] + [q["correct_answer"]] # list of all options
session["questions"] = questions #questions for this round
questions = session["questions"] #restores questions
return render_template("trivia.html", questions=questions)
@app.route("/trivia-submit", methods=["POST"])
def trivia_submit():
user_answers = []
# Retrieve all submitted answers
for i in range(10):
user_answers.append(request.form.get(f"answer{i + 1}"))
# Retrieve the questions from this specific session
questions = session["questions"]
# Check if the user answered correctly and create a score
correct_answers = 0
for i in range(10):
question = questions[i] # get a specific question
if user_answers[i] == question["correct_answer"]: # check if the answer matches the correct answer
correct_answers += 1 # add to the score
# Return the result to the user
return render_template("results.html", score=correct_answers)
@app.route("/ctry")
def ctry():
return render_template("country.html")
@app.route("/ctry-info", methods=["GET", "POST"])
def ctry_info():
if request.method == 'POST':
ctry = request.form.get("country")
r = requests.get(f"https://restcountries.com/v3.1/name/{ctry}?fields=name,capital,currencies,languages,car")
response = r.json()
for i in response[0]['currencies'].items():
currency_list = []
currency_list.append(i[1]['name'])
for i in response[0]['languages'].items():
lang_list = []
lang_list.append(i[1])
df = pd.DataFrame(data={
"name": response[0]['name']['common'],
"currencies": currency_list[0],
"capital": response[0]['capital'],
"languages": lang_list[0],
'car': response[0]['car']['side']})
return render_template("country-results.html", results=df)
if __name__ == "__main__":
app.run(debug=True)