-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
62 lines (50 loc) · 1.48 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
"""
Main application file. See readme for instructions on how to run
"""
from flask import Flask, jsonify, request
import sqlite3
import pandas as pd
# == configuration
app = Flask(__name__)
class config():
API_PORT = 5000
DEV_PORT = 5005
@app.route('/')
def isAlive():
return "hello world"
# == routes == #
@app.route('/routes/<int:rid>')
def routes(rid):
conn = sqlite3.connect("dataset.db")
q = "SELECT * FROM data WHERE `index` = ?"
r = pd.read_sql(q, conn, params=[rid])
conn.close()
return jsonify(r.to_dict(orient='records')[0])
@app.route('/routes')
def flight_list():
conn = sqlite3.connect("dataset.db")
q = "SELECT `index` FROM data"
r = pd.read_sql(q, conn)
ids = [str(row[0]) for row in r.values]
conn.close()
return str(list(map(int, ids)))
@app.route('/search')
def search():
query = request.args.get('query')
print(query)
conn = sqlite3.connect("dataset.db")
q = r"SELECT * FROM data WHERE UPPER(ORIGIN_DESC) LIKE '%' || UPPER(?) || '%' OR UPPER(DEST_DESC) LIKE '%' || UPPER(?) || '%'"
r = pd.read_sql(q, conn, params=[query, query])
conn.close()
return jsonify(r.to_dict(orient='records'))
# == execution logic == #
def main(prod: ('production mode', 'flag', 'p')):
""" about the program """
if not prod:
app.run(debug=True, host='0.0.0.0', port=config.DEV_PORT)
else:
from waitress import serve
serve(app, listen=f'*:{config.API_PORT}')
if __name__=='__main__':
import plac
plac.call(main)