-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
116 lines (84 loc) · 3.45 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
__author__ = 'spousty'
import psycopg2
from bottle import route, run, get, static_file, DEBUG
import os,json
@route('/')
def index():
return static_file("index.html", root='./')
@get('/ws/zips')
def getzips():
results = []
try:
conn = psycopg2.connect(database=os.environ.get('POSTGRES_DB'), user=os.environ.get('POSTGRES_USER'),
host=os.environ.get('POSTGRES_HOST'), password=os.environ.get('POSTGRES_PASSWORD'))
except:
print("couldn't make connection" + os.environ.get('POSTGRES_HOST'))
cur = conn.cursor()
cur.execute("""select zipcode, count, ST_AsText(the_geom) from zipcodes""")
rows = cur.fetchall()
for row in rows:
result = {}
result = {'zipcode': row[0], 'count': row[1]}
coords = []
temp_coords = row[2]
lon = temp_coords[temp_coords.find('(')+ 1:temp_coords.find(' ')]
lat = temp_coords[temp_coords.find(' '):temp_coords.find(')')]
coords = [lon, lat]
result['coords'] = coords
results.append(result)
return json.dumps({'results': list(results)})
@get('/ws/airports')
def getairports():
results = []
try:
conn = psycopg2.connect(database=os.environ.get('POSTGRES_DB'), user=os.environ.get('POSTGRES_USER'),
host=os.environ.get('POSTGRES_HOST'), password=os.environ.get('POSTGRES_PASSWORD'))
except:
print(os.environ.get('POSTGRES_HOST'))
cur = conn.cursor()
cur.execute("""select name, passengers, ST_AsText(the_geom) from airports""")
rows = cur.fetchall()
for row in rows:
result = {}
result = {'name': row[0], 'passengers': row[1]}
coords = []
temp_coords = row[2]
lon = temp_coords[temp_coords.find('(')+ 1:temp_coords.find(' ')]
lat = temp_coords[temp_coords.find(' '):temp_coords.find(')')]
coords = [lon, lat]
result['coords'] = coords
results.append(result)
return json.dumps({'results': list(results)})
#This is for the parkpoints data set which may or may not be there
@get('/db')
def dbexample():
try:
conn = psycopg2.connect(database=os.environ.get('POSTGRES_DB'), user=os.environ.get('POSTGRES_USER'),
host=os.environ.get('POSTGRES_HOST'), password=os.environ.get('POSTGRES_PASSWORD'))
except:
print(os.environ.get('POSTGRES_HOST'))
cur = conn.cursor()
# cur.execute("""select parkid, name, ST_AsText(the_geom) from parkpoints limit 10""")
cur.execute("""select parkid, name, ST_AsText(the_geom) from parkpoints ORDER by parkid DESC LIMIT 10""")
rows = cur.fetchall()
result_string = "<h2>Here are your results: </h2>"
for row in rows:
result_string += "<h3>" + str(row[0]) + ", " + str(row[1]) + ", " + str(row[2]) + "</h3>"
cur.close()
conn.close()
return result_string
#For Static files
@get("/static/css/<filename:re:.*\.css>")
def css(filename):
return static_file(filename, root="static/css")
@get("/static/font/<filename:re:.*\.(eot|otf|svg|ttf|woff|woff2?)>")
def font(filename):
return static_file(filename, root="static/font")
@get("/static/img/<filename:re:.*\.(jpg|png|gif|ico|svg)>")
def img(filename):
return static_file(filename, root="static/img")
@get("/static/js/<filename:re:.*\.js>")
def js(filename):
return static_file(filename, root="static/js")
if __name__ == '__main__':
run(host='0.0.0.0', port=8080, debug=True, reloader=True)