forked from advanced-computing/Haixin-and-Chengpu-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
79 lines (62 loc) · 2.19 KB
/
app.py
File metadata and controls
79 lines (62 loc) · 2.19 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
import pandas as pd
from flask import Flask, Response, request
app = Flask(__name__)
df = pd.read_csv("data.csv")
ID_COLUMN = "EventID"
@app.route("/permits", methods=["GET"])
def list_permits():
"""List all film permits with optional filtering, pagination, and format selection."""
result = df.copy()
# Filter by any column
for col in df.columns:
value = request.args.get(col)
if value is not None:
result = result[result[col].astype(str) == value]
# Pagination
limit = request.args.get("limit", default=None, type=int)
offset = request.args.get("offset", default=0, type=int)
result = result.iloc[offset:]
if limit is not None:
result = result.iloc[:limit]
# Output format
output_format = request.args.get("format", default="json")
if output_format == "csv":
return Response(result.to_csv(index=False), mimetype="text/csv")
else:
return Response(
result.to_json(orient="records", indent=2),
mimetype="application/json",
)
@app.route("/permits/<int:event_id>", methods=["GET"])
def get_permit(event_id):
"""Retrieve a single permit by EventID."""
match = df[df[ID_COLUMN] == event_id]
if match.empty:
return Response(
'{"error": "Permit not found"}',
status=404,
mimetype="application/json",
)
output_format = request.args.get("format", default="json")
if output_format == "csv":
return Response(match.to_csv(index=False), mimetype="text/csv")
else:
return Response(
match.to_json(orient="records", indent=2),
mimetype="application/json",
)
@app.route("/columns", methods=["GET"])
def list_columns():
"""List all available column names."""
return {"columns": list(df.columns)}
@app.route("/stats", methods=["GET"])
def get_stats():
"""Return basic statistics about the dataset."""
return {
"total_records": len(df),
"columns": list(df.columns),
"boroughs": df["Borough"].dropna().unique().tolist(),
"categories": df["Category"].dropna().unique().tolist(),
}
if __name__ == "__main__":
app.run(debug=True)