-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_entry_app.py
More file actions
114 lines (95 loc) · 3.38 KB
/
data_entry_app.py
File metadata and controls
114 lines (95 loc) · 3.38 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from flask import Flask, render_template, request, jsonify
import time
import mysql.connector
import pdb
import logging
from logging.handlers import RotatingFileHandler
app = Flask(__name__, template_folder='.', static_url_path='', static_folder='static') # Specify the template folder
# Configure the logger
handler = RotatingFileHandler('app.log', maxBytes=10000, backupCount=1)
handler.setLevel(logging.DEBUG)
# Set the format of the log messages
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
# Attach the handler to the app's logger
app.logger.addHandler(handler)
# Set the log level for the app
app.logger.setLevel(logging.DEBUG)
def create_table():
conn = mysql.connector.connect(
host="db",
database="your_database",
user="gburucua",
password="q1w2e3r4"
)
cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age VARCHAR(255))")
conn.commit()
conn.close()
def insert_data(name, age):
conn = mysql.connector.connect(
host="db",
database="your_database",
user="gburucua",
password="q1w2e3r4"
)
cursor = conn.cursor()
query = "INSERT INTO users (name, age) VALUES (%s, %s)"
values = (name, age)
cursor.execute(query, values)
conn.commit()
conn.close()
def fetch_data_from_database():
connection = mysql.connector.connect(
host="db",
database="your_database",
user="gburucua",
password="q1w2e3r4"
)
cursor = connection.cursor()
cursor.execute("SELECT * FROM your_table")
data = cursor.fetchall()
cursor.close()
connection.close()
return data
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
name = request.form["name"]
#surname = request.form["surname"]
age = request.form["age"]
#gender = request.form["gender"]
#comments = request.form["comments"]
# Print the form data
print("Form Data:", request.form)
pdb.set_trace() # Add this line to start debugging
try:
create_table()
insert_data(name, age)
return render_template("index.html", data_saved=True)
except mysql.connector.Error as error:
error_message = f"An error occurred while connecting to the database: {error}"
return render_template("users.html", error_message=error_message, data_saved=False)
return render_template("users.html", data_saved=False)
@app.route("/api/data", methods=["GET"])
def get_data():
# Retrieve data from the database and return as JSON
data = fetch_data_from_database()
return jsonify(data)
@app.route("/api/data", methods=["POST"])
def save_data():
try:
# Log the request data
app.logger.info("Request data: %s", request.get_data(as_text=True))
name = request.form["name"]
#surname = request.form["surname"]
age = request.form["age"]
#gender = request.form["gender"]
#comments = request.form["comments"]
create_table()
insert_data(name, age)
return jsonify({"message": "Data saved successfully"}), 200
except Exception as e:
return jsonify({"error": str(e)}), 400
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)