-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcoaster_app.py
76 lines (65 loc) · 1.93 KB
/
coaster_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
68
69
70
71
72
73
74
75
76
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 16 16:05:10 2017
@author: kurner
"""
from make_coasters_db import DB
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
@app.route("/coasters")
def coasters():
return all_coasters()
@app.route("/coasters/<coaster>")
def coaster(coaster):
return one_coaster(coaster)
def all_coasters():
with DB() as db:
query = ("SELECT name, park, yr_opened FROM Coasters "
"ORDER BY name")
db.get_coasters(query)
results = []
for rec in db.cursor.fetchall():
results.append(rec)
body = "<ul>"+ \
"\n".join(["<li>{}, {}, {}</li>".format(*data) for data in results]) + \
"</ul>"
return basic_html("All Coasters", body)
def one_coaster(coaster):
with DB() as db:
if coaster:
query = ("SELECT * FROM Coasters "
"WHERE name = '{}'".format(coaster))
db.get_coasters(query)
results = []
for rec in db.cursor.fetchall():
results.append(rec)
fields =("Name: {} <br /> ",
"Park: {} <br /> ",
"State: {} <br /> ",
"Country: {} <br /> ",
"Duration: {} <br /> ",
"Speed: {} <br />",
"Height: {} <br />",
"VertDrop: {} <br />",
"Length: {} <br />",
"Yr_Opened: {} <br />",
"Inversions: {} <br />")
body = ("\n".join(fields))
body = body.format(*results[0])
return basic_html(coaster, body)
def basic_html(title="Web Page", body="content goes here"):
page = ("<!doctype html>\n"
"<html>\n"
"<head>\n"
"<title>{}</title>\n"
"</head>\n"
"<body>\n"
"{}\n"
"</body>\n"
"</html>").format(title, body)
return page
if __name__ == "__main__":
app.run()