-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
63 lines (48 loc) · 2.24 KB
/
main.py
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
#!/usr/bin/python3
from typing import Optional
from fastapi import FastAPI, Query
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from starlette.requests import Request
from starlette.responses import HTMLResponse
import uvicorn
import requests
import htmx
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.gzip import GZipMiddleware
app = FastAPI()
app.add_middleware(GZipMiddleware)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Replace with the list of allowed origins (or use "*" for any origin)
allow_methods=["*"], # Replace with the list of allowed HTTP methods (e.g., ["GET", "POST"])
allow_headers=["*"], # Replace with the list of allowed headers
)
# Mount the "static" directory as a route to serve static files
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")
@app.get("/home", response_class=HTMLResponse)
async def read_root(request: Request):
return templates.TemplateResponse("index.html", context={"request": request})
@app.get("/", response_class=HTMLResponse)
async def read_root(request: Request):
response = requests.get("https://rickandmortyapi.com/api/character")
data = response.json()
return templates.TemplateResponse("character_card.html", context={"request": request, "characters": data["results"]})
@app.get("/data", response_class=HTMLResponse)
async def read_data(request: Request, search: Optional[str]=Query(None)):
if search:
response = requests.get(f"https://rickandmortyapi.com/api/character/?name={search}")
data = response.json()
else:
response = requests.get(f"https://rickandmortyapi.com/api/character")
data = response.json()
return templates.TemplateResponse("character_card.html", context={"request": request, "characters": data["results"]})
if __name__ == "__main__":
uvicorn.run(app="main:app",host="0.0.0.0", reload=True)
# @app.post("/submit")
# async def submit_form(request: Request):
# form_data = await request.form()
# # Process form_data as needed
# return htmx.json_response({"message": "Form submitted successfully!"})