FastAPI教程翻译-⽤户指南2-第⼀步FastAPI 教程翻译 - ⽤户指南 2 - 第⼀步
乌子FastAPI Tutorial - Ur Guide - First Steps
The simplest FastAPI file could look like this:
最简单的 FastAPI ⽂件可能如下所⽰:
from fastapi import FastAPI
app = FastAPI()
@("/")
async def root():
return{"message":"Hello World"}
Copy that to a file main.py.
将其复制到⽂件 main.py 中。
Run the live rver:
运⾏实时服务器:
uvicorn main:app --reload
Note
注意
The command uvicorn main:app refers to:
命令 uvicorn main:app 指的是:
main: the file main.py (the Python “module”).
main:⽂件 main.py(Python 的『模块』)。
app: the object created inside of main.py with the line app = FastAPI().
优美的歌词>协作是什么意思app:在 main.py 内部创建的对象,其中⼀⾏ app = FastAPI()。
-
-reload: make the rver restart after code changes. Only u for development.
--reload:使服务器在代码更改后重新启动,仅⽤于开发。
You will e an output like:
您将看到如下的输出:
INFO: Started reloader process [17961]长干行其一
INFO: Started rver process [17962]
INFO: Waiting for application startup.
INFO: Uvicorn running on 127.0.0.1:8000 (Press CTRL+C to quit)
That last line shows the URL where your app is being rved, in your local machine.
最后⼀⾏显⽰在本地计算机中为您的应⽤提供服务的 URL。
Check it
检查
You will e the JSON respon as:
您将看到如下的 JSON 响应:
{"message":"Hello World"}
Interactive API docs
交互式 API ⽂档
You will e the automatic interactive API documentation (provided by ):您将看到⾃动交互式 API ⽂档(由 提供):
Alternative API docs
备⽤ API ⽂档
You will e the alternative automatic documentation (provided by ):
您将看到备⽤的⾃动交互式⽂档(由 提供):
OpenAPI
FastAPI generates a “schema” with all your API using the OpenAPI standard for defining APIs. FastAPI 使⽤⽤于定义API的 OpenAPI 标准为您的所有 API ⽣成『模式』。
喝啤酒会长胖吗“Schema”
『模式』
A “schema” is a definition or description of something. Not the code that implements it, but just the abstract description.
『模式』是事物的定义或描述。不是实现它的代码,⽽是抽象描述。
API “schema”
API『模式』
In this ca, OpenAPI is a specification that dictates how to define a schema of your API.
在这种情况下,OpenAPI 是规定如何定义 API 模式的规范。
This OpenAPI schema would include your API paths, the possible parameters they take, etc.
此 OpenAPI 模式将包括您的 API 路径,以及路径中包含的可能参数等。
Data “schema”
数据『模式』
The term “schema” might also refer to the shape of some data, like a JSON content.
术语『模式』也可能表⽰某些数据的形状,例如 JSON 内容。
In that ca, it would mean the JSON attributes, and data types they have, etc.
在这种情况下,这将意味着 JSON 属性及其具有的数据类型,等等。
OpenAPI and JSON Schema
OpenAPI 和 JSON 模式
OpenAPI defines an API schema for your API. And that schema includes definitions (or “schemas”) o
f the data nt and received by your API using JSON Schema, the standard for JSON data schemas.
OpenAPI:为您的 API 定义了⼀个 API 模式。并且这个模式包含了 API 传输数据的定义和 API 接收数据的定义。
JSON模式:JSON 数据模式的标准。
Check the openapi.json石榴石功效
检查openapi.json
If you are curious about how the raw OpenAPI schema looks like, it is just an automatically generated JSON with the descriptions of all your API.
原始 OpenAPI 模式只是⼀个⾃动⽣成的 JSON,其中包含所有 API 的描述。
It will show a JSON starting with something like:
它将会显⽰如下的 JSON:
{
"openapi":"3.0.2",
"info":{
"title":"FastAPI",
"version":"0.1.0"
},
"paths":{
"/items/":{
"get":{
"respons":{
"200":{
旅游业发展现状
"description":"Successful Respon",
"content":{
"application/json":{
...
What is OpenAPI for
什么是 OpenAPI
This OpenAPI schema is what powers the 2 interactive documentation systems included.
此 OpenAPI 架构是为所包含的2个交互式⽂档提供⽀持的。
And there are dozens of alternatives, all bad on OpenAPI. You could easily add any of tho alternatives to your application built with FastAPI.上大人孔乙己
并且有数⼗种替代⽅案,全部基于 OpenAPI。您可以轻松地将这些替代⽅案中的任何⼀种添加到使⽤ FastAPI 构建的应⽤程序中。
You could also u it to generate code automatically, for clients that communicate with your API. For example, frontend, mobile or IoT applications.
您还可以使⽤它为与 API 通信的客户端⾃动⽣成代码。例如:前端,移动或物联⽹应⽤程序。
Recap, step by step
回顾,逐步的
Step 1: import FastAPI
步骤1:导⼊FastAPI
from fastapi import FastAPI
app = FastAPI()
@("/")
async def root():
return{"message":"Hello World"}
FastAPI is a Python class that provides all the functionality for your API.
FastAPI 是⼀个 Python 类,可为您的 API 提供所有功能。