-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
33 lines (28 loc) · 900 Bytes
/
main.py
File metadata and controls
33 lines (28 loc) · 900 Bytes
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
from fastapi import FastAPI, Query
from fastapi.middleware.cors import CORSMiddleware
import csv
app = FastAPI()
# Enable CORS (allow GET requests from any origin)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["GET"],
allow_headers=["*"],
)
# Read CSV data once at startup
students_data = []
with open("students.csv", newline="") as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
# Convert studentId to int
row["studentId"] = int(row["studentId"])
students_data.append(row)
@app.get("/api")
def get_students(class_: list[str] = Query(default=None, alias="class")):
"""
Returns all students, or filters by ?class=1A&class=1B etc.
"""
if class_:
filtered = [s for s in students_data if s["class"] in class_]
return {"students": filtered}
return {"students": students_data}