forked from devfile-samples/devfile-sample-python-basic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
42 lines (35 loc) · 895 Bytes
/
Copy pathapp.py
File metadata and controls
42 lines (35 loc) · 895 Bytes
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
from flask import Flask
import os
# import necessary packages
import flask
import json
# create the flask app
app = flask.Flask(__name__)
app.config["DEBUG"] = True
# configuration used to connect to MariaDB
config = {
'host': '127.0.0.1',
'port': 3306,
'user': 'root',
'password': 'Password123!',
'database': 'demo'
}
# route to return all people
@app.route('/api/people', methods=['GET'])
def index():
# connection for MariaDB
conn = mariadb.connect(**config)
# create a connection cursor
cur = conn.cursor()
# execute a SQL statement
cur.execute("select * from people")
# serialize results into JSON
row_headers=[x[0] for x in cur.description]
rv = cur.fetchall()
json_data=[]
for result in rv:
json_data.append(dict(zip(row_headers,result)))
# return the results!
return json.dumps(json_data)
# run the app
app.run()