Merge html file with restapi

@app.post("/help/{id}/{surname}")
def adduser(id:int,surname:str):
cursor.execute("INSERT INTO Persons (personid,lastname) VALUES (%s,%s)",(id,surname))
connect.commit()
return "Added Succesfuly"
I have created a RestAPI application and I made a login screen with html css how can I combine my restAPI app and html wepsite
Answer
from fastapi import FastAPI, Request, Form
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
import mysql.connector
app = FastAPI()
templates = Jinja2Templates(directory="templates") # folder with your login.html
# Database connection
connect = mysql.connector.connect(host="localhost", user="root", password="pass", database="test")
cursor = connect.cursor()
@app.get("/", response_class=HTMLResponse)
def login_page(request: Request):
return templates.TemplateResponse("login.html", {"request": request})
@app.post("/login", response_class=HTMLResponse)
def handle_login(username: str = Form(...), password: str = Form(...)):
# Logic here: check user/password
return "Login successful!" # Replace with TemplateResponse if you want to show a page
Enjoyed this question?
Check out more content on our blog or follow us on social media.
Browse more questions