Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
66 lines (51 loc) · 1.39 KB
/
Copy pathapp.py
File metadata and controls
66 lines (51 loc) · 1.39 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 import Flask, render_template, json, request
from flaskext.mysql import MySQL
mysql = MySQL()
app = Flask(__name__)
# MySQL configurations
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'jay'
app.config['MYSQL_DATABASE_DB'] = 'BucketList'
app.config['MYSQL_DATABASE_HOST'] = 'some-mysql'
mysql.init_app(app)
# Web main page method
@app.route('/')
def main():
# Empty answer by default
prettyView = ''
# Try cause we can catch fail with connect to db
try:
# Mysql connection
conn = mysql.connect()
cursor = conn.cursor()
# Get data from mysql
cursor.execute("SELECT first_name FROM employees")
data = cursor.fetchall()
# Show what get
print(data)
# Generate pretty view
for item in data:
prettyView += item[0] + '<br/>'
except Exception as e:
# return error if connect to db fail
return "Error in get data from mysql"
finally:
# Close db connection
cursor.close()
conn.close()
# Return page
return """
<!DOCTYPE html>
<html lang="en">
<head>
<title>Python Flask + Mysql Demo App</title>
</head>
<body>
<h3>Our supper heroes:</h3>
{}
</body>
</html>
""".format(prettyView)
if __name__ == "__main__":
# Up web server
app.run(host='0.0.0.0', port=5000)