fastapi 返回html文件Demo
时间: 2023-09-14 08:11:22 浏览: 45
Sure, here's a simple example of how to return an HTML file using FastAPI:
```
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
app = FastAPI()
@app.get("/", response_class=HTMLResponse)
def read_root():
html_content = """
<html>
<head>
<title>Welcome to my website!</title>
</head>
<body>
<h1>Hello, world!</h1>
<p>This is a simple HTML page returned by FastAPI.</p>
</body>
</html>
"""
return html_content
```
When you run this code and go to http://localhost:8000/ in your web browser, you should see a basic HTML page with a title, header, and paragraph. Hope that helps!