You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.5 KiB

from math import floor
from random import uniform
from fastapi import FastAPI, UploadFile
from fastapi.middleware.cors import CORSMiddleware
import sqlalchemy
from sqlalchemy import text
import pandas as pd
app = FastAPI()
origins = ["http://localhost:5173"]
# noinspection PyTypeChecker
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"]
)
engine = sqlalchemy.create_engine("mariadb+mariadbconnector://cflsxjw:1212@127.0.0.1:3306/ROLLER_DB")
@app.get("/")
async def root():
return {"message": "Hello World"}
@app.get("/api/get/get_random_stu")
async def get_random_stu():
with engine.connect() as c:
stu = c.execute(text("SELECT sno, sname FROM stu_info")).fetchall()
size = stu.__len__()
return {"sno": stu[floor(uniform(0, size - 1))][0], "sname": stu[floor(uniform(0, size - 1))][1]}
@app.get("/api/get/get_random_event")
async def get_event():
return {"eventId": 10, "eventDescription": "疯狂星期四!/v我50:P"}
@app.post("/api/post/upload_stu_info")
async def upload_stu_info(file: UploadFile):
upload_file = file.file
data = pd.read_csv(upload_file)
with engine.connect() as c:
c.execute(text("TRUNCATE TABLE stu_info"))
for i in data.index.values:
with engine.begin() as c:
c.execute(text(f"INSERT INTO stu_info VALUES ('{data.values[i, 0]}', '{data.values[i,1]}', 0);"))
return file.filename