-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
70 lines (60 loc) · 1.93 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
#!flask/bin/python
from flask import Flask, jsonify, render_template, redirect, url_for
from flask.ext.cors import CORS
from flask import g
from flask import Response
from flask import request
import json
import MySQLdb
app = Flask(__name__)
CORS(app)
'''cors = CORS(app, resources={r"/*": {"origins": "*"}})'''
@app.before_request
def db_connect():
g.conn = MySQLdb.connect(host='mysql.ip.address',
user='user.name',
passwd='user.password',
db='database.name')
g.cursor = g.conn.cursor()
@app.after_request
def db_disconnect(response):
g.cursor.close()
g.conn.close()
return response
@app.route('/')
def home():
'''return render_template('index.html', title = 'Projects')'''
return redirect(url_for('static', filename='index.html'))
@app.route('/rooms', methods=['GET'])
def rooms():
g.cursor.execute('SELECT roomid, roomname from rooms')
rooms = g.cursor.fetchall()
return jsonify({'rooms': rooms.name})
@app.route("/names", methods=['GET'])
def names():
g.cursor.execute('SELECT personid, firstname, lastname from persons')
result = g.cursor.fetchall()
data = json.dumps(result)
resp = Response(data, status=200, mimetype='application/json')
return resp
@app.route("/sales", methods=['GET'])
def sales():
g.cursor.execute('select year,age,sex,people from sales')
data = g.cursor.fetchall()
sales_list=[]
for item in data:
i = {
'year':item[0],
'age':item[1],
'people':item[2]
}
sales_list.append(i)
data = json.dumps(sales_list)
resp = Response(data, status=200, mimetype='application/json')
return resp
'''return {'StatusCode':'200','Sales':sales_list}'''
if __name__ == '__main__':
app.run(debug=True)
''' return jsonify(year=result[0],
age=result[1],
people=result[2])'''