mirror of
https://github.com/fastapi/fastapi.git
synced 2026-05-24 16:29:41 -04:00
📝 Update source examples and docs from Python 3.9 to 3.10 (#14900)
This commit is contained in:
committed by
GitHub
parent
d06ab3f5c7
commit
c9e2277d8b
9
docs_src/custom_response/tutorial001_py310.py
Normal file
9
docs_src/custom_response/tutorial001_py310.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi.responses import UJSONResponse
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/", response_class=UJSONResponse)
|
||||
async def read_items():
|
||||
return [{"item_id": "Foo"}]
|
||||
9
docs_src/custom_response/tutorial001b_py310.py
Normal file
9
docs_src/custom_response/tutorial001b_py310.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi.responses import ORJSONResponse
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/", response_class=ORJSONResponse)
|
||||
async def read_items():
|
||||
return ORJSONResponse([{"item_id": "Foo"}])
|
||||
18
docs_src/custom_response/tutorial002_py310.py
Normal file
18
docs_src/custom_response/tutorial002_py310.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi.responses import HTMLResponse
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/", response_class=HTMLResponse)
|
||||
async def read_items():
|
||||
return """
|
||||
<html>
|
||||
<head>
|
||||
<title>Some HTML in here</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Look ma! HTML!</h1>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
19
docs_src/custom_response/tutorial003_py310.py
Normal file
19
docs_src/custom_response/tutorial003_py310.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi.responses import HTMLResponse
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items():
|
||||
html_content = """
|
||||
<html>
|
||||
<head>
|
||||
<title>Some HTML in here</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Look ma! HTML!</h1>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
return HTMLResponse(content=html_content, status_code=200)
|
||||
23
docs_src/custom_response/tutorial004_py310.py
Normal file
23
docs_src/custom_response/tutorial004_py310.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi.responses import HTMLResponse
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
def generate_html_response():
|
||||
html_content = """
|
||||
<html>
|
||||
<head>
|
||||
<title>Some HTML in here</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Look ma! HTML!</h1>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
return HTMLResponse(content=html_content, status_code=200)
|
||||
|
||||
|
||||
@app.get("/items/", response_class=HTMLResponse)
|
||||
async def read_items():
|
||||
return generate_html_response()
|
||||
9
docs_src/custom_response/tutorial005_py310.py
Normal file
9
docs_src/custom_response/tutorial005_py310.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi.responses import PlainTextResponse
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/", response_class=PlainTextResponse)
|
||||
async def main():
|
||||
return "Hello World"
|
||||
9
docs_src/custom_response/tutorial006_py310.py
Normal file
9
docs_src/custom_response/tutorial006_py310.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi.responses import RedirectResponse
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/typer")
|
||||
async def redirect_typer():
|
||||
return RedirectResponse("https://typer.tiangolo.com")
|
||||
9
docs_src/custom_response/tutorial006b_py310.py
Normal file
9
docs_src/custom_response/tutorial006b_py310.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi.responses import RedirectResponse
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/fastapi", response_class=RedirectResponse)
|
||||
async def redirect_fastapi():
|
||||
return "https://fastapi.tiangolo.com"
|
||||
9
docs_src/custom_response/tutorial006c_py310.py
Normal file
9
docs_src/custom_response/tutorial006c_py310.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi.responses import RedirectResponse
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/pydantic", response_class=RedirectResponse, status_code=302)
|
||||
async def redirect_pydantic():
|
||||
return "https://docs.pydantic.dev/"
|
||||
14
docs_src/custom_response/tutorial007_py310.py
Normal file
14
docs_src/custom_response/tutorial007_py310.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi.responses import StreamingResponse
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
async def fake_video_streamer():
|
||||
for i in range(10):
|
||||
yield b"some fake video bytes"
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def main():
|
||||
return StreamingResponse(fake_video_streamer())
|
||||
14
docs_src/custom_response/tutorial008_py310.py
Normal file
14
docs_src/custom_response/tutorial008_py310.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi.responses import StreamingResponse
|
||||
|
||||
some_file_path = "large-video-file.mp4"
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/")
|
||||
def main():
|
||||
def iterfile(): # (1)
|
||||
with open(some_file_path, mode="rb") as file_like: # (2)
|
||||
yield from file_like # (3)
|
||||
|
||||
return StreamingResponse(iterfile(), media_type="video/mp4")
|
||||
10
docs_src/custom_response/tutorial009_py310.py
Normal file
10
docs_src/custom_response/tutorial009_py310.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi.responses import FileResponse
|
||||
|
||||
some_file_path = "large-video-file.mp4"
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def main():
|
||||
return FileResponse(some_file_path)
|
||||
10
docs_src/custom_response/tutorial009b_py310.py
Normal file
10
docs_src/custom_response/tutorial009b_py310.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi.responses import FileResponse
|
||||
|
||||
some_file_path = "large-video-file.mp4"
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/", response_class=FileResponse)
|
||||
async def main():
|
||||
return some_file_path
|
||||
19
docs_src/custom_response/tutorial009c_py310.py
Normal file
19
docs_src/custom_response/tutorial009c_py310.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from typing import Any
|
||||
|
||||
import orjson
|
||||
from fastapi import FastAPI, Response
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
class CustomORJSONResponse(Response):
|
||||
media_type = "application/json"
|
||||
|
||||
def render(self, content: Any) -> bytes:
|
||||
assert orjson is not None, "orjson must be installed"
|
||||
return orjson.dumps(content, option=orjson.OPT_INDENT_2)
|
||||
|
||||
|
||||
@app.get("/", response_class=CustomORJSONResponse)
|
||||
async def main():
|
||||
return {"message": "Hello World"}
|
||||
9
docs_src/custom_response/tutorial010_py310.py
Normal file
9
docs_src/custom_response/tutorial010_py310.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi.responses import ORJSONResponse
|
||||
|
||||
app = FastAPI(default_response_class=ORJSONResponse)
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items():
|
||||
return [{"item_id": "Foo"}]
|
||||
Reference in New Issue
Block a user