-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
62 lines (52 loc) · 2 KB
/
main.py
File metadata and controls
62 lines (52 loc) · 2 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
from fastapi import FastAPI, UploadFile, File, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import StreamingResponse, JSONResponse, FileResponse
from fastapi.staticfiles import StaticFiles
import pandas as pd
from io import BytesIO
import logic # <-- normal import (NOT: from . import logic)
import os
app = FastAPI()
# Enable CORS for frontend
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Since all files (index.html, style.css, script.js) are in SAME folder:
app.mount("/static", StaticFiles(directory="."), name="static")
@app.get("/")
def read_root():
return FileResponse('index.html')
@app.get("/style.css")
def read_style():
return FileResponse('style.css')
@app.get("/script.js")
def read_script():
return FileResponse('script.js')
@app.get("/api/template")
def get_template():
try:
output = logic.generate_template_file()
return StreamingResponse(
output,
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
headers={"Content-Disposition": "attachment; filename=data_detail_template.xlsx"}
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/api/generate-plan")
async def generate_plan(file: UploadFile = File(...)):
if not file.filename.endswith('.xlsx'):
raise HTTPException(status_code=400, detail="Invalid file format. Please upload an Excel file.")
try:
contents = await file.read()
result = logic.process_plan(contents)
return JSONResponse(content=result)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error processing plan: {str(e)}")
@app.post("/api/download-plan")
async def download_plan(file: UploadFile = File(...)):
return {"message": "Use frontend to export CSV from data"}