-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
47 lines (41 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
from flask_mysqldb import MySQL
from flask import Flask, request, jsonify
app = Flask(__name__)
app.config["MYSQL_HOST"] = '##'
app.config["MYSQL_USER"] = 'root'
app.config["MYSQL_PASSWORD"] = '123123123'
app.config["MYSQL_DB"] = 'flaskapp'
mysql = MySQL(app)
html = """
<br><h2>Please, type your <i>Name</i>, favorite <i>color</i> and choose your <i>pet</i>:</h2>
<br>
<form method='POST' action='/'>
Name: <input type='text' name='name' required="required"> <br>
Color: <input type='text' name='color' required="required"> <br>
Pet: <select name='pet' required>
<option value=""> Select Pet</option>
<option value="cat">Cat</option>
<option value="dog">Dog</option>
</select> <br>
<br>
<input type='submit'>
</form>
"""
@app.route('/', methods = ['GET', 'POST'])
def index():
if request.method == 'POST':
userDetails = request.form
name = userDetails['name']
color = userDetails['color']
pet = userDetails['pet']
cur = mysql.connection.cursor()
try:
cur.execute("INSERT INTO Customers(name, color, pet) VALUES (%s, %s, %s)",(name, color, pet))
mysql.connection.commit()
cur.close()
return "Success, Welcome {}, your favorite color is {}, and you love {}s as I do".format(name, color, pet)
except:
return "You are already registered"
return html
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=80)