-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
52 lines (37 loc) · 1.35 KB
/
app.py
File metadata and controls
52 lines (37 loc) · 1.35 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
from fastapi import FastAPI, Form, Request
from fastapi.responses import FileResponse, HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from loguru import logger
from src.api import ApiGet
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
app.mount("/chart", StaticFiles(directory="chart"), name="chart")
templates = Jinja2Templates(directory="templates")
@app.get("/favicon.ico")
def favicon():
return FileResponse("static/favicon.ico")
@app.get("/", response_class=HTMLResponse)
async def index(request: Request):
return templates.TemplateResponse(
request=request, name="index.html"
)
@app.get("/api/{user}")
async def api(_: Request, user: str):
return handle_generate_chart(user)
@app.post("/search")
async def search(_: Request, user: str = Form(...)):
return handle_generate_chart(user)
def handle_generate_chart(user: str): # utils
if user.strip() == "":
return {"ERROR": "Username cannot be blank"}
try:
api = ApiGet(user)
api.generate_chart()
except Exception as e:
logger.error(e)
return {"ERROR": "username not found"}
return FileResponse(f"./chart/{user}_profile.svg")
if __name__ == "__main__":
import uvicorn
uvicorn.run("app:app", host="127.0.0.1", port=3010, reload=True)