-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgiulio_naga_api.py
196 lines (154 loc) · 5.91 KB
/
giulio_naga_api.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
from flask import Flask, render_template, jsonify, request, Response
import pandas as pd
import duckdb
app = Flask(__name__)
# df = pd.read_csv("energy.csv")
def get_connection():
return duckdb.connect("energy.db") # create a connection to the database
# @app.route("/")
# def show_table():
# offset = request.args.get("offset", default=0, type=int)
# limit = request.args.get("limit", default=100, type=int)
# query = f"SELECT * FROM energy LIMIT {limit} OFFSET {offset}"
# df = con.sql(query).df()
# return render_template("index.html", table=df.to_html())
@app.route("/")
def show_table():
offset = request.args.get("offset", default=0, type=int)
limit = request.args.get("limit", default=100, type=int)
con = get_connection()
query = "SELECT * FROM energy LIMIT ? OFFSET ?"
df = con.execute(query, [limit, offset]).df()
con.close()
return render_template("index.html", table=df.to_html())
# def calculate_intern_mean(df):
# intern_mean = float(df["Total Interns"].mean())
# return intern_mean
# def calculate_intern_min(df):
# intern_min = float(df["Total Interns"].min())
# return intern_min
# def calculate_intern_max(df):
# intern_max = float(df["Total Interns"].max())
# return intern_max
# @app.route("/intern")
# def intern_info():
# query = """
# SELECT
# CAST(AVG("Total Interns") AS FLOAT) as mean,
# CAST(MIN("Total Interns") AS FLOAT) as min,
# CAST(MAX("Total Interns") AS FLOAT) as max
# FROM energy
# """
# result = con.sql(query).df().iloc[0].to_dict()
# return jsonify(result)
# @app.route("/employee")
# def employee_mean():
# employee_mean = df["Employees"].mean()
# employee_min = df["Employees"].min()
# employee_max = df["Employees"].max()
# return jsonify(
# {"The mean of employees": employee_mean,
# "The min of employees": employee_min,
# "The max of employees": employee_max}
# )
@app.route("/employee")
def employee_info():
con = get_connection()
query = """
SELECT AVG(Employees) as mean,
MIN(Employees) as min,
MAX(Employees) as max
FROM energy
"""
result = con.execute(query).df().iloc[0].to_dict()
con.close()
return jsonify(result)
# @app.route("/filter_employees", methods=["GET"])
# def filter_employees():
# df["Employees"] = pd.to_numeric(df["Employees"], errors="coerce")
# # filter
# filter_employees = request.args.get("a", default=100, type=int)
# filtered_df = df[df["Employees"] >= filter_employees]
# return render_template("index.html", table=filtered_df.to_html())
@app.route("/filter_employees", methods=["GET"])
def filter_employees():
filter_value = request.args.get("a", default=100, type=int)
con = get_connection()
query = "SELECT * FROM energy WHERE Employees >= ?"
df = con.execute(query, [filter_value]).df()
con.close()
return render_template("index.html", table=df.to_html())
# @app.route("/record/<int:id>", methods=["GET"])
# def record(id):
# format_type = request.args.get("format", default="json", type=str).lower()
# if 0 <= id < len(df):
# record = df.iloc[id].to_dict()
# if format_type == "csv":
# csv_data = pd.DataFrame([record]).to_csv(index=False)
# return Response(csv_data, mimetype="text/csv")
# return jsonify(record)
# else:
# return jsonify({"error": "Record not found"}), 404
@app.route("/record/<int:id>", methods=["GET"])
def record(id):
format_type = request.args.get("format", default="json", type=str).lower()
con = get_connection()
query = "SELECT * FROM energy LIMIT 1 OFFSET ?"
df = con.execute(query, [id]).df()
con.close()
if df.empty:
return jsonify({"error": "Record not found"}), 404
record = df.iloc[0].to_dict()
if format_type == "csv":
csv_data = pd.DataFrame([record]).to_csv(index=False)
return Response(csv_data, mimetype="text/csv")
return jsonify(record)
# def compute_employee_stats(df):
# return {
# "The mean of employees": df["Employees"].mean(),
# "The min of employees": df["Employees"].min(),
# "The max of employees": df["Employees"].max(),
# }
# @app.route("/add_user", methods=["POST"])
# def add_user():
# data = request.json
# username = data.get("username")
# age = data.get("age")
# country = data.get("country")
# if not username or not age or not country:
# return jsonify({"error": "Missing required fields"}), 400
# con.sql("INSERT INTO users VALUES (?, ?, ?)", (username, age, country))
# return jsonify({"message": "User added successfully"}), 201
@app.route("/add_user", methods=["GET"])
def add_user():
username = request.args.get("username")
age = request.args.get("age", type=int)
country = request.args.get("country")
if not username or not age or not country:
return jsonify({"error": "Missing required fields"}), 400
con = get_connection()
con.execute("INSERT INTO users (username, age, country) VALUES (?, ?, ?)",
[username, age, country])
con.close()
return jsonify({"message": "User added successfully"}), 201
@app.route("/user_stats", methods=["GET"])
def user_stats():
con = get_connection()
total_users = con.execute("SELECT COUNT(*) FROM users").fetchone()[0]
avg_age = con.execute("SELECT AVG(age) FROM users").fetchone()[0]
query = """
SELECT country, COUNT(*) as user_count
FROM users
GROUP BY country
ORDER BY user_count DESC
LIMIT 3
"""
top_countries = con.execute(query).df().to_dict(orient="records")
con.close()
return jsonify({
"total_users": total_users,
"average_age": avg_age,
"top_countries": top_countries
})
if __name__ == "__main__":
app.run(debug=True)