-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqueryGrapher.py
More file actions
66 lines (49 loc) · 1.42 KB
/
queryGrapher.py
File metadata and controls
66 lines (49 loc) · 1.42 KB
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
from flask.ext.mysql import MySQL
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash, jsonify
from contextlib import closing
# configuration
DEBUG = True
# variables for database access
HOST = 'sql3.freemysqlhosting.net'
USER = 'sql3114361'
PASSWD = 'zZl9FPvPV9'
DATABASE = 'sql3114361'
# create application
app = Flask(__name__)
mysql = MySQL()
app.config.from_object(__name__)
app.config['MYSQL_DATABASE_HOST'] = HOST
app.config['MYSQL_DATABASE_USER'] = USER
app.config['MYSQL_DATABASE_PASSWORD'] = PASSWD
app.config['MYSQL_DATABASE_DB'] = DATABASE
mysql.init_app(app)
def connect_db():
return mysql.connect()
@app.before_request
def before_request():
g.db = connect_db()
@app.teardown_request
def teardown_request(exception):
db = getattr(g, 'db', None)
if db is not None:
db.close()
#Custom query
@app.route('/getData', methods=['GET'])
def getData():
result = None
cursor = g.db.cursor()
#get all interesting queries in one shot, divide them up later
query=request.args.get('query')
cursor.execute(query)
# Get column headers
result = [[word[0] for word in cursor.description]]
#get data
queryRows = cursor.fetchall()
for row in queryRows:
result.append(row)
return jsonify(result=result)
@app.route('/')
def mainPg():
return render_template('customQuery.html')
if __name__ == '__main__':
app.run()