Return main HTML file
examples/fastapi/return-html-file/main.py
from fastapi import FastAPI, Response import os root = os.path.dirname(os.path.abspath(__file__)) app = FastAPI() @app.get("/") async def main(): #print(root) with open(os.path.join(root, 'index.html')) as fh: data = fh.read() return Response(content=data, media_type="text/html") @app.get("/hello") async def hello(): return {"message": "Hello World"}
examples/fastapi/return-html-file/index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes"> <title>Demo FastAPI</title> </head> <body> <h1>Main subject</h1> <a href="/hello">hello</a> </body> </html>