-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
56 lines (41 loc) · 1.15 KB
/
app.py
File metadata and controls
56 lines (41 loc) · 1.15 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
from flask import Flask, render_template, request
import mysql.connector
app = Flask(__name__)
def db_connection():
db = mysql.connector.connect(
host="mysql",
user="root",
password="123456",
database="user_db"
)
cursor = db.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100)
)
""")
return [db, cursor]
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
name = request.form["name"]
email = request.form["email"]
[db, cursor] = db_connection()
cursor.execute(
"INSERT INTO users (name, email) VALUES (%s, %s)",
(name, email)
)
db.commit()
return render_template("index.html")
@app.route("/go")
def go():
return render_template("show.html")
@app.route("/show")
def show():
[db, cursor] = db_connection()
cursor.execute("SELECT name, email FROM users")
data = cursor.fetchall()
return render_template("show.html", data=data)
app.run(host="0.0.0.0", port=5000)