如何使用 FastAPI return 带有换行符的响应?
How to return a response with a line break using FastAPI?
@app.get('/status')
def get_func(request: Request):
output = 'this output should have a line break'
return output
我尝试过的事情:
output = this output should \n have a line break
output = this output should <br /> have a line break
返回文本本身,但我没有换行。
仅当响应是 HTML 响应(即 HTML 页面)时,换行符才有意义。并且 \n
无法正确呈现为新行或换行符,您必须使用 <br>
或 HTML template + some CSS styling to preserve line breaks.
FastAPI returns,默认情况下,JSONResponse
type.
Takes some data and returns an application/json
encoded response.
This is the default response used in FastAPI, as you read above.
但是你可以告诉它 use an HTMLResponse
type using the response_class
parameter:
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
app = FastAPI()
@app.get('/status', response_class=HTMLResponse)
def get_func():
output = 'this output should <br> have a line break'
return output
或者,为了更好地控制,使用实际的 HTML 模板。 FastAPI 支持 Jinja2 模板,请参阅 FastAPI Templates.
部分
proj/templates/output.html
<html>
<head>
</head>
<body>
<p>This output should have a <br>line break.</p>
<p>Other stuff: {{ stuff }}</p>
</body>
</html>
proj/main.py
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
app = FastAPI()
templates = Jinja2Templates(directory="templates")
@app.get('/status', response_class=HTMLResponse)
def get_func(request: Request):
return templates.TemplateResponse("output.html", {"request": request, "stuff": 123})
有了 HTML 模板,您就可以使用 CSS styling to preserve line breaks.
使用response_class=PlainTextResponse
from fastapi.responses import PlainTextResponse
@app_fastapi.get("/get_log", response_class=PlainTextResponse)
async def get_log():
return "hello\nbye\n"
@app.get('/status')
def get_func(request: Request):
output = 'this output should have a line break'
return output
我尝试过的事情:
output = this output should \n have a line break
output = this output should <br /> have a line break
返回文本本身,但我没有换行。
仅当响应是 HTML 响应(即 HTML 页面)时,换行符才有意义。并且 \n
无法正确呈现为新行或换行符,您必须使用 <br>
或 HTML template + some CSS styling to preserve line breaks.
FastAPI returns,默认情况下,JSONResponse
type.
Takes some data and returns an
application/json
encoded response.This is the default response used in FastAPI, as you read above.
但是你可以告诉它 use an HTMLResponse
type using the response_class
parameter:
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
app = FastAPI()
@app.get('/status', response_class=HTMLResponse)
def get_func():
output = 'this output should <br> have a line break'
return output
或者,为了更好地控制,使用实际的 HTML 模板。 FastAPI 支持 Jinja2 模板,请参阅 FastAPI Templates.
部分proj/templates/output.html
<html>
<head>
</head>
<body>
<p>This output should have a <br>line break.</p>
<p>Other stuff: {{ stuff }}</p>
</body>
</html>
proj/main.py
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
app = FastAPI()
templates = Jinja2Templates(directory="templates")
@app.get('/status', response_class=HTMLResponse)
def get_func(request: Request):
return templates.TemplateResponse("output.html", {"request": request, "stuff": 123})
有了 HTML 模板,您就可以使用 CSS styling to preserve line breaks.
使用response_class=PlainTextResponse
from fastapi.responses import PlainTextResponse
@app_fastapi.get("/get_log", response_class=PlainTextResponse)
async def get_log():
return "hello\nbye\n"