Browse Source

🌐 Update translations for zh (update-outdated)

pull/15898/head
github-actions[bot] 3 weeks ago
parent
commit
4d70c72388
  1. 488
      docs/zh/docs/_llm-test.md
  2. 11
      docs/zh/docs/alternatives.md
  3. 14
      docs/zh/docs/async.md
  4. 2
      docs/zh/docs/editor-support.md
  5. 28
      docs/zh/docs/features.md
  6. 32
      docs/zh/docs/index.md
  7. 44
      docs/zh/docs/python-types.md
  8. 2
      docs/zh/docs/virtual-environments.md

488
docs/zh/docs/_llm-test.md

@ -37,7 +37,7 @@
昨天,我的朋友写道:"如果你把 incorrectly 拼对了,你就把它拼错了"。我回答:"没错,但 'incorrectly' 错的不是 '"incorrectly"'"。 昨天,我的朋友写道:"如果你把 incorrectly 拼对了,你就把它拼错了"。我回答:"没错,但 'incorrectly' 错的不是 '"incorrectly"'"。
/// note /// note | 注意
LLM 很可能会把这段翻错。我们只关心在重新翻译时它是否能保持修正后的译文。 LLM 很可能会把这段翻错。我们只关心在重新翻译时它是否能保持修正后的译文。
@ -124,24 +124,24 @@ works(foo="bar") # 这可行 🎉
//// tab | 测试 //// tab | 测试
/// note /// note | 注意
Some text 一些文本
/// ///
/// note | 技术细节 /// note | 技术细节
Some text 一些文本
/// ///
/// tip /// tip | 提示
Some text 一些文本
/// ///
/// warning /// warning | 警告
Some text 一些文本
/// ///
/// danger /// danger | 危险
Some text 一些文本
/// ///
//// ////
@ -213,7 +213,7 @@ Some text
## HTML "dfn" 元素 { #html-dfn-elements } ## HTML "dfn" 元素 { #html-dfn-elements }
* <dfn title="配置为以某种方式连接并协同工作的机器组">集群</dfn> * <dfn title="配置为以某种方式连接并协同工作的一组机器。">集群</dfn>
* <dfn title="一种使用具有多个隐藏层的人工神经网络的机器学习方法,从输入层到输出层构建了完整的内部结构">深度学习</dfn> * <dfn title="一种使用具有多个隐藏层的人工神经网络的机器学习方法,从输入层到输出层构建了完整的内部结构">深度学习</dfn>
## 标题 { #headings } ## 标题 { #headings }
@ -222,15 +222,15 @@ Some text
### 开发 Web 应用——教程 { #develop-a-webapp-a-tutorial } ### 开发 Web 应用——教程 { #develop-a-webapp-a-tutorial }
Hello. 你好。
### 类型提示与注解 { #type-hints-and-annotations } ### 类型提示与注解 { #type-hints-and-annotations }
Hello again. 再次你好。
### 超类与子类 { #super-and-subclasses } ### 超类与子类 { #super-and-subclasses }
Hello again. 再次你好。
//// ////
@ -248,241 +248,241 @@ Hello again.
//// tab | 测试 //// tab | 测试
* you *
* your * 你的
* e.g. * 例如
* etc. *
* `foo` as an `int` * 作为 `int``foo`
* `bar` as a `str` * 作为 `str``bar`
* `baz` as a `list` * 作为 `list``baz`
* the Tutorial - User guide * 教程 - 用户指南
* the Advanced User Guide * 高级用户指南
* the SQLModel docs * SQLModel 文档
* the API docs * API 文档
* the automatic docs * 自动文档
* Data Science * 数据科学
* Deep Learning * 深度学习
* Machine Learning * 机器学习
* Dependency Injection * 依赖注入
* HTTP Basic authentication * HTTP Basic 认证
* HTTP Digest * HTTP Digest
* ISO format * ISO 格式
* the JSON Schema standard * JSON Schema 标准
* the JSON schema * JSON schema
* the schema definition * schema 定义
* Password Flow * 密码流
* Mobile * 移动端
* deprecated * 已弃用
* designed * 设计的
* invalid * 无效
* on the fly * 动态地
* standard * 标准
* default * 默认
* case-sensitive * 区分大小写
* case-insensitive * 不区分大小写
* to serve the application * 为应用提供服务
* to serve the page * 为页面提供服务
* the app * 应用
* the application * 应用程序
* the request * 请求
* the response * 响应
* the error response * 错误响应
* the path operation * 路径操作
* the path operation decorator * 路径操作装饰器
* the path operation function * 路径操作函数
* the body * 请求体
* the request body * 请求体
* the response body * 响应体
* the JSON body * JSON 请求体
* the form body * 表单体
* the file body * 文件体
* the function body * 函数体
* the parameter * 参数
* the body parameter * 请求体参数
* the path parameter * 路径参数
* the query parameter * 查询参数
* the cookie parameter * Cookie 参数
* the header parameter * Header 参数
* the form parameter * 表单参数
* the function parameter * 函数参数
* the event * 事件
* the startup event * 启动事件
* the startup of the server * 服务器启动
* the shutdown event * 关闭事件
* the lifespan event * lifespan 事件
* the handler * 处理器
* the event handler * 事件处理器
* the exception handler * 异常处理器
* to handle * 处理
* the model * 模型
* the Pydantic model * Pydantic 模型
* the data model * 数据模型
* the database model * 数据库模型
* the form model * 表单模型
* the model object * 模型对象
* the class *
* the base class * 基类
* the parent class * 父类
* the subclass * 子类
* the child class * 子类
* the sibling class * 兄弟类
* the class method * 类方法
* the header * Header
* the headers * Headers
* the authorization header * 授权 Header
* the `Authorization` header * `Authorization` header
* the forwarded header * 转发 Header
* the dependency injection system * 依赖注入系统
* the dependency * 依赖项
* the dependable * 可依赖项
* the dependant * 依赖方
* I/O bound * I/O 密集型
* CPU bound * CPU 密集型
* concurrency * 并发
* parallelism * 并行
* multiprocessing * 多进程
* the env var * 环境变量
* the environment variable * 环境变量
* the `PATH` * `PATH`
* the `PATH` variable * `PATH` 变量
* the authentication * 认证
* the authentication provider * 认证提供方
* the authorization * 授权
* the authorization form * 授权表单
* the authorization provider * 授权提供方
* the user authenticates * 用户进行认证
* the system authenticates the user * 系统对用户进行认证
* the CLI * CLI
* the command line interface * 命令行界面
* the server * 服务器
* the client * 客户端
* the cloud provider * 云服务提供商
* the cloud service * 云服务
* the development * 开发
* the development stages * 开发阶段
* the dict * dict
* the dictionary * 字典
* the enumeration * 枚举
* the enum * 枚举
* the enum member * 枚举成员
* the encoder * 编码器
* the decoder * 解码器
* to encode * 编码
* to decode * 解码
* the exception * 异常
* to raise * 抛出
* the expression * 表达式
* the statement * 语句
* the frontend * 前端
* the backend * 后端
* the GitHub discussion * GitHub 讨论
* the GitHub issue * GitHub issue
* the performance * 性能
* the performance optimization * 性能优化
* the return type * 返回类型
* the return value * 返回值
* the security * 安全
* the security scheme * 安全方案
* the task * 任务
* the background task * 后台任务
* the task function * 任务函数
* the template * 模板
* the template engine * 模板引擎
* the type annotation * 类型注解
* the type hint * 类型提示
* the server worker * 服务器 worker
* the Uvicorn worker * Uvicorn worker
* the Gunicorn Worker * Gunicorn Worker
* the worker process * worker 进程
* the worker class * worker 类
* the workload * 工作负载
* the deployment * 部署
* to deploy * 部署
* the SDK * SDK
* the software development kit * 软件开发工具包
* the `APIRouter` * `APIRouter`
* the `requirements.txt` * `requirements.txt`
* the Bearer Token * Bearer Token
* the breaking change * 破坏性变更
* the bug * bug
* the button * 按钮
* the callable * 可调用对象
* the code * 代码
* the commit * 提交
* the context manager * 上下文管理器
* the coroutine * 协程
* the database session * 数据库会话
* the disk * 磁盘
* the domain * 域名
* the engine * 引擎
* the fake X * 虚假 X
* the HTTP GET method * HTTP GET 方法
* the item *
* the library *
* the lifespan * 生命周期
* the lock *
* the middleware * 中间件
* the mobile application * 移动应用
* the module * 模块
* the mounting * 挂载
* the network * 网络
* the origin *
* the override * 覆盖
* the payload * 载荷
* the processor * 处理器
* the property * 属性
* the proxy * 代理
* the pull request * Pull Request
* the query * 查询
* the RAM * RAM
* the remote machine * 远程机器
* the status code * 状态码
* the string * 字符串
* the tag * 标签
* the web framework * Web 框架
* the wildcard * 通配符
* to return * 返回
* to validate * 校验
//// ////

11
docs/zh/docs/alternatives.md

@ -28,7 +28,7 @@ Django REST framework 作为一个灵活工具箱而创建,用于在底层使
它被包括 Mozilla、Red Hat、Eventbrite 在内的许多公司使用。 它被包括 Mozilla、Red Hat、Eventbrite 在内的许多公司使用。
它是最早的“自动 API 文档”的范例之一,这正是启发“寻找” **FastAPI** 的最初想法之一。 它是最早的**自动 API 文档**的范例之一,这正是启发“寻找” **FastAPI** 的最初想法之一。
/// note | 注意 /// note | 注意
@ -58,8 +58,9 @@ Flask 是一个“微框架”,它不包含数据库集成,也没有像 Djan
/// tip | 启发 **FastAPI** /// tip | 启发 **FastAPI**
- 成为微框架,便于按需组合所需的工具与组件。 成为微框架。让按需组合所需的工具与组件变得容易。
- 提供简单易用的路由系统。
提供简单易用的路由系统。
/// ///
@ -87,7 +88,7 @@ Requests 设计非常简单直观,易于使用,且有合理的默认值。
response = requests.get("http://example.com/some/url") response = requests.get("http://example.com/some/url")
``` ```
对应地,FastAPI 的 API 路径操作可能看起来是这样的: 对应地,FastAPI 的 API *路径操作*可能看起来是这样的:
```Python hl_lines="1" ```Python hl_lines="1"
@app.get("/some/url") @app.get("/some/url")
@ -282,7 +283,7 @@ Flask-apispec 由与 Marshmallow 相同的开发者创建。
Falcon 是另一个高性能 Python 框架,它被设计为精简且可作为 Hug 等其他框架的基础。 Falcon 是另一个高性能 Python 框架,它被设计为精简且可作为 Hug 等其他框架的基础。
它设计为接收两个参数的函数:一个“request”和一个“response”。然后从 request 中“读取”,向 response 中“写入”。由于这种设计,无法用标准的 Python 类型提示将请求参数和请求体声明为函数形参。 它设计为接收两个参数的函数:一个“请求”和一个“响应”。然后从请求中“读取”,向响应中“写入”。由于这种设计,无法用标准的 Python 类型提示将请求参数和请求体声明为函数形参。
因此,数据校验、序列化与文档要么需要手写完成,无法自动化;要么需要在 Falcon 之上实现一个框架,例如 Hug。其他受 Falcon 设计启发、采用“一个 request 对象 + 一个 response 对象作为参数”的框架也有同样的区别。 因此,数据校验、序列化与文档要么需要手写完成,无法自动化;要么需要在 Falcon 之上实现一个框架,例如 Hug。其他受 Falcon 设计启发、采用“一个 request 对象 + 一个 response 对象作为参数”的框架也有同样的区别。

14
docs/zh/docs/async.md

@ -95,11 +95,11 @@ Python 的现代版本支持通过一种叫**“协程”**——使用 `async`
### 并发与汉堡 { #concurrency-and-burgers } ### 并发与汉堡 { #concurrency-and-burgers }
上述异步代码的思想有时也被称为“并发”,它不同于“并行”。 上述**异步**代码的思想有时也被称为**“并发”**,它不同于**“并行”**
并发和并行都与“不同的事情或多或少同时发生”有关。 **并发****并行**都与“不同的事情或多或少同时发生”有关。
但是并发和并行之间的细节是完全不同的。 但是*并发**并行*之间的细节是完全不同的。
要了解差异,请想象以下关于汉堡的故事: 要了解差异,请想象以下关于汉堡的故事:
@ -367,7 +367,7 @@ Starlette(和 **FastAPI**)是基于 [AnyIO](https://anyio.readthedocs.io/en/
特别是,你可以直接使用 [AnyIO](https://anyio.readthedocs.io/en/stable/) 来处理高级的并发用例,这些用例需要在自己的代码中使用更高级的模式。 特别是,你可以直接使用 [AnyIO](https://anyio.readthedocs.io/en/stable/) 来处理高级的并发用例,这些用例需要在自己的代码中使用更高级的模式。
即使你没有使用 **FastAPI**,你也可以使用 [AnyIO](https://anyio.readthedocs.io/en/stable/) 编写自己的异步程序,使其拥有较高的兼容性并获得一些好处(例如,结构化并发)。 即使你没有使用 FastAPI,你也可以使用 [AnyIO](https://anyio.readthedocs.io/en/stable/) 编写自己的异步程序,使其拥有较高的兼容性并获得一些好处(例如,结构化并发)。
我基于 AnyIO 新建了一个库,作为一个轻量级的封装层,用来优化类型注解,同时提供了更好的**自动补全**、**内联错误提示**等功能。这个库还附带了一个友好的入门指南和教程,能帮助你**理解**并编写**自己的异步代码**:[Asyncer](https://asyncer.tiangolo.com/)。如果你有**结合使用异步代码和常规**(阻塞/同步)代码的需求,这个库会特别有用。 我基于 AnyIO 新建了一个库,作为一个轻量级的封装层,用来优化类型注解,同时提供了更好的**自动补全**、**内联错误提示**等功能。这个库还附带了一个友好的入门指南和教程,能帮助你**理解**并编写**自己的异步代码**:[Asyncer](https://asyncer.tiangolo.com/)。如果你有**结合使用异步代码和常规**(阻塞/同步)代码的需求,这个库会特别有用。
@ -429,13 +429,13 @@ Starlette(和 **FastAPI**)是基于 [AnyIO](https://anyio.readthedocs.io/en/
你可以拥有多个相互依赖的依赖以及[子依赖](tutorial/dependencies/sub-dependencies.md)(作为函数的参数),它们中的一些可能是通过 `async def` 声明,也可能是通过 `def` 声明。它们仍然可以正常工作,这些通过 `def` 声明的函数将会在外部线程中调用(来自线程池),而不是“被等待”。 你可以拥有多个相互依赖的依赖以及[子依赖](tutorial/dependencies/sub-dependencies.md)(作为函数的参数),它们中的一些可能是通过 `async def` 声明,也可能是通过 `def` 声明。它们仍然可以正常工作,这些通过 `def` 声明的函数将会在外部线程中调用(来自线程池),而不是“被等待”。
### 其他函数 { #other-utility-functions } ### 其他工具函数 { #other-utility-functions }
你可直接调用通过 `def``async def` 创建的任何其他函数,FastAPI 不会影响你调用它们的方式。 你可直接调用通过 `def``async def` 创建的任何其他工具函数,FastAPI 不会影响你调用它们的方式。
这与 FastAPI 为你调用*路径操作函数*和依赖项的逻辑相反。 这与 FastAPI 为你调用*路径操作函数*和依赖项的逻辑相反。
如果你的函数是通过 `def` 声明的,它将被直接调用(在代码中编写的地方),而不会在线程池中;如果这个函数通过 `async def` 声明,当在代码中调用时,你就应该使用 `await` 等待函数的结果。 如果你的工具函数是通过 `def` 声明的,它将被直接调用(在代码中编写的地方),而不会在线程池中;如果这个函数通过 `async def` 声明,当在代码中调用时,你就应该使用 `await` 等待函数的结果。
--- ---

2
docs/zh/docs/editor-support.md

@ -14,7 +14,7 @@
## 功能 { #features } ## 功能 { #features }
- **Path Operation 资源管理器** - 侧边栏树状视图展示应用中的所有 <dfn title="路由,端点">*路径操作*</dfn>。点击可跳转至任一路由或 APIRouter 的定义。 - **Path Operation 资源管理器** - 侧边栏树状视图展示应用中的所有 <dfn title="路由,端点">*路径操作*</dfn>。点击可跳转至任一路由或 router 的定义。
- **路由搜索** - 使用 <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>E</kbd>(macOS 上为 <kbd>Cmd</kbd> + <kbd>Shift</kbd> + <kbd>E</kbd>)按路径、方法或名称进行搜索。 - **路由搜索** - 使用 <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>E</kbd>(macOS 上为 <kbd>Cmd</kbd> + <kbd>Shift</kbd> + <kbd>E</kbd>)按路径、方法或名称进行搜索。
- **CodeLens 导航** - 测试客户端调用(例如 `client.get('/items')`)上方的可点击链接,可跳转到匹配的*路径操作*,在测试与实现之间快速往返。 - **CodeLens 导航** - 测试客户端调用(例如 `client.get('/items')`)上方的可点击链接,可跳转到匹配的*路径操作*,在测试与实现之间快速往返。
- **部署到 FastAPI Cloud** - 一键将你的应用部署到 [FastAPI Cloud](https://fastapicloud.com/)。 - **部署到 FastAPI Cloud** - 一键将你的应用部署到 [FastAPI Cloud](https://fastapicloud.com/)。

28
docs/zh/docs/features.md

@ -19,11 +19,11 @@
![Swagger UI interaction](https://fastapi.tiangolo.com/img/index/index-03-swagger-02.png) ![Swagger UI interaction](https://fastapi.tiangolo.com/img/index/index-03-swagger-02.png)
* 另外的 API 文档:[**ReDoc**](https://github.com/Rebilly/ReDoc) * 另外的 API 文档:[**ReDoc**](https://github.com/Rebilly/ReDoc)
![ReDoc](https://fastapi.tiangolo.com/img/index/index-06-redoc-02.png) ![ReDoc](https://fastapi.tiangolo.com/img/index/index-06-redoc-02.png)
### 更主流的 Python { #just-modern-python } ### 就是现代 Python { #just-modern-python }
全部都基于标准的 **Python 类型** 声明(感谢 Pydantic)。没有新的语法需要学习。只需要标准的现代 Python。 全部都基于标准的 **Python 类型** 声明(感谢 Pydantic)。没有新的语法需要学习。只需要标准的现代 Python。
@ -98,7 +98,7 @@ my_second_user: User = User(**second_user_data)
### 简洁 { #short } ### 简洁 { #short }
任何类型都有合理的**默认值**,任何地方都有可选配置。所有的参数被微调,来满足你的需求,定义成你需要的 API。 任何类型都有合理的**默认值**,任何地方都有可选配置。所有的参数被微调,来满足你的需求,定义成你需要的 API。
但是默认情况下,一切都能**“顺利工作”**。 但是默认情况下,一切都能**“顺利工作”**。
@ -110,7 +110,7 @@ my_second_user: User = User(**second_user_data)
* 字符串 (`str`) 字段,定义最小或最大长度。 * 字符串 (`str`) 字段,定义最小或最大长度。
* 数字 (`int`, `float`) 有最大值和最小值,等等。 * 数字 (`int`, `float`) 有最大值和最小值,等等。
* 校验外来类型,比如: * 校验更特殊的类型,比如:
* URL。 * URL。
* Email。 * Email。
* UUID。 * UUID。
@ -120,9 +120,9 @@ my_second_user: User = User(**second_user_data)
### 安全性及身份验证 { #security-and-authentication } ### 安全性及身份验证 { #security-and-authentication }
集成了安全性和身份认证。杜绝数据库或者数据模型的渗透风险 集成了安全性和身份验证。不需要在数据库或数据模型上作出任何妥协
OpenAPI 中定义的安全模式,包括: OpenAPI 中定义的所有安全模式,包括:
* HTTP 基本认证。 * HTTP 基本认证。
* **OAuth2**(也使用 **JWT tokens**)。在 [使用 JWT 的 OAuth2](tutorial/security/oauth2-jwt.md) 查看教程。 * **OAuth2**(也使用 **JWT tokens**)。在 [使用 JWT 的 OAuth2](tutorial/security/oauth2-jwt.md) 查看教程。
@ -131,7 +131,7 @@ OpenAPI 中定义的安全模式,包括:
* 查询参数。 * 查询参数。
* Cookies,等等。 * Cookies,等等。
加上来自 Starlette(包括 **session cookie**)的所有安全特性。 加上来自 Starlette(包括 **session cookies**)的所有安全特性。
所有的这些都是可复用的工具和组件,可以轻松与你的系统,数据仓库,关系型以及 NoSQL 数据库等等集成。 所有的这些都是可复用的工具和组件,可以轻松与你的系统,数据仓库,关系型以及 NoSQL 数据库等等集成。
@ -142,7 +142,7 @@ FastAPI 有一个使用非常简单,但是非常强大的<dfn title='也称为
* 甚至依赖也可以有依赖,创建一个层级或者**“图”依赖**。 * 甚至依赖也可以有依赖,创建一个层级或者**“图”依赖**。
* 所有**自动化处理**都由框架完成。 * 所有**自动化处理**都由框架完成。
* 所有的依赖关系都可以从请求中获取数据,并且**增加了路径操作**约束和自动文档生成。 * 所有的依赖关系都可以从请求中获取数据,并且**增加了路径操作**约束和自动文档生成。
* 即使是在依赖项中定义的路径操作参数也会**自动验证**。 * 即使是在依赖项中定义的*路径操作*参数也会**自动验证**。
* 支持复杂的用户身份认证系统,**数据库连接**等等。 * 支持复杂的用户身份认证系统,**数据库连接**等等。
* **不依赖**数据库,前端等。但是和它们集成很简单。 * **不依赖**数据库,前端等。但是和它们集成很简单。
@ -155,12 +155,14 @@ FastAPI 有一个使用非常简单,但是非常强大的<dfn title='也称为
### 测试 { #tested } ### 测试 { #tested }
* 100% <dfn title="自动测试的代码量">测试覆盖</dfn> * 100% <dfn title="自动测试的代码量">测试覆盖</dfn>
* 代码库100% <dfn title="Python 类型注解,有了这个你的编辑器和外部工具可以给你更好的支持">类型注</dfn> * 代码库100% <dfn title="Python 类型注解,有了这个你的编辑器和外部工具可以给你更好的支持">类型</dfn>
* 用于生产应用。 * 用于生产应用。
## Starlette 特性 { #starlette-features } ## Starlette 特性 { #starlette-features }
**FastAPI** 和 [**Starlette**](https://www.starlette.dev/) 完全兼容(并基于)。所以,你有的其他的 Starlette 代码也能正常工作。`FastAPI` 实际上是 `Starlette` 的一个子类。所以,如果你已经知道或者使用 Starlette,大部分的功能会以相同的方式工作。 **FastAPI** 与 [**Starlette**](https://www.starlette.dev/) 完全兼容(并基于它构建)。所以,你有的其他的 Starlette 代码也能正常工作。
`FastAPI` 实际上是 `Starlette` 的一个子类。所以,如果你已经知道或者使用 Starlette,大部分的功能会以相同的方式工作。
通过 **FastAPI** 你可以获得所有 **Starlette** 的特性(FastAPI 就像加强版的 Starlette): 通过 **FastAPI** 你可以获得所有 **Starlette** 的特性(FastAPI 就像加强版的 Starlette):
@ -172,11 +174,11 @@ FastAPI 有一个使用非常简单,但是非常强大的<dfn title='也称为
* **CORS**、GZip、静态文件、流响应。 * **CORS**、GZip、静态文件、流响应。
* 支持 **Session 和 Cookie** * 支持 **Session 和 Cookie**
* 100% 测试覆盖率。 * 100% 测试覆盖率。
* 代码库 100% 类型注 * 代码库 100% 类型注。
## Pydantic 特性 { #pydantic-features } ## Pydantic 特性 { #pydantic-features }
**FastAPI** [**Pydantic**](https://docs.pydantic.dev/) 完全兼容(并基于)。所以,你有的其他的 Pydantic 代码也能正常工作。 **FastAPI** [**Pydantic**](https://docs.pydantic.dev/) 完全兼容(并基于它构建)。所以,你有的其他的 Pydantic 代码也能正常工作。
兼容包括基于 Pydantic 的外部库,例如用于数据库的 <abbr title="Object-Relational Mapper - 对象关系映射">ORM</abbr>s、<abbr title="Object-Document Mapper - 对象文档映射">ODM</abbr>s。 兼容包括基于 Pydantic 的外部库,例如用于数据库的 <abbr title="Object-Relational Mapper - 对象关系映射">ORM</abbr>s、<abbr title="Object-Document Mapper - 对象文档映射">ODM</abbr>s。
@ -188,7 +190,7 @@ FastAPI 有一个使用非常简单,但是非常强大的<dfn title='也称为
* **不烧脑** * **不烧脑**
* 没有新的模式定义 micro-language 需要学习。 * 没有新的模式定义 micro-language 需要学习。
* 如果你知道 Python types,你就知道如何使用 Pydantic。 * 如果你知道 Python 类型,你就知道如何使用 Pydantic。
* 和你 **<abbr title="Integrated Development Environment - 集成开发环境: 类似于代码编辑器">IDE</abbr>/<dfn title="一个检查代码错误的程序">linter</dfn>/brain** 适配: * 和你 **<abbr title="Integrated Development Environment - 集成开发环境: 类似于代码编辑器">IDE</abbr>/<dfn title="一个检查代码错误的程序">linter</dfn>/brain** 适配:
* 因为 pydantic 数据结构仅仅是你定义的类的实例;自动补全,linting,mypy 以及你的直觉应该可以和你验证的数据一起正常工作。 * 因为 pydantic 数据结构仅仅是你定义的类的实例;自动补全,linting,mypy 以及你的直觉应该可以和你验证的数据一起正常工作。
* 验证**复杂结构**: * 验证**复杂结构**:

32
docs/zh/docs/index.md

@ -106,19 +106,19 @@ FastAPI 是一个用于构建 API 的现代、快速(高性能)的 Web 框
<div class="fastapi-opinions__panel" id="fo-panel-microsoft" role="tabpanel" aria-labelledby="fo-tab-microsoft" tabindex="0"> <div class="fastapi-opinions__panel" id="fo-panel-microsoft" role="tabpanel" aria-labelledby="fo-tab-microsoft" tabindex="0">
<blockquote class="fastapi-opinions__quote">“我最近大量使用 <strong>FastAPI</strong>。我实际上计划把它用于我团队在 <strong>微软的机器学习(ML)服务</strong>。其中一些正在集成进核心 <strong>Windows</strong> 产品以及一些 <strong>Office</strong> 产品。”</blockquote> <blockquote class="fastapi-opinions__quote">“我最近大量使用 <strong>FastAPI</strong>。我实际上计划把它用于我团队在 <strong>微软的机器学习(ML)服务</strong>。其中一些正在集成进核心 <strong>Windows</strong> 产品以及一些 <strong>Office</strong> 产品。”</blockquote>
<div class="fastapi-opinions__attr">— Kabir Khan,<strong>Microsoft</strong> <a href="https://github.com/fastapi/fastapi/pull/26">(ref)</a></div> <div class="fastapi-opinions__attr">— Kabir Khan,<strong>Microsoft</strong> <a href="https://github.com/fastapi/fastapi/pull/26">(参考)</a></div>
</div> </div>
<div class="fastapi-opinions__panel" id="fo-panel-uber" role="tabpanel" aria-labelledby="fo-tab-uber" tabindex="0" hidden> <div class="fastapi-opinions__panel" id="fo-panel-uber" role="tabpanel" aria-labelledby="fo-tab-uber" tabindex="0" hidden>
<blockquote class="fastapi-opinions__quote">“我们采用了 <strong>FastAPI</strong> 库来启动一个可查询获取<strong>预测结果</strong><strong>REST</strong> 服务器。” <em>[用于 Ludwig]</em></blockquote> <blockquote class="fastapi-opinions__quote">“我们采用了 <strong>FastAPI</strong> 库来启动一个可查询获取<strong>预测结果</strong><strong>REST</strong> 服务器。” <em>[用于 Ludwig]</em></blockquote>
<div class="fastapi-opinions__attr">— Piero Molino,Yaroslav Dudin,Sai Sumanth Miryala,<strong>Uber</strong> <a href="https://eng.uber.com/ludwig-v0-2/">(ref)</a></div> <div class="fastapi-opinions__attr">— Piero Molino,Yaroslav Dudin,Sai Sumanth Miryala,<strong>Uber</strong> <a href="https://eng.uber.com/ludwig-v0-2/">(参考)</a></div>
</div> </div>
<div class="fastapi-opinions__panel" id="fo-panel-netflix" role="tabpanel" aria-labelledby="fo-tab-netflix" tabindex="0" hidden> <div class="fastapi-opinions__panel" id="fo-panel-netflix" role="tabpanel" aria-labelledby="fo-tab-netflix" tabindex="0" hidden>
<blockquote class="fastapi-opinions__quote"><strong>Netflix</strong> 很高兴宣布开源我们的<strong>危机管理</strong>编排框架:<strong>Dispatch</strong>!” <em>[使用 FastAPI 构建]</em></blockquote> <blockquote class="fastapi-opinions__quote"><strong>Netflix</strong> 很高兴宣布开源我们的<strong>危机管理</strong>编排框架:<strong>Dispatch</strong>!” <em>[使用 FastAPI 构建]</em></blockquote>
<div class="fastapi-opinions__attr">— Kevin Glisson,Marc Vilanova,Forest Monsen,<strong>Netflix</strong> <a href="https://netflixtechblog.com/introducing-dispatch-da4b8a2a8072">(ref)</a></div> <div class="fastapi-opinions__attr">— Kevin Glisson,Marc Vilanova,Forest Monsen,<strong>Netflix</strong> <a href="https://netflixtechblog.com/introducing-dispatch-da4b8a2a8072">(参考)</a></div>
</div> </div>
<div class="fastapi-opinions__panel" id="fo-panel-cisco" role="tabpanel" aria-labelledby="fo-tab-cisco" tabindex="0" hidden> <div class="fastapi-opinions__panel" id="fo-panel-cisco" role="tabpanel" aria-labelledby="fo-tab-cisco" tabindex="0" hidden>
<blockquote class="fastapi-opinions__quote">“如果有人正在构建生产级的 Python API,我强烈推荐 <strong>FastAPI</strong>。它<strong>设计优雅</strong><strong>使用简单</strong><strong>高度可扩展</strong> —— 它已经成为我们 API 优先开发战略中的<strong>关键组件</strong>。”</blockquote> <blockquote class="fastapi-opinions__quote">“如果有人正在构建生产级的 Python API,我强烈推荐 <strong>FastAPI</strong>。它<strong>设计优雅</strong><strong>使用简单</strong><strong>高度可扩展</strong> —— 它已经成为我们 API 优先开发战略中的<strong>关键组件</strong>。”</blockquote>
<div class="fastapi-opinions__attr">— Deon Pillsbury,<strong>Cisco</strong> <a href="https://www.linkedin.com/posts/deonpillsbury_cisco-cx-python-activity-6963242628536487936-trAp/">(ref)</a></div> <div class="fastapi-opinions__attr">— Deon Pillsbury,<strong>Cisco</strong> <a href="https://www.linkedin.com/posts/deonpillsbury_cisco-cx-python-activity-6963242628536487936-trAp/">(参考)</a></div>
</div> </div>
</div> </div>
<!-- /only-mkdocs --> <!-- /only-mkdocs -->
@ -127,25 +127,25 @@ FastAPI 是一个用于构建 API 的现代、快速(高性能)的 Web 框
「_[...] 我最近大量使用 **FastAPI**。[...] 我实际上计划把它用于我团队在 **微软的机器学习(ML)服务**。其中一些正在集成进核心 **Windows** 产品以及一些 **Office** 产品。_」 「_[...] 我最近大量使用 **FastAPI**。[...] 我实际上计划把它用于我团队在 **微软的机器学习(ML)服务**。其中一些正在集成进核心 **Windows** 产品以及一些 **Office** 产品。_」
<div style="text-align: right; margin-right: 10%;">Kabir Khan - <strong>Microsoft</strong> <a href="https://github.com/fastapi/fastapi/pull/26"><small>(ref)</small></a></div> <div style="text-align: right; margin-right: 10%;">Kabir Khan - <strong>Microsoft</strong> <a href="https://github.com/fastapi/fastapi/pull/26"><small>(参考)</small></a></div>
--- ---
「_我们采用 **FastAPI** 库来启动一个可查询以获取**预测结果**的 **REST** 服务器。[用于 Ludwig]_」 「_我们采用 **FastAPI** 库来启动一个可查询以获取**预测结果**的 **REST** 服务器。[用于 Ludwig]_」
<div style="text-align: right; margin-right: 10%;">Piero Molino,Yaroslav Dudin,Sai Sumanth Miryala - <strong>Uber</strong> <a href="https://eng.uber.com/ludwig-v0-2/"><small>(ref)</small></a></div> <div style="text-align: right; margin-right: 10%;">Piero Molino,Yaroslav Dudin,Sai Sumanth Miryala - <strong>Uber</strong> <a href="https://eng.uber.com/ludwig-v0-2/"><small>(参考)</small></a></div>
--- ---
「_**Netflix** 很高兴宣布开源我们的**危机管理**编排框架:**Dispatch**![使用 **FastAPI** 构建]_」 「_**Netflix** 很高兴宣布开源我们的**危机管理**编排框架:**Dispatch**![使用 **FastAPI** 构建]_」
<div style="text-align: right; margin-right: 10%;">Kevin Glisson,Marc Vilanova,Forest Monsen - <strong>Netflix</strong> <a href="https://netflixtechblog.com/introducing-dispatch-da4b8a2a8072"><small>(ref)</small></a></div> <div style="text-align: right; margin-right: 10%;">Kevin Glisson,Marc Vilanova,Forest Monsen - <strong>Netflix</strong> <a href="https://netflixtechblog.com/introducing-dispatch-da4b8a2a8072"><small>(参考)</small></a></div>
--- ---
「_如果有人正在构建生产级的 Python API,我强烈推荐 **FastAPI**。它**设计优雅**、**使用简单**且**高度可扩展**,它已经成为我们 API 优先开发战略中的**关键组件**,并驱动了许多自动化和服务,比如我们的 Virtual TAC Engineer。_」 「_如果有人正在构建生产级的 Python API,我强烈推荐 **FastAPI**。它**设计优雅**、**使用简单**且**高度可扩展**,它已经成为我们 API 优先开发战略中的**关键组件**,并驱动了许多自动化和服务,比如我们的 Virtual TAC Engineer。_」
<div style="text-align: right; margin-right: 10%;">Deon Pillsbury - <strong>Cisco</strong> <a href="https://www.linkedin.com/posts/deonpillsbury_cisco-cx-python-activity-6963242628536487936-trAp/"><small>(ref)</small></a></div> <div style="text-align: right; margin-right: 10%;">Deon Pillsbury - <strong>Cisco</strong> <a href="https://www.linkedin.com/posts/deonpillsbury_cisco-cx-python-activity-6963242628536487936-trAp/"><small>(参考)</small></a></div>
--- ---
@ -192,7 +192,7 @@ $ pip install "fastapi[standard]"
</div> </div>
**Note**: 请确保把 `"fastapi[standard]"` 用引号包起来,以保证在所有终端中都能正常工作。 **注意**: 请确保把 `"fastapi[standard]"` 用引号包起来,以保证在所有终端中都能正常工作。
## 示例 { #example } ## 示例 { #example }
@ -237,7 +237,7 @@ async def read_item(item_id: int, q: str | None = None):
return {"item_id": item_id, "q": q} return {"item_id": item_id, "q": q}
``` ```
**Note**: **注意**:
如果你不确定,请查看文档中 _"In a hurry?"_ 章节的 [`async` 和 `await`](https://fastapi.tiangolo.com/zh/async/#in-a-hurry) 部分。 如果你不确定,请查看文档中 _"In a hurry?"_ 章节的 [`async` 和 `await`](https://fastapi.tiangolo.com/zh/async/#in-a-hurry) 部分。
@ -400,7 +400,7 @@ item_id: int
item: Item item: Item
``` ```
……通过一次声明,你将获得: ...通过一次声明,你将获得:
* 编辑器支持,包括: * 编辑器支持,包括:
* 自动补全。 * 自动补全。
@ -421,7 +421,7 @@ item: Item
* `datetime` 对象。 * `datetime` 对象。
* `UUID` 对象。 * `UUID` 对象。
* 数据库模型。 * 数据库模型。
* ……以及更多。 * ...以及更多。
* 自动生成的交互式 API 文档,包括两种可选的用户界面: * 自动生成的交互式 API 文档,包括两种可选的用户界面:
* Swagger UI。 * Swagger UI。
* ReDoc。 * ReDoc。
@ -457,19 +457,19 @@ item: Item
return {"item_name": item.name, "item_id": item_id} return {"item_name": item.name, "item_id": item_id}
``` ```
……从: ...从:
```Python ```Python
... "item_name": item.name ... ... "item_name": item.name ...
``` ```
……改为: ...改为:
```Python ```Python
... "item_price": item.price ... ... "item_price": item.price ...
``` ```
……看看你的编辑器如何自动补全属性并知道它们的类型: ...看看你的编辑器如何自动补全属性并知道它们的类型:
![editor support](https://fastapi.tiangolo.com/img/vscode-completion.png) ![editor support](https://fastapi.tiangolo.com/img/vscode-completion.png)
@ -488,7 +488,7 @@ item: Item
* 基于 HTTPX 和 `pytest` 的极其简单的测试 * 基于 HTTPX 和 `pytest` 的极其简单的测试
* **CORS** * **CORS**
* **Cookie Sessions** * **Cookie Sessions**
* ……以及更多。 * ...以及更多。
### 部署你的应用(可选) { #deploy-your-app-optional } ### 部署你的应用(可选) { #deploy-your-app-optional }

44
docs/zh/docs/python-types.md

@ -2,11 +2,11 @@
Python 支持可选的“类型提示”(也叫“类型注解”)。 Python 支持可选的“类型提示”(也叫“类型注解”)。
这些“类型提示”或注解是一种特殊语法,用来声明变量的<dfn title="例如:str、int、float、bool">类型</dfn> 这些 **“类型提示”** 或注解是一种特殊语法,用来声明变量的<dfn title="例如:str、int、float、bool">类型</dfn>
通过为变量声明类型,编辑器和工具可以为你提供更好的支持。 通过为变量声明类型,编辑器和工具可以为你提供更好的支持。
这只是一个关于 Python 类型提示的快速入门/复习。它只涵盖与 **FastAPI** 一起使用所需的最少部分...实际上非常少。 这只是一个关于 Python 类型提示的**快速入门/复习**。它只涵盖与 **FastAPI** 一起使用所需的最少部分...实际上非常少。
**FastAPI** 完全基于这些类型提示构建,它们带来了许多优势和好处。 **FastAPI** 完全基于这些类型提示构建,它们带来了许多优势和好处。
@ -44,7 +44,7 @@ John Doe
但现在想象你要从零开始写它。 但现在想象你要从零开始写它。
在某个时刻你开始定义函数,并且准备好了参数…… 在某个时刻你开始定义函数,并且准备好了参数...
接下来你需要调用“那个把首字母变大写的方法”。 接下来你需要调用“那个把首字母变大写的方法”。
@ -62,7 +62,7 @@ John Doe
我们来改前一个版本的一行代码。 我们来改前一个版本的一行代码。
把函数参数从: 我们会这个片段,也就是函数参数从:
```Python ```Python
first_name, last_name first_name, last_name
@ -151,7 +151,7 @@ def some_function(data: Any):
有些类型可以在方括号中接收“类型参数”(type parameters),用于声明其内部值的类型。比如“字符串列表”可以写为 `list[str]` 有些类型可以在方括号中接收“类型参数”(type parameters),用于声明其内部值的类型。比如“字符串列表”可以写为 `list[str]`
这些能接收类型参数的类型称为“泛型类型”(Generic types)或“泛型”(Generics)。 这些能接收类型参数的类型称为**泛型类型**(Generic types)或**泛型**(Generics)。
你可以把相同的内建类型作为泛型使用(带方括号和内部类型): 你可以把相同的内建类型作为泛型使用(带方括号和内部类型):
@ -221,7 +221,7 @@ def some_function(data: Any):
#### Union { #union } #### Union { #union }
你可以声明一个变量可以是若干种类型中的任意一种,比如既可以是 `int` 也可以是 `str` 你可以声明一个变量可以是**若干种类型**中的任意一种,比如既可以是 `int` 也可以是 `str`
定义时使用<dfn title='也叫“按位或运算符(bitwise or operator)”,但这里与该含义无关'>竖线(`|`)</dfn>把两种类型分开。 定义时使用<dfn title='也叫“按位或运算符(bitwise or operator)”,但这里与该含义无关'>竖线(`|`)</dfn>把两种类型分开。
@ -263,9 +263,9 @@ def some_function(data: Any):
<img src="/img/python-types/image06.png"> <img src="/img/python-types/image06.png">
注意,这表示“`one_person` 是类 `Person` 的一个实例(instance)”。 注意,这表示“`one_person` 是类 `Person` 的一个**实例**(instance)”。
它并不表示“`one_person` 是名为 `Person`类本身(class)”。 它并不表示“`one_person` 是名为 `Person`**类**(class)”。
## Pydantic 模型 { #pydantic-models } ## Pydantic 模型 { #pydantic-models }
@ -285,7 +285,7 @@ def some_function(data: Any):
/// note | 注意 /// note | 注意
想了解更多关于 [Pydantic](https://docs.pydantic.dev/) 的信息,请查看其文档 要了解更多关于 [Pydantic 的信息,请查看其文档](https://docs.pydantic.dev/)。
/// ///
@ -295,7 +295,7 @@ def some_function(data: Any):
## 带元数据注解的类型提示 { #type-hints-with-metadata-annotations } ## 带元数据注解的类型提示 { #type-hints-with-metadata-annotations }
Python 还提供了一个特性,可以使用 `Annotated` 在这些类型提示中放入额外的<dfn title="关于数据的数据,此处指关于类型的信息,例如描述。">元数据</dfn> Python 还提供了一个特性,可以使用 `Annotated` 在这些类型提示中放入**额外的<dfn title="关于数据的数据,此处指关于类型的信息,例如描述。">元数据</dfn>**
你可以从 `typing` 导入 `Annotated` 你可以从 `typing` 导入 `Annotated`
@ -305,15 +305,15 @@ Python 本身不会对这个 `Annotated` 做任何处理。对于编辑器和其
但你可以在 `Annotated` 中为 **FastAPI** 提供额外的元数据,来描述你希望应用如何行为。 但你可以在 `Annotated` 中为 **FastAPI** 提供额外的元数据,来描述你希望应用如何行为。
重要的是要记住:传给 `Annotated` 的第一个类型参数才是实际类型。其余的只是给其他工具用的元数据。 重要的是要记住:传给 `Annotated`**第一个*类型参数***才是**实际类型**。其余的只是给其他工具用的元数据。
现在你只需要知道 `Annotated` 的存在,并且它是标准 Python。😎 现在你只需要知道 `Annotated` 的存在,并且它是标准 Python。😎
稍后你会看到它有多么强大。 稍后你会看到它有多么**强大**
/// tip | 提示 /// tip | 提示
这是标准 Python,这意味着你仍然可以在编辑器里获得尽可能好的开发体验,并能和你用来分析、重构代码的工具良好协作等。✨ 这是**标准 Python**,这意味着你仍然可以在编辑器里获得**尽可能好的开发体验**,并能和你用来分析、重构代码的工具良好协作等。✨
同时你的代码也能与许多其他 Python 工具和库高度兼容。🚀 同时你的代码也能与许多其他 Python 工具和库高度兼容。🚀
@ -325,16 +325,16 @@ Python 本身不会对这个 `Annotated` 做任何处理。对于编辑器和其
**FastAPI** 中,用类型提示来声明参数,你将获得: **FastAPI** 中,用类型提示来声明参数,你将获得:
* 编辑器支持。 * **编辑器支持**
* 类型检查。 * **类型检查**
……并且 **FastAPI** 会使用相同的声明来: ...并且 **FastAPI** 会使用相同的声明来:
* 定义要求:从请求路径参数、查询参数、请求头、请求体、依赖等。 * **定义要求**:从请求路径参数、查询参数、请求头、请求体、依赖等。
* 转换数据:把请求中的数据转换为所需类型。 * **转换数据**:把请求中的数据转换为所需类型。
* 校验数据:对于每个请求: * **校验数据**:对于每个请求:
* 当数据无效时,自动生成错误信息返回给客户端。 * 当数据无效时,自动生成返回给客户端的**错误**
* 使用 OpenAPI 记录 API: * 使用 OpenAPI **记录** API:
* 然后用于自动生成交互式文档界面。 * 然后用于自动生成交互式文档界面。
这些听起来可能有点抽象。别担心。你会在[教程 - 用户指南](tutorial/index.md)中看到所有这些的实际效果。 这些听起来可能有点抽象。别担心。你会在[教程 - 用户指南](tutorial/index.md)中看到所有这些的实际效果。
@ -343,6 +343,6 @@ Python 本身不会对这个 `Annotated` 做任何处理。对于编辑器和其
/// note | 注意 /// note | 注意
如果你已经读完所有教程,又回来想进一步了解类型,一个不错的资源是 [`mypy` 的“速查表”](https://mypy.readthedocs.io/en/latest/cheat_sheet_py3.html)。 如果你已经读完整个教程,又回来想进一步了解类型,一个不错的资源是 [`mypy` 的“速查表”](https://mypy.readthedocs.io/en/latest/cheat_sheet_py3.html)。
/// ///

2
docs/zh/docs/virtual-environments.md

@ -861,4 +861,4 @@ I solemnly swear 🐺
如果你读过并理解了所有这些,现在**你对虚拟环境的了解比很多开发者都要多**。🤓 如果你读过并理解了所有这些,现在**你对虚拟环境的了解比很多开发者都要多**。🤓
在未来当你调看起来复杂的东西时,了解这些细节很可能会有用,你会知道**它是如何在底层工作的**。😎 在未来当你调看起来复杂的东西时,了解这些细节很可能会有用,你会知道**它是如何在底层工作的**。😎

Loading…
Cancel
Save