Browse Source

🌐 Update translations for zh-hant (update-outdated)

pull/15178/head
github-actions[bot] 4 months ago
committed by Yurii Motov
parent
commit
26a7f8a6f5
  1. 18
      docs/zh-hant/docs/_llm-test.md
  2. 60
      docs/zh-hant/docs/alternatives.md
  3. 18
      docs/zh-hant/docs/async.md
  4. 2
      docs/zh-hant/docs/benchmarks.md
  5. 10
      docs/zh-hant/docs/environment-variables.md
  6. 71
      docs/zh-hant/docs/fastapi-cli.md
  7. 26
      docs/zh-hant/docs/features.md
  8. 58
      docs/zh-hant/docs/help-fastapi.md
  9. 14
      docs/zh-hant/docs/history-design-future.md
  10. 122
      docs/zh-hant/docs/index.md
  11. 2
      docs/zh-hant/docs/project-generation.md
  12. 10
      docs/zh-hant/docs/python-types.md
  13. 47
      docs/zh-hant/docs/tutorial/bigger-applications.md
  14. 6
      docs/zh-hant/docs/tutorial/body-updates.md
  15. 8
      docs/zh-hant/docs/tutorial/cors.md
  16. 4
      docs/zh-hant/docs/tutorial/debugging.md
  17. 4
      docs/zh-hant/docs/tutorial/encoder.md
  18. 4
      docs/zh-hant/docs/tutorial/extra-data-types.md
  19. 6
      docs/zh-hant/docs/tutorial/extra-models.md
  20. 75
      docs/zh-hant/docs/tutorial/first-steps.md
  21. 2
      docs/zh-hant/docs/tutorial/handling-errors.md
  22. 22
      docs/zh-hant/docs/tutorial/index.md
  23. 10
      docs/zh-hant/docs/tutorial/middleware.md
  24. 16
      docs/zh-hant/docs/tutorial/path-params.md
  25. 6
      docs/zh-hant/docs/tutorial/query-params-str-validations.md
  26. 6
      docs/zh-hant/docs/tutorial/request-forms.md
  27. 9
      docs/zh-hant/docs/tutorial/response-model.md
  28. 6
      docs/zh-hant/docs/tutorial/response-status-code.md
  29. 28
      docs/zh-hant/docs/virtual-environments.md

18
docs/zh-hant/docs/_llm-test.md

@ -11,7 +11,7 @@
* 檢查翻譯是否正確。 * 檢查翻譯是否正確。
* 如有需要,改進你的語言特定提示、通用提示,或英文原文。 * 如有需要,改進你的語言特定提示、通用提示,或英文原文。
* 然後手動修正翻譯中剩下的問題,讓它成為一個好的譯文。 * 然後手動修正翻譯中剩下的問題,讓它成為一個好的譯文。
* 重新翻譯,並保留這份好的譯文。理想結果是 LLM 不再對該譯文做任何變更。這代表通用提示與你的語言特定提示已經盡可能完善(有時它仍可能做出幾個看似隨機的變更,原因是<a href="https://doublespeak.chat/#/handbook#deterministic-output" class="external-link" target="_blank">LLMs 並非決定性演算法</a>)。 * 重新翻譯,並保留這份好的譯文。理想結果是 LLM 不再對該譯文做任何變更。這代表通用提示與你的語言特定提示已經盡可能完善(有時它仍可能做出幾個看似隨機的變更,原因是[LLMs 並非決定性演算法](https://doublespeak.chat/#/handbook#deterministic-output))。
測試: 測試:
@ -95,7 +95,7 @@ $ <font color="#4E9A06">fastapi</font> run <u style="text-decoration-style:solid
...以及另一個主控台範例... ...以及另一個主控台範例...
```console ```console
// 建立目錄 "code" // 建立目錄 "Code"
$ mkdir code $ mkdir code
// 切換到該目錄 // 切換到該目錄
$ cd code $ cd code
@ -169,15 +169,15 @@ Some text
連結文字應被翻譯,連結位址應保持不變: 連結文字應被翻譯,連結位址應保持不變:
* [連結到上方標題](#code-snippets) * [連結到上方標題](#code-snippets)
* [內部連結](index.md#installation){.internal-link target=_blank} * [內部連結](index.md#installation)
* <a href="https://sqlmodel.tiangolo.com/" class="external-link" target="_blank">外部連結</a> * [外部連結](https://sqlmodel.tiangolo.com/)
* <a href="https://fastapi.tiangolo.com/css/styles.css" class="external-link" target="_blank">連結到樣式</a> * [連結到樣式](https://fastapi.tiangolo.com/css/styles.css)
* <a href="https://fastapi.tiangolo.com/js/logic.js" class="external-link" target="_blank">連結到腳本</a> * [連結到腳本](https://fastapi.tiangolo.com/js/logic.js)
* <a href="https://fastapi.tiangolo.com/img/foo.jpg" class="external-link" target="_blank">連結到圖片</a> * [連結到圖片](https://fastapi.tiangolo.com/img/foo.jpg)
連結文字應被翻譯,連結位址應指向對應的翻譯版本: 連結文字應被翻譯,連結位址應指向對應的翻譯版本:
* <a href="https://fastapi.tiangolo.com/zh-hant/" class="external-link" target="_blank">FastAPI 連結</a> * [FastAPI 連結](https://fastapi.tiangolo.com/zh-hant/)
//// ////
@ -291,7 +291,7 @@ Hello again.
* 即時 * 即時
* 標準 * 標準
* 預設 * 預設
* 分大小寫 * 分大小寫
* 不區分大小寫 * 不區分大小寫
* 提供應用程式服務 * 提供應用程式服務

60
docs/zh-hant/docs/alternatives.md

@ -14,7 +14,7 @@
## 先前的工具 { #previous-tools } ## 先前的工具 { #previous-tools }
### <a href="https://www.djangoproject.com/" class="external-link" target="_blank">Django</a> { #django } ### [Django](https://www.djangoproject.com/) { #django }
它是最受歡迎且廣受信任的 Python 框架。像 Instagram 等系統就是用它打造的。 它是最受歡迎且廣受信任的 Python 框架。像 Instagram 等系統就是用它打造的。
@ -22,7 +22,7 @@
它一開始是為在後端產生 HTML 而設計,而非為了建立提供現代前端(如 React、Vue.js、Angular)或其他系統(如 <abbr title="Internet of Things - 物聯網">IoT</abbr> 裝置)使用的 API。 它一開始是為在後端產生 HTML 而設計,而非為了建立提供現代前端(如 React、Vue.js、Angular)或其他系統(如 <abbr title="Internet of Things - 物聯網">IoT</abbr> 裝置)使用的 API。
### <a href="https://www.django-rest-framework.org/" class="external-link" target="_blank">Django REST Framework</a> { #django-rest-framework } ### [Django REST Framework](https://www.django-rest-framework.org/) { #django-rest-framework }
Django REST framework 的目標是成為一套在 Django 之上構建 Web API 的彈性工具組,以強化其 API 能力。 Django REST framework 的目標是成為一套在 Django 之上構建 Web API 的彈性工具組,以強化其 API 能力。
@ -30,7 +30,7 @@ Django REST framework 的目標是成為一套在 Django 之上構建 Web API
它是「自動 API 文件」的早期典範之一,而這正是啟發我「尋找」**FastAPI** 的第一個想法。 它是「自動 API 文件」的早期典範之一,而這正是啟發我「尋找」**FastAPI** 的第一個想法。
/// note | 注意 /// note
Django REST Framework 由 Tom Christie 創建。他同時也是 Starlette 與 Uvicorn 的作者,而 **FastAPI** 就是建立在它們之上。 Django REST Framework 由 Tom Christie 創建。他同時也是 Starlette 與 Uvicorn 的作者,而 **FastAPI** 就是建立在它們之上。
@ -42,7 +42,7 @@ Django REST Framework 由 Tom Christie 創建。他同時也是 Starlette 與 Uv
/// ///
### <a href="https://flask.palletsprojects.com" class="external-link" target="_blank">Flask</a> { #flask } ### [Flask](https://flask.palletsprojects.com) { #flask }
Flask 是一個「微框架」,它不包含資料庫整合,也沒有像 Django 那樣內建許多功能。 Flask 是一個「微框架」,它不包含資料庫整合,也沒有像 Django 那樣內建許多功能。
@ -64,7 +64,7 @@ Flask 是一個「微框架」,它不包含資料庫整合,也沒有像 Djan
/// ///
### <a href="https://requests.readthedocs.io" class="external-link" target="_blank">Requests</a> { #requests } ### [Requests](https://requests.readthedocs.io) { #requests }
**FastAPI** 其實不是 **Requests** 的替代品。兩者的範疇截然不同。 **FastAPI** 其實不是 **Requests** 的替代品。兩者的範疇截然不同。
@ -106,7 +106,7 @@ def read_url():
/// ///
### <a href="https://swagger.io/" class="external-link" target="_blank">Swagger</a> / <a href="https://github.com/OAI/OpenAPI-Specification/" class="external-link" target="_blank">OpenAPI</a> { #swagger-openapi } ### [Swagger](https://swagger.io/) / [OpenAPI](https://github.com/OAI/OpenAPI-Specification/) { #swagger-openapi }
我想從 Django REST Framework 得到的主要功能是自動 API 文件。 我想從 Django REST Framework 得到的主要功能是自動 API 文件。
@ -124,8 +124,8 @@ def read_url():
並整合基於標準的使用者介面工具: 並整合基於標準的使用者介面工具:
* <a href="https://github.com/swagger-api/swagger-ui" class="external-link" target="_blank">Swagger UI</a> * [Swagger UI](https://github.com/swagger-api/swagger-ui)
* <a href="https://github.com/Rebilly/ReDoc" class="external-link" target="_blank">ReDoc</a> * [ReDoc](https://github.com/Rebilly/ReDoc)
選擇這兩個是因為它們相當受歡迎且穩定,但稍加搜尋,你會發現有數十種 OpenAPI 的替代使用者介面(都能與 **FastAPI** 一起使用)。 選擇這兩個是因為它們相當受歡迎且穩定,但稍加搜尋,你會發現有數十種 OpenAPI 的替代使用者介面(都能與 **FastAPI** 一起使用)。
@ -135,7 +135,7 @@ def read_url():
有幾個 Flask 的 REST 框架,但在投入時間調查後,我發現許多已停止維護或被棄置,且存在一些關鍵問題使之不適用。 有幾個 Flask 的 REST 框架,但在投入時間調查後,我發現許多已停止維護或被棄置,且存在一些關鍵問題使之不適用。
### <a href="https://marshmallow.readthedocs.io/en/stable/" class="external-link" target="_blank">Marshmallow</a> { #marshmallow } ### [Marshmallow](https://marshmallow.readthedocs.io/en/stable/) { #marshmallow }
API 系統需要的主要功能之一是資料「<dfn title="也稱為 marshalling、轉換">序列化</dfn>」,也就是把程式中的資料(Python)轉成能透過網路傳輸的形式。例如,將含有資料庫資料的物件轉成 JSON 物件、把 `datetime` 物件轉成字串等等。 API 系統需要的主要功能之一是資料「<dfn title="也稱為 marshalling、轉換">序列化</dfn>」,也就是把程式中的資料(Python)轉成能透過網路傳輸的形式。例如,將含有資料庫資料的物件轉成 JSON 物件、把 `datetime` 物件轉成字串等等。
@ -153,7 +153,7 @@ API 需要的另一個重要功能是資料驗證,確保資料在特定條件
/// ///
### <a href="https://webargs.readthedocs.io/en/latest/" class="external-link" target="_blank">Webargs</a> { #webargs } ### [Webargs](https://webargs.readthedocs.io/en/latest/) { #webargs }
API 所需的另一項大功能,是從傳入請求中<dfn title="讀取並轉換為 Python 資料">解析</dfn>資料。 API 所需的另一項大功能,是從傳入請求中<dfn title="讀取並轉換為 Python 資料">解析</dfn>資料。
@ -175,7 +175,7 @@ Webargs 由與 Marshmallow 相同的開發者創建。
/// ///
### <a href="https://apispec.readthedocs.io/en/stable/" class="external-link" target="_blank">APISpec</a> { #apispec } ### [APISpec](https://apispec.readthedocs.io/en/stable/) { #apispec }
Marshmallow 與 Webargs 以外掛提供驗證、解析與序列化。 Marshmallow 與 Webargs 以外掛提供驗證、解析與序列化。
@ -205,7 +205,7 @@ APISpec 由與 Marshmallow 相同的開發者創建。
/// ///
### <a href="https://flask-apispec.readthedocs.io/en/latest/" class="external-link" target="_blank">Flask-apispec</a> { #flask-apispec } ### [Flask-apispec](https://flask-apispec.readthedocs.io/en/latest/) { #flask-apispec }
這是一個 Flask 外掛,把 Webargs、Marshmallow 與 APISpec 串在一起。 這是一個 Flask 外掛,把 Webargs、Marshmallow 與 APISpec 串在一起。
@ -219,11 +219,11 @@ APISpec 由與 Marshmallow 相同的開發者創建。
使用它促成了數個 Flask 全端(full-stack)產生器。這些是我(以及若干外部團隊)至今主要使用的技術組合: 使用它促成了數個 Flask 全端(full-stack)產生器。這些是我(以及若干外部團隊)至今主要使用的技術組合:
* <a href="https://github.com/tiangolo/full-stack" class="external-link" target="_blank">https://github.com/tiangolo/full-stack</a> * [https://github.com/tiangolo/full-stack](https://github.com/tiangolo/full-stack)
* <a href="https://github.com/tiangolo/full-stack-flask-couchbase" class="external-link" target="_blank">https://github.com/tiangolo/full-stack-flask-couchbase</a> * [https://github.com/tiangolo/full-stack-flask-couchbase](https://github.com/tiangolo/full-stack-flask-couchbase)
* <a href="https://github.com/tiangolo/full-stack-flask-couchdb" class="external-link" target="_blank">https://github.com/tiangolo/full-stack-flask-couchdb</a> * [https://github.com/tiangolo/full-stack-flask-couchdb](https://github.com/tiangolo/full-stack-flask-couchdb)
而這些全端產生器,也成為了 [**FastAPI** 專案產生器](project-generation.md){.internal-link target=_blank} 的基礎。 而這些全端產生器,也成為了 [**FastAPI** 專案產生器](project-generation.md) 的基礎。
/// info /// info
@ -237,7 +237,7 @@ Flask-apispec 由與 Marshmallow 相同的開發者創建。
/// ///
### <a href="https://nestjs.com/" class="external-link" target="_blank">NestJS</a>(與 <a href="https://angular.io/" class="external-link" target="_blank">Angular</a>) { #nestjs-and-angular } ### [NestJS](https://nestjs.com/)(與 [Angular](https://angular.io/)) { #nestjs-and-angular }
這甚至不是 Python。NestJS 是受 Angular 啟發的 JavaScript(TypeScript)NodeJS 框架。 這甚至不是 Python。NestJS 是受 Angular 啟發的 JavaScript(TypeScript)NodeJS 框架。
@ -259,13 +259,13 @@ Flask-apispec 由與 Marshmallow 相同的開發者創建。
/// ///
### <a href="https://sanic.readthedocs.io/en/latest/" class="external-link" target="_blank">Sanic</a> { #sanic } ### [Sanic](https://sanic.readthedocs.io/en/latest/) { #sanic }
它是最早基於 `asyncio` 的極高速 Python 框架之一,並做得很像 Flask。 它是最早基於 `asyncio` 的極高速 Python 框架之一,並做得很像 Flask。
/// note | 技術細節 /// note | 技術細節
它使用 <a href="https://github.com/MagicStack/uvloop" class="external-link" target="_blank">`uvloop`</a> 取代預設的 Python `asyncio` 事件圈。這也是它如此之快的原因。 它使用 [`uvloop`](https://github.com/MagicStack/uvloop) 取代預設的 Python `asyncio` 事件圈。這也是它如此之快的原因。
它明顯啟發了 Uvicorn 與 Starlette,而在公開的基準測試中,它們目前比 Sanic 更快。 它明顯啟發了 Uvicorn 與 Starlette,而在公開的基準測試中,它們目前比 Sanic 更快。
@ -279,7 +279,7 @@ Flask-apispec 由與 Marshmallow 相同的開發者創建。
/// ///
### <a href="https://falconframework.org/" class="external-link" target="_blank">Falcon</a> { #falcon } ### [Falcon](https://falconframework.org/) { #falcon }
Falcon 是另一個高效能 Python 框架,設計上極簡,並作為其他框架(如 Hug)的基礎。 Falcon 是另一個高效能 Python 框架,設計上極簡,並作為其他框架(如 Hug)的基礎。
@ -297,7 +297,7 @@ Falcon 是另一個高效能 Python 框架,設計上極簡,並作為其他
/// ///
### <a href="https://moltenframework.com/" class="external-link" target="_blank">Molten</a> { #molten } ### [Molten](https://moltenframework.com/) { #molten }
我在 **FastAPI** 打造的早期發現了 Molten。它有一些相當類似的想法: 我在 **FastAPI** 打造的早期發現了 Molten。它有一些相當類似的想法:
@ -321,7 +321,7 @@ Falcon 是另一個高效能 Python 框架,設計上極簡,並作為其他
/// ///
### <a href="https://github.com/hugapi/hug" class="external-link" target="_blank">Hug</a> { #hug } ### [Hug](https://github.com/hugapi/hug) { #hug }
Hug 是最早使用 Python 型別提示來宣告 API 參數型別的框架之一。這是個很棒的點子,也啟發了其他工具。 Hug 是最早使用 Python 型別提示來宣告 API 參數型別的框架之一。這是個很棒的點子,也啟發了其他工具。
@ -337,7 +337,7 @@ Hug 是最早使用 Python 型別提示來宣告 API 參數型別的框架之一
/// info /// info
Hug 由 Timothy Crosley 創建,他同時也是 <a href="https://github.com/timothycrosley/isort" class="external-link" target="_blank">`isort`</a> 的作者,一個自動排序 Python 匯入的好工具。 Hug 由 Timothy Crosley 創建,他同時也是 [`isort`](https://github.com/timothycrosley/isort) 的作者,一個自動排序 Python 匯入的好工具。
/// ///
@ -351,7 +351,7 @@ Hug 啟發 **FastAPI** 在函式中宣告 `response` 參數以設定標頭與 Co
/// ///
### <a href="https://github.com/encode/apistar" class="external-link" target="_blank">APIStar</a> (<= 0.5) { #apistar-0-5 } ### [APIStar](https://github.com/encode/apistar) (<= 0.5) { #apistar-0-5 }
在決定打造 **FastAPI** 之前,我找到了 **APIStar** 伺服器。它幾乎具備我所尋找的一切,而且設計很出色。 在決定打造 **FastAPI** 之前,我找到了 **APIStar** 伺服器。它幾乎具備我所尋找的一切,而且設計很出色。
@ -401,7 +401,7 @@ APIStar 由 Tom Christie 創建。他也創建了:
## **FastAPI** 所採用的工具 { #used-by-fastapi } ## **FastAPI** 所採用的工具 { #used-by-fastapi }
### <a href="https://docs.pydantic.dev/" class="external-link" target="_blank">Pydantic</a> { #pydantic } ### [Pydantic](https://docs.pydantic.dev/) { #pydantic }
Pydantic 是基於 Python 型別提示,定義資料驗證、序列化與文件(使用 JSON Schema)的函式庫。 Pydantic 是基於 Python 型別提示,定義資料驗證、序列化與文件(使用 JSON Schema)的函式庫。
@ -417,7 +417,7 @@ Pydantic 是基於 Python 型別提示,定義資料驗證、序列化與文件
/// ///
### <a href="https://www.starlette.dev/" class="external-link" target="_blank">Starlette</a> { #starlette } ### [Starlette](https://www.starlette.dev/) { #starlette }
Starlette 是一個輕量的 <dfn title="用於構建非同步 Python 網頁應用的新標準">ASGI</dfn> 框架/工具集,非常適合用來建構高效能的 asyncio 服務。 Starlette 是一個輕量的 <dfn title="用於構建非同步 Python 網頁應用的新標準">ASGI</dfn> 框架/工具集,非常適合用來建構高效能的 asyncio 服務。
@ -458,11 +458,11 @@ ASGI 是由 Django 核心團隊成員正在開發的新「標準」。它尚未
`FastAPI` 這個類別本身直接繼承自 `Starlette` 類別。 `FastAPI` 這個類別本身直接繼承自 `Starlette` 類別。
因此,凡是你能用 Starlette 做的事,你幾乎都能直接用 **FastAPI** 完成,因為它基本上就是加強版的 Starlette。 因此,凡是你能用 Starlette 做的事,你都能直接用 **FastAPI** 完成,因為它基本上就是加強版的 Starlette。
/// ///
### <a href="https://www.uvicorn.dev/" class="external-link" target="_blank">Uvicorn</a> { #uvicorn } ### [Uvicorn](https://www.uvicorn.dev/) { #uvicorn }
Uvicorn 是基於 uvloop 與 httptools 的極速 ASGI 伺服器。 Uvicorn 是基於 uvloop 與 httptools 的極速 ASGI 伺服器。
@ -476,10 +476,10 @@ Uvicorn 是基於 uvloop 與 httptools 的極速 ASGI 伺服器。
你也可以使用 `--workers` 命令列選項,取得非同步的多製程伺服器。 你也可以使用 `--workers` 命令列選項,取得非同步的多製程伺服器。
更多細節請見[部署](deployment/index.md){.internal-link target=_blank}章節。 更多細節請見[部署](deployment/index.md)章節。
/// ///
## 效能與速度 { #benchmarks-and-speed } ## 效能與速度 { #benchmarks-and-speed }
想了解、比較並看出 Uvicorn、Starlette 與 FastAPI 之間的差異,請參考[效能評測](benchmarks.md){.internal-link target=_blank} 想了解、比較並看出 Uvicorn、Starlette 與 FastAPI 之間的差異,請參考[效能評測](benchmarks.md)。

18
docs/zh-hant/docs/async.md

@ -141,7 +141,7 @@ def results():
/// info /// info
漂亮的插畫來自 <a href="https://www.instagram.com/ketrinadrawsalot" class="external-link" target="_blank">Ketrina Thompson</a>. 🎨 漂亮的插畫來自 [Ketrina Thompson](https://www.instagram.com/ketrinadrawsalot)。 🎨
/// ///
@ -207,7 +207,7 @@ def results():
/// info /// info
漂亮的插畫來自 <a href="https://www.instagram.com/ketrinadrawsalot" class="external-link" target="_blank">Ketrina Thompson</a>. 🎨 漂亮的插畫來自 [Ketrina Thompson](https://www.instagram.com/ketrinadrawsalot)。 🎨
/// ///
@ -245,13 +245,13 @@ def results():
這種「等待」 🕙 通常以微秒來衡量,但累加起來,最終還是花費了很多等待時間。 這種「等待」 🕙 通常以微秒來衡量,但累加起來,最終還是花費了很多等待時間。
這就是為什麼對於 Web API 來說,使用非同步程式碼 ⏸🔀⏯ 是非常有意的。 這就是為什麼對於 Web API 來說,使用非同步程式碼 ⏸🔀⏯ 是非常有意的。
這種類型的非同步性正是 NodeJS 成功的原因(儘管 NodeJS 不是平行的),這也是 Go 語言作為程式語言的一個強大優勢。 這種類型的非同步性正是 NodeJS 成功的原因(儘管 NodeJS 不是平行的),這也是 Go 語言作為程式語言的一個強大優勢。
這與 **FastAPI** 所能提供的性能水平相同。 這與 **FastAPI** 所能提供的性能水平相同。
你可以同時利用並行性和平行性,進一步提升效能,這比大多數已測試的 NodeJS 框架都更快,並且與 Go 語言相當,而 Go 是一種更接近 C 的編譯語言(<a href="https://www.techempower.com/benchmarks/#section=data-r17&hw=ph&test=query&l=zijmkf-1" class="external-link" target="_blank">感謝 Starlette</a>)。 你可以同時利用並行性和平行性,進一步提升效能,這比大多數已測試的 NodeJS 框架都更快,並且與 Go 語言相當,而 Go 是一種更接近 C 的編譯語言([感謝 Starlette](https://www.techempower.com/benchmarks/#section=data-r17&hw=ph&test=query&l=zijmkf-1))。
### 並行比平行更好嗎? { #is-concurrency-better-than-parallelism } ### 並行比平行更好嗎? { #is-concurrency-better-than-parallelism }
@ -363,13 +363,13 @@ async def read_burgers():
### 編寫自己的非同步程式碼 { #write-your-own-async-code } ### 編寫自己的非同步程式碼 { #write-your-own-async-code }
Starlette(和 **FastAPI**)是基於 <a href="https://anyio.readthedocs.io/en/stable/" class="external-link" target="_blank">AnyIO</a> 實作的,這使得它們與 Python 標準函式庫 <a href="https://docs.python.org/3/library/asyncio-task.html" class="external-link" target="_blank">asyncio</a><a href="https://trio.readthedocs.io/en/stable/" class="external-link" target="_blank">Trio</a> 相容。 Starlette(和 **FastAPI**)是基於 [AnyIO](https://anyio.readthedocs.io/en/stable/) 實作的,這使得它們與 Python 標準函式庫 [asyncio](https://docs.python.org/3/library/asyncio-task.html) 和 [Trio](https://trio.readthedocs.io/en/stable/) 相容。
特別是,你可以直接使用 <a href="https://anyio.readthedocs.io/en/stable/" class="external-link" target="_blank">AnyIO</a> 來處理更複雜的並行使用案例,這些案例需要你在自己的程式碼中使用更高階的模式。 特別是,你可以直接使用 [AnyIO](https://anyio.readthedocs.io/en/stable/) 來處理更複雜的並行使用案例,這些案例需要你在自己的程式碼中使用更高階的模式。
即使你不使用 **FastAPI**,你也可以使用 <a href="https://anyio.readthedocs.io/en/stable/" class="external-link" target="_blank">AnyIO</a> 來撰寫自己的非同步應用程式,並獲得高相容性及一些好處(例如「結構化並行」)。 即使你不使用 **FastAPI**,你也可以使用 [AnyIO](https://anyio.readthedocs.io/en/stable/) 來撰寫自己的非同步應用程式,並獲得高相容性及一些好處(例如「結構化並行」)。
我另外在 AnyIO 之上做了一個薄封裝的函式庫,稍微改進型別註解以獲得更好的**自動補全**、**即時錯誤**等。同時它也提供友善的介紹與教學,幫助你**理解**並撰寫**自己的非同步程式碼**:<a href="https://asyncer.tiangolo.com/" class="external-link" target="_blank">Asyncer</a>。當你需要**將非同步程式碼與一般**(阻塞/同步)**程式碼整合**時,它特別實用。 我另外在 AnyIO 之上做了一個薄封裝的函式庫,稍微改進型別註解以獲得更好的**自動補全**、**即時錯誤**等。同時它也提供友善的介紹與教學,幫助你**理解**並撰寫**自己的非同步程式碼**:[Asyncer](https://asyncer.tiangolo.com/)。當你需要**將非同步程式碼與一般**(阻塞/同步)**程式碼整合**時,它特別實用。
### 其他形式的非同步程式碼 { #other-forms-of-asynchronous-code } ### 其他形式的非同步程式碼 { #other-forms-of-asynchronous-code }
@ -381,7 +381,7 @@ Starlette(和 **FastAPI**)是基於 <a href="https://anyio.readthedocs.io/en
但在此之前,處理非同步程式碼要更加複雜和困難。 但在此之前,處理非同步程式碼要更加複雜和困難。
在較舊的 Python 版本中,你可能會使用多執行緒或 <a href="https://www.gevent.org/" class="external-link" target="_blank">Gevent</a>。但這些程式碼要更難以理解、調試和思考。 在較舊的 Python 版本中,你可能會使用多執行緒或 [Gevent](https://www.gevent.org/)。但這些程式碼要更難以理解、調試和思考。
在較舊的 NodeJS / 瀏覽器 JavaScript 中,你會使用「回呼」,這可能會導致“回呼地獄”。 在較舊的 NodeJS / 瀏覽器 JavaScript 中,你會使用「回呼」,這可能會導致“回呼地獄”。

2
docs/zh-hant/docs/benchmarks.md

@ -1,6 +1,6 @@
# 基準測試 { #benchmarks } # 基準測試 { #benchmarks }
由第三方機構 TechEmpower 的基準測試表明在 Uvicorn 下運行的 **FastAPI** 應用程式是 <a href="https://www.techempower.com/benchmarks/#section=test&runid=7464e520-0dc2-473d-bd34-dbdfd7e85911&hw=ph&test=query&l=zijzen-7" class="external-link" target="_blank">最快的 Python 可用框架之一</a>,僅次於 Starlette 和 Uvicorn 本身(於 FastAPI 內部使用)。 由第三方機構 TechEmpower 的基準測試表明在 Uvicorn 下運行的 **FastAPI** 應用程式是 [最快的 Python 可用框架之一](https://www.techempower.com/benchmarks/#section=test&runid=7464e520-0dc2-473d-bd34-dbdfd7e85911&hw=ph&test=query&l=zijzen-7),僅次於 Starlette 和 Uvicorn 本身(於 FastAPI 內部使用)。
但是在查看基準得分和對比時,請注意以下幾點。 但是在查看基準得分和對比時,請注意以下幾點。

10
docs/zh-hant/docs/environment-variables.md

@ -65,7 +65,7 @@ print(f"Hello {name} from Python")
/// tip /// tip
第二個參數是 <a href="https://docs.python.org/3.8/library/os.html#os.getenv" class="external-link" target="_blank">`os.getenv()`</a> 的預設回傳值。 第二個參數是 [`os.getenv()`](https://docs.python.org/3.8/library/os.html#os.getenv) 的預設回傳值。
如果沒有提供,預設值為 `None`,這裡我們提供 `"World"` 作為預設值。 如果沒有提供,預設值為 `None`,這裡我們提供 `"World"` 作為預設值。
@ -153,7 +153,7 @@ Hello World from Python
/// tip /// tip
你可以在 <a href="https://12factor.net/config" class="external-link" target="_blank">The Twelve-Factor App: 配置</a>中了解更多資訊。 你可以在 [The Twelve-Factor App: 配置](https://12factor.net/config) 中了解更多資訊。
/// ///
@ -163,7 +163,7 @@ Hello World from Python
這意味著從環境變數中讀取的**任何值**在 Python 中都將是一個 `str`,任何型別轉換或驗證都必須在程式碼中完成。 這意味著從環境變數中讀取的**任何值**在 Python 中都將是一個 `str`,任何型別轉換或驗證都必須在程式碼中完成。
你將在[進階使用者指南 - 設定和環境變數](./advanced/settings.md){.internal-link target=_blank}中了解更多關於使用環境變數處理**應用程式設定**的資訊。 你將在[進階使用者指南 - 設定和環境變數](./advanced/settings.md)中了解更多關於使用環境變數處理**應用程式設定**的資訊。
## `PATH` 環境變數 { #path-environment-variable } ## `PATH` 環境變數 { #path-environment-variable }
@ -285,13 +285,13 @@ $ C:\opt\custompython\bin\python
//// ////
當學習[虛擬環境](virtual-environments.md){.internal-link target=_blank}時,這些資訊將會很有用。 當學習[虛擬環境](virtual-environments.md)時,這些資訊將會很有用。
## 結論 { #conclusion } ## 結論 { #conclusion }
透過這個教學,你應該對**環境變數**是什麼以及如何在 Python 中使用它們有了基本的了解。 透過這個教學,你應該對**環境變數**是什麼以及如何在 Python 中使用它們有了基本的了解。
你也可以在 <a href="https://en.wikipedia.org/wiki/Environment_variable" class="external-link" target="_blank">環境變數的維基百科條目</a> 中閱讀更多。 你也可以在 [環境變數的維基百科條目](https://en.wikipedia.org/wiki/Environment_variable) 中閱讀更多。
在許多情況下,環境變數的用途和適用性可能不會立刻顯現。但是在開發過程中,它們會在許多不同的場景中出現,因此瞭解它們是非常必要的。 在許多情況下,環境變數的用途和適用性可能不會立刻顯現。但是在開發過程中,它們會在許多不同的場景中出現,因此瞭解它們是非常必要的。

71
docs/zh-hant/docs/fastapi-cli.md

@ -1,15 +1,15 @@
# FastAPI CLI { #fastapi-cli } # FastAPI CLI { #fastapi-cli }
**FastAPI CLI** 是一個命令列程式,能用來運行你的 FastAPI 應用程式、管理你的 FastAPI 專案等。 **FastAPI <abbr title="command line interface - 命令列介面">CLI</abbr>** 是一個命令列程式,你可以用它來啟動你的 FastAPI 應用程式、管理你的 FastAPI 專案,等等。
當你安裝 FastAPI(例如使用 `pip install "fastapi[standard]"`,它會包含一個叫做 `fastapi-cli` 的套件,這個套件提供了 `fastapi` 命令 當你安裝 FastAPI(例如使用 `pip install "fastapi[standard]"`時,會附帶一個可以在終端機執行的命令列程式
要運行你的 FastAPI 應用程式來進行開發,你可以使用 `fastapi dev` 令: 在開發時運行你的 FastAPI 應用程式,你可以使用 `fastapi dev` 令:
<div class="termy"> <div class="termy">
```console ```console
$ <font color="#4E9A06">fastapi</font> dev <u style="text-decoration-style:solid">main.py</u> $ <font color="#4E9A06">fastapi</font> dev
<span style="background-color:#009485"><font color="#D3D7CF"> FastAPI </font></span> Starting development server 🚀 <span style="background-color:#009485"><font color="#D3D7CF"> FastAPI </font></span> Starting development server 🚀
@ -46,13 +46,66 @@ $ <font color="#4E9A06">fastapi</font> dev <u style="text-decoration-style:solid
</div> </div>
名為 `fastapi` 的命令列程式就是 **FastAPI CLI** /// tip
在生產環境請改用 `fastapi run`,不要用 `fastapi dev`。🚀
///
在內部,**FastAPI CLI** 使用 [Uvicorn](https://www.uvicorn.dev),這是一個高效能、適用於生產環境的 ASGI 伺服器。😎
`fastapi` CLI 會嘗試自動偵測要執行的 FastAPI 應用程式,預設假設它是檔案 `main.py` 中名為 `app` 的物件(或其他幾種變體)。
不過你也可以明確設定要使用的 app。
## 在 `pyproject.toml` 中設定應用程式的 `entrypoint` { #configure-the-app-entrypoint-in-pyproject.toml }
你可以在 `pyproject.toml` 檔案中指定你的 app 位置,例如:
```toml
[tool.fastapi]
entrypoint = "main:app"
```
這個 `entrypoint` 會告訴 `fastapi` 指令應該像這樣匯入 app:
```python
from main import app
```
FastAPI CLI 接收你的 Python 程式路徑(例如 `main.py`),並自動檢測 `FastAPI` 實例(通常命名為 `app`),確定正確的引入模組流程,然後運行該應用程式。 如果你的程式碼結構是這樣:
```
.
├── backend
│   ├── main.py
│   ├── __init__.py
```
那麼你應該把 `entrypoint` 設為:
```toml
[tool.fastapi]
entrypoint = "backend.main:app"
```
這等同於:
```python
from backend.main import app
```
### 帶路徑的 `fastapi dev` { #fastapi-dev-with-path }
你也可以把檔案路徑傳給 `fastapi dev` 指令,它會推測要使用的 FastAPI app 物件:
```console
$ fastapi dev main.py
```
在生產環境,你應該使用 `fastapi run` 命令。 🚀 但這樣每次呼叫 `fastapi` 指令時都得記得傳入正確的路徑。
**FastAPI CLI** 內部使用了 <a href="https://www.uvicorn.dev" class="external-link" target="_blank">Uvicorn</a>,這是一個高效能、適合生產環境的 ASGI 伺服器。 😎 此外,其他工具可能找不到它,例如 [VS Code 擴充套件](editor-support.md) 或 [FastAPI Cloud](https://fastapicloud.com),因此建議在 `pyproject.toml` 中使用 `entrypoint`
## `fastapi dev` { #fastapi-dev } ## `fastapi dev` { #fastapi-dev }
@ -70,6 +123,6 @@ FastAPI CLI 接收你的 Python 程式路徑(例如 `main.py`),並自動
/// tip /// tip
你可以在[部署文件](deployment/index.md){.internal-link target=_blank}中了解更多相關資訊。 你可以在[部署文件](deployment/index.md)中了解更多相關資訊。
/// ///

26
docs/zh-hant/docs/features.md

@ -6,8 +6,8 @@
### 建立在開放標準的基礎上 { #based-on-open-standards } ### 建立在開放標準的基礎上 { #based-on-open-standards }
* 使用 <a href="https://github.com/OAI/OpenAPI-Specification" class="external-link" target="_blank"><strong>OpenAPI</strong></a> 來建立 API,包含 <dfn title="也稱為:端點、路由">路徑</dfn><dfn title="也稱為 HTTP 方法,例如 POST、GET、PUT、DELETE">操作</dfn>、參數、請求內文、安全性等宣告。 * 使用 [**OpenAPI**](https://github.com/OAI/OpenAPI-Specification) 來建立 API,包含 <dfn title="也稱為:端點、路由">路徑</dfn><dfn title="也稱為 HTTP 方法,例如 POST、GET、PUT、DELETE">操作</dfn>、參數、請求內文、安全性等宣告。
* 使用 <a href="https://json-schema.org/" class="external-link" target="_blank"><strong>JSON Schema</strong></a>(因為 OpenAPI 本身就是基於 JSON Schema)自動生成資料模型文件。 * 使用 [**JSON Schema**](https://json-schema.org/)(因為 OpenAPI 本身就是基於 JSON Schema)自動生成資料模型文件。
* 經過縝密的研究後圍繞這些標準進行設計,而不是事後在已有系統上附加的一層功能。 * 經過縝密的研究後圍繞這些標準進行設計,而不是事後在已有系統上附加的一層功能。
* 這也讓我們在多種語言中可以使用自動**用戶端程式碼生成**。 * 這也讓我們在多種語言中可以使用自動**用戶端程式碼生成**。
@ -15,11 +15,11 @@
FastAPI 能生成互動式 API 文件和探索性的 Web 使用者介面。由於該框架基於 OpenAPI,因此有多種選擇,預設提供了兩種。 FastAPI 能生成互動式 API 文件和探索性的 Web 使用者介面。由於該框架基於 OpenAPI,因此有多種選擇,預設提供了兩種。
* <a href="https://github.com/swagger-api/swagger-ui" class="external-link" target="_blank"><strong>Swagger UI</strong></a> 提供互動式探索,讓你可以直接從瀏覽器呼叫並測試你的 API 。 * [**Swagger UI**](https://github.com/swagger-api/swagger-ui) 提供互動式探索,讓你可以直接從瀏覽器呼叫並測試你的 API 。
![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)
* 使用 <a href="https://github.com/Rebilly/ReDoc" class="external-link" target="_blank"><strong>ReDoc</strong></a> 的替代 API 文件。 * 使用 [**ReDoc**](https://github.com/Rebilly/ReDoc) 的替代 API 文件。
![ReDoc](https://fastapi.tiangolo.com/img/index/index-06-redoc-02.png) ![ReDoc](https://fastapi.tiangolo.com/img/index/index-06-redoc-02.png)
@ -27,7 +27,7 @@ FastAPI 能生成互動式 API 文件和探索性的 Web 使用者介面。由
這一切都基於標準的 **Python 型別**宣告(感謝 Pydantic)。無需學習新的語法,只需使用標準的現代 Python。 這一切都基於標準的 **Python 型別**宣告(感謝 Pydantic)。無需學習新的語法,只需使用標準的現代 Python。
如果你需要 2 分鐘來學習如何使用 Python 型別(即使你不使用 FastAPI),可以看看這個簡短的教學:[Python 型別](python-types.md){.internal-link target=_blank} 如果你需要 2 分鐘來學習如何使用 Python 型別(即使你不使用 FastAPI),可以看看這個簡短的教學:[Python 型別](python-types.md)。
如果你寫帶有 Python 型別的程式碼: 如果你寫帶有 Python 型別的程式碼:
@ -75,7 +75,7 @@ my_second_user: User = User(**second_user_data)
整個框架的設計是為了讓使用變得簡單且直觀,在開始開發之前,所有決策都在多個編輯器上進行了測試,以確保提供最佳的開發體驗。 整個框架的設計是為了讓使用變得簡單且直觀,在開始開發之前,所有決策都在多個編輯器上進行了測試,以確保提供最佳的開發體驗。
在最近的 Python 開發者調查中,我們能看到<a href="https://www.jetbrains.com/research/python-developers-survey-2017/#tools-and-features" class="external-link" target="_blank"> 被使用最多的功能是 autocompletion</a> 在最近的 Python 開發者調查中,我們能看到[被使用最多的功能是 autocompletion](https://www.jetbrains.com/research/python-developers-survey-2017/#tools-and-features)
整個 **FastAPI** 框架就是基於這一點,任何地方都可以進行自動補齊。 整個 **FastAPI** 框架就是基於這一點,任何地方都可以進行自動補齊。
@ -83,11 +83,11 @@ my_second_user: User = User(**second_user_data)
在這裡,你的編輯器可能會這樣幫助你: 在這裡,你的編輯器可能會這樣幫助你:
* 在 <a href="https://code.visualstudio.com/" class="external-link" target="_blank">Visual Studio Code</a> 中: * 在 [Visual Studio Code](https://code.visualstudio.com/) 中:
![editor support](https://fastapi.tiangolo.com/img/vscode-completion.png) ![editor support](https://fastapi.tiangolo.com/img/vscode-completion.png)
* 在 <a href="https://www.jetbrains.com/pycharm/" class="external-link" target="_blank">PyCharm</a> 中: * 在 [PyCharm](https://www.jetbrains.com/pycharm/) 中:
![editor support](https://fastapi.tiangolo.com/img/pycharm-completion.png) ![editor support](https://fastapi.tiangolo.com/img/pycharm-completion.png)
@ -124,13 +124,13 @@ FastAPI 已經整合了安全性和身份驗證的功能,但不會強制與特
OpenAPI 中定義的安全模式,包括: OpenAPI 中定義的安全模式,包括:
* HTTP 基本認證。 * HTTP 基本認證。
* **OAuth2**(也使用 **JWT tokens**)。在 [OAuth2 with JWT](tutorial/security/oauth2-jwt.md){.internal-link target=_blank} 查看教學。 * **OAuth2**(也使用 **JWT tokens**)。在 [OAuth2 with JWT](tutorial/security/oauth2-jwt.md) 查看教學。
* API 密鑰,在: * API 密鑰,在:
* 標頭(Header) * 標頭(Header)
* 查詢參數 * 查詢參數
* Cookies,等等。 * Cookies,等等。
加上自 Starlette(包括 **session cookie**)的所有安全特性。 加上自 Starlette(包括 **session cookie**)的所有安全特性。
所有的這些都是可重複使用的工具和套件,可以輕鬆與你的系統、資料儲存(Data Stores)、關聯式資料庫(RDBMS)以及非關聯式資料庫(NoSQL)等等整合。 所有的這些都是可重複使用的工具和套件,可以輕鬆與你的系統、資料儲存(Data Stores)、關聯式資料庫(RDBMS)以及非關聯式資料庫(NoSQL)等等整合。
@ -159,11 +159,11 @@ FastAPI 有一個使用簡單,但是非常強大的 <dfn title='也稱為「co
## Starlette 特性 { #starlette-features } ## Starlette 特性 { #starlette-features }
**FastAPI** 完全相容且基於 <a href="https://www.starlette.dev/" class="external-link" target="_blank"><strong>Starlette</strong></a>。所以,你有其他的 Starlette 程式碼也能正常運作。`FastAPI` 實際上是 `Starlette` 的一個子類別。所以,如果你已經知道或者使用過 Starlette,大部分的功能會以相同的方式運作。 **FastAPI** 完全相容且基於 [**Starlette**](https://www.starlette.dev/)。所以,你有其他的 Starlette 程式碼也能正常運作。`FastAPI` 實際上是 `Starlette` 的一個子類別。所以,如果你已經知道或者使用過 Starlette,大部分的功能會以相同的方式運作。
通過 **FastAPI** 你可以獲得所有 **Starlette** 的特性(FastAPI 就像加強版的 Starlette): 通過 **FastAPI** 你可以獲得所有 **Starlette** 的特性(FastAPI 就像加強版的 Starlette):
* 性能極其出色。它是 <a href="https://github.com/encode/starlette#performance" class="external-link" target="_blank">Python 可用的最快框架之一,和 **NodeJS****Go** 相當</a> * 性能極其出色。它是 [Python 可用的最快框架之一,和 **NodeJS** 及 **Go** 相當](https://github.com/encode/starlette#performance)
* **支援 WebSocket** * **支援 WebSocket**
* 能在行程內處理背景任務。 * 能在行程內處理背景任務。
* 支援啟動和關閉事件。 * 支援啟動和關閉事件。
@ -175,7 +175,7 @@ FastAPI 有一個使用簡單,但是非常強大的 <dfn title='也稱為「co
## Pydantic 特性 { #pydantic-features } ## Pydantic 特性 { #pydantic-features }
**FastAPI** 完全相容且基於 <a href="https://docs.pydantic.dev/" class="external-link" target="_blank"><strong>Pydantic</strong></a>。所以,你有其他 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。

58
docs/zh-hant/docs/help-fastapi.md

@ -12,7 +12,7 @@
## 訂閱電子報 { #subscribe-to-the-newsletter } ## 訂閱電子報 { #subscribe-to-the-newsletter }
你可以訂閱(不常發送的)[**FastAPI 與夥伴**電子報](newsletter.md){.internal-link target=_blank},隨時掌握: 你可以訂閱(不常發送的)[**FastAPI 與夥伴**電子報](newsletter.md),隨時掌握:
* 關於 FastAPI 與夥伴的最新消息 🚀 * 關於 FastAPI 與夥伴的最新消息 🚀
* 教學指南 📝 * 教學指南 📝
@ -22,17 +22,17 @@
## 在 X(Twitter)關注 FastAPI { #follow-fastapi-on-x-twitter } ## 在 X(Twitter)關注 FastAPI { #follow-fastapi-on-x-twitter }
<a href="https://x.com/fastapi" class="external-link" target="_blank">**X(Twitter)** 關注 @fastapi</a>,獲取 **FastAPI** 的最新消息。🐦 [在 **X(Twitter)** 關注 @fastapi](https://x.com/fastapi),獲取 **FastAPI** 的最新消息。🐦
## 在 GitHub 為 **FastAPI** 加星 { #star-fastapi-in-github } ## 在 GitHub 為 **FastAPI** 加星 { #star-fastapi-in-github }
你可以在 GitHub 為 FastAPI「加星」(點擊右上角的 star 星號按鈕):<a href="https://github.com/fastapi/fastapi" class="external-link" target="_blank">https://github.com/fastapi/fastapi</a>。⭐️ 你可以在 GitHub 為 FastAPI「加星」(點擊右上角的 star 星號按鈕):[https://github.com/fastapi/fastapi](https://github.com/fastapi/fastapi)。⭐️
加上星標後,其他使用者會更容易發現它,並看到它已經對許多人很有幫助。 加上星標後,其他使用者會更容易發現它,並看到它已經對許多人很有幫助。
## 追蹤 GitHub 儲存庫的發行版 { #watch-the-github-repository-for-releases } ## 追蹤 GitHub 儲存庫的發行版 { #watch-the-github-repository-for-releases }
你可以在 GitHub「watch」FastAPI(點擊右上角的「watch」按鈕):<a href="https://github.com/fastapi/fastapi" class="external-link" target="_blank">https://github.com/fastapi/fastapi</a>。👀 你可以在 GitHub「watch」FastAPI(點擊右上角的「watch」按鈕):[https://github.com/fastapi/fastapi](https://github.com/fastapi/fastapi)。👀
在那裡你可以選擇「Releases only」。 在那裡你可以選擇「Releases only」。
@ -40,45 +40,45 @@
## 與作者連結 { #connect-with-the-author } ## 與作者連結 { #connect-with-the-author }
你可以與作者 <a href="https://tiangolo.com" class="external-link" target="_blank">我(Sebastián Ramírez / `tiangolo`</a> 建立連結。 你可以與作者 [我(Sebastián Ramírez / `tiangolo`)](https://tiangolo.com) 建立連結。
你可以: 你可以:
* <a href="https://github.com/tiangolo" class="external-link" target="_blank">**GitHub** 關注我</a> * [在 **GitHub** 關注我](https://github.com/tiangolo)
* 看看我建立的其他開源專案,可能對你有幫助。 * 看看我建立的其他開源專案,可能對你有幫助。
* 關注我以便知道我何時建立新的開源專案。 * 關注我以便知道我何時建立新的開源專案。
* <a href="https://x.com/tiangolo" class="external-link" target="_blank">**X(Twitter)**</a><a href="https://fosstodon.org/@tiangolo" class="external-link" target="_blank">Mastodon</a> 關注我。 * [在 **X(Twitter)**](https://x.com/tiangolo) 或 [Mastodon](https://fosstodon.org/@tiangolo) 關注我。
* 告訴我你如何使用 FastAPI(我很愛聽這些)。 * 告訴我你如何使用 FastAPI(我很愛聽這些)。
* 接收我發布公告或釋出新工具時的消息。 * 接收我發布公告或釋出新工具時的消息。
* 你也可以<a href="https://x.com/fastapi" class="external-link" target="_blank">在 X(Twitter)關注 @fastapi</a>(另外的帳號)。 * 你也可以[在 X(Twitter)關注 @fastapi](https://x.com/fastapi)(另外的帳號)。
* <a href="https://www.linkedin.com/in/tiangolo/" class="external-link" target="_blank">**LinkedIn** 關注我</a> * [在 **LinkedIn** 關注我](https://www.linkedin.com/in/tiangolo/)
* 接收我發布公告或釋出新工具時的消息(不過我更常用 X(Twitter)🤷‍♂)。 * 接收我發布公告或釋出新工具時的消息(不過我更常用 X(Twitter)🤷‍♂)。
* 在 <a href="https://dev.to/tiangolo" class="external-link" target="_blank">**Dev.to**</a><a href="https://medium.com/@tiangolo" class="external-link" target="_blank">**Medium**</a> 閱讀我寫的內容(或關注我)。 * 在 [**Dev.to**](https://dev.to/tiangolo) 或 [**Medium**](https://medium.com/@tiangolo) 閱讀我寫的內容(或關注我)。
* 閱讀我的想法、文章,以及我建立的工具。 * 閱讀我的想法、文章,以及我建立的工具。
* 關注我以便在我發佈新內容時能第一時間看到。 * 關注我以便在我發佈新內容時能第一時間看到。
## 在 X(Twitter)發文談談 **FastAPI** { #tweet-about-fastapi } ## 在 X(Twitter)發文談談 **FastAPI** { #tweet-about-fastapi }
<a href="https://x.com/compose/tweet?text=I'm loving @fastapi because... https://github.com/fastapi/fastapi" class="external-link" target="_blank">發一則關於 **FastAPI** 的推文</a>,讓我與其他人知道你為什麼喜歡它。🎉 [發一則關於 **FastAPI** 的推文](https://x.com/compose/tweet?text=I'm loving @fastapi because... https://github.com/fastapi/fastapi),讓我與其他人知道你為什麼喜歡它。🎉
我很樂於聽到 **FastAPI** 是如何被使用、你喜歡它的哪些地方、在哪個專案/公司使用它等等。 我很樂於聽到 **FastAPI** 是如何被使用、你喜歡它的哪些地方、在哪個專案/公司使用它等等。
## 為 FastAPI 投票 { #vote-for-fastapi } ## 為 FastAPI 投票 { #vote-for-fastapi }
* <a href="https://www.slant.co/options/34241/~fastapi-review" class="external-link" target="_blank">在 Slant 為 **FastAPI** 投票</a> * [在 Slant 為 **FastAPI** 投票](https://www.slant.co/options/34241/~fastapi-review)
* <a href="https://alternativeto.net/software/fastapi/about/" class="external-link" target="_blank">在 AlternativeTo 為 **FastAPI** 投票</a> * [在 AlternativeTo 為 **FastAPI** 投票](https://alternativeto.net/software/fastapi/about/)
* <a href="https://stackshare.io/pypi-fastapi" class="external-link" target="_blank">在 StackShare 表示你使用 **FastAPI**</a> * [在 StackShare 表示你使用 **FastAPI**](https://stackshare.io/pypi-fastapi)
## 在 GitHub 幫助他人解答問題 { #help-others-with-questions-in-github } ## 在 GitHub 幫助他人解答問題 { #help-others-with-questions-in-github }
你可以嘗試幫助他人回答以下地方的問題: 你可以嘗試幫助他人回答以下地方的問題:
* <a href="https://github.com/fastapi/fastapi/discussions/categories/questions?discussions_q=category%3AQuestions+is%3Aunanswered" class="external-link" target="_blank">GitHub Discussions</a> * [GitHub Discussions](https://github.com/fastapi/fastapi/discussions/categories/questions?discussions_q=category%3AQuestions+is%3Aunanswered)
* <a href="https://github.com/fastapi/fastapi/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+label%3Aquestion+-label%3Aanswered+" class="external-link" target="_blank">GitHub Issues</a> * [GitHub Issues](https://github.com/fastapi/fastapi/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+label%3Aquestion+-label%3Aanswered+)
很多時候你可能已經知道這些問題的解答。🤓 很多時候你可能已經知道這些問題的解答。🤓
如果你經常幫大家解決問題,你會成為官方的 [FastAPI 專家](fastapi-people.md#fastapi-experts){.internal-link target=_blank}。🎉 如果你經常幫大家解決問題,你會成為官方的 [FastAPI 專家](fastapi-people.md#fastapi-experts)。🎉
請記得,最重要的是:盡量友善。大家可能帶著挫折而來,很多時候提問方式不夠理想,但請盡你所能保持友善。🤗 請記得,最重要的是:盡量友善。大家可能帶著挫折而來,很多時候提問方式不夠理想,但請盡你所能保持友善。🤗
@ -104,7 +104,7 @@
很多時候他們只會貼出一小段程式碼,但那不足以**重現問題**。 很多時候他們只會貼出一小段程式碼,但那不足以**重現問題**。
* 你可以請他們提供一個<a href="https://stackoverflow.com/help/minimal-reproducible-example" class="external-link" target="_blank">最小可重現範例</a>,讓你可以**複製貼上**並在本機執行,看到與他們相同的錯誤或行為,或更好地理解他們的使用情境。 * 你可以請他們提供一個[最小可重現範例](https://stackoverflow.com/help/minimal-reproducible-example),讓你可以**複製貼上**並在本機執行,看到與他們相同的錯誤或行為,或更好地理解他們的使用情境。
* 如果你有心力,你也可以嘗試自己**建立一個範例**,僅依據問題描述來還原。不過請記得這可能很耗時,或許更好的是先請他們把問題說清楚。 * 如果你有心力,你也可以嘗試自己**建立一個範例**,僅依據問題描述來還原。不過請記得這可能很耗時,或許更好的是先請他們把問題說清楚。
@ -124,7 +124,7 @@
## 追蹤 GitHub 儲存庫 { #watch-the-github-repository } ## 追蹤 GitHub 儲存庫 { #watch-the-github-repository }
你可以在 GitHub「watch」FastAPI(點擊右上角的「watch」按鈕):<a href="https://github.com/fastapi/fastapi" class="external-link" target="_blank">https://github.com/fastapi/fastapi</a>。👀 你可以在 GitHub「watch」FastAPI(點擊右上角的「watch」按鈕):[https://github.com/fastapi/fastapi](https://github.com/fastapi/fastapi)。👀
如果你選擇「Watching」而不是「Releases only」,當有人建立新的 issue 或問題時你會收到通知。你也可以指定只想被通知新的 issues、discussions、PR 等等。 如果你選擇「Watching」而不是「Releases only」,當有人建立新的 issue 或問題時你會收到通知。你也可以指定只想被通知新的 issues、discussions、PR 等等。
@ -132,7 +132,7 @@
## 提問 { #ask-questions } ## 提問 { #ask-questions }
你可以在 GitHub 儲存庫<a href="https://github.com/fastapi/fastapi/discussions/new?category=questions" class="external-link" target="_blank">建立一個新的問題(Question)</a>,例如用來: 你可以在 GitHub 儲存庫[建立一個新的問題(Question)](https://github.com/fastapi/fastapi/discussions/new?category=questions),例如用來:
* 提出**問題**或詢問某個**疑難**。 * 提出**問題**或詢問某個**疑難**。
* 建議一個新的**功能**。 * 建議一個新的**功能**。
@ -195,12 +195,12 @@
## 建立 Pull Request { #create-a-pull-request } ## 建立 Pull Request { #create-a-pull-request }
你可以透過 Pull Request 來[貢獻](contributing.md){.internal-link target=_blank}原始碼,例如: 你可以透過 Pull Request 來[貢獻](contributing.md)原始碼,例如:
* 修正文檔中你發現的錯字。 * 修正文檔中你發現的錯字。
* 分享你建立或發現的 FastAPI 相關文章、影片或 podcast,方法是<a href="https://github.com/fastapi/fastapi/edit/master/docs/en/data/external_links.yml" class="external-link" target="_blank">編輯這個檔案</a> * 分享你建立或發現的 FastAPI 相關文章、影片或 podcast,方法是[編輯這個檔案](https://github.com/fastapi/fastapi/edit/master/docs/en/data/external_links.yml)
* 請確保把你的連結加到對應章節的開頭。 * 請確保把你的連結加到對應章節的開頭。
* 協助把[文件翻譯](contributing.md#translations){.internal-link target=_blank}成你的語言。 * 協助把[文件翻譯](contributing.md#translations)成你的語言。
* 你也可以幫忙審查他人提交的翻譯。 * 你也可以幫忙審查他人提交的翻譯。
* 提議新的文件章節。 * 提議新的文件章節。
* 修復既有的 issue/bug。 * 修復既有的 issue/bug。
@ -217,8 +217,8 @@
你現在就能做的主要任務有: 你現在就能做的主要任務有:
* [在 GitHub 幫助他人解答問題](#help-others-with-questions-in-github){.internal-link target=_blank}(見上方章節)。 * [在 GitHub 幫助他人解答問題](#help-others-with-questions-in-github)(見上方章節)。
* [審核 Pull Request](#review-pull-requests){.internal-link target=_blank}(見上方章節)。 * [審核 Pull Request](#review-pull-requests)(見上方章節)。
這兩件事是**最耗時**的。這也是維護 FastAPI 的主要工作。 這兩件事是**最耗時**的。這也是維護 FastAPI 的主要工作。
@ -226,11 +226,11 @@
## 加入聊天室 { #join-the-chat } ## 加入聊天室 { #join-the-chat }
加入 👥 <a href="https://discord.gg/VQjSZaeJmf" class="external-link" target="_blank">Discord 聊天伺服器</a> 👥,與 FastAPI 社群的其他人一起交流。 加入 👥 [Discord 聊天伺服器](https://discord.gg/VQjSZaeJmf) 👥,與 FastAPI 社群的其他人一起交流。
/// tip /// tip
若要提問,請到 <a href="https://github.com/fastapi/fastapi/discussions/new?category=questions" class="external-link" target="_blank">GitHub Discussions</a>,在那裡更有機會獲得[FastAPI 專家](fastapi-people.md#fastapi-experts){.internal-link target=_blank}的協助。 若要提問,請到 [GitHub Discussions](https://github.com/fastapi/fastapi/discussions/new?category=questions),在那裡更有機會獲得[FastAPI 專家](fastapi-people.md#fastapi-experts)的協助。
聊天室請僅用於其他一般性的交流。 聊天室請僅用於其他一般性的交流。
@ -242,13 +242,13 @@
在 GitHub 上,模板會引導你寫出合適的提問,讓你更容易得到好的解答,甚至在提問前就自己解決問題。而且在 GitHub 上,我能確保最終都會回覆(即使需要一些時間)。我個人無法在聊天系統做到這一點。😅 在 GitHub 上,模板會引導你寫出合適的提問,讓你更容易得到好的解答,甚至在提問前就自己解決問題。而且在 GitHub 上,我能確保最終都會回覆(即使需要一些時間)。我個人無法在聊天系統做到這一點。😅
聊天系統中的對話也不像 GitHub 那樣容易被搜尋,因此問題與答案可能在對話中淹沒。而且只有 GitHub 上的問題與回答才會被計入成為[FastAPI 專家](fastapi-people.md#fastapi-experts){.internal-link target=_blank},因此你在 GitHub 上更有機會獲得關注。 聊天系統中的對話也不像 GitHub 那樣容易被搜尋,因此問題與答案可能在對話中淹沒。而且只有 GitHub 上的問題與回答才會被計入成為[FastAPI 專家](fastapi-people.md#fastapi-experts),因此你在 GitHub 上更有機會獲得關注。
另一方面,聊天室裡有成千上萬的使用者,所以幾乎隨時都有很大的機會能找到人聊天。😄 另一方面,聊天室裡有成千上萬的使用者,所以幾乎隨時都有很大的機會能找到人聊天。😄
## 贊助作者 { #sponsor-the-author } ## 贊助作者 { #sponsor-the-author }
如果你的**產品/公司**依賴或與 **FastAPI** 相關,且你想觸及它的使用者,你可以透過 <a href="https://github.com/sponsors/tiangolo" class="external-link" target="_blank">GitHub sponsors</a> 贊助作者(我)。依據不同級別,你可能會得到一些額外福利,例如在文件中顯示徽章。🎁 如果你的**產品/公司**依賴或與 **FastAPI** 相關,且你想觸及它的使用者,你可以透過 [GitHub sponsors](https://github.com/sponsors/tiangolo) 贊助作者(我)。依據不同級別,你可能會得到一些額外福利,例如在文件中顯示徽章。🎁
--- ---

14
docs/zh-hant/docs/history-design-future.md

@ -1,6 +1,6 @@
# 歷史、設計與未來 { #history-design-and-future } # 歷史、設計與未來 { #history-design-and-future }
不久之前,<a href="https://github.com/fastapi/fastapi/issues/3#issuecomment-454956920" class="external-link" target="_blank">一位 **FastAPI** 使用者提問</a> 不久之前,[一位 **FastAPI** 使用者提問](https://github.com/fastapi/fastapi/issues/3#issuecomment-454956920)
> 這個專案的歷史是什麼?看起來它在短短幾週內從默默無名變得非常厲害 [...] > 這個專案的歷史是什麼?看起來它在短短幾週內從默默無名變得非常厲害 [...]
@ -14,7 +14,7 @@
**FastAPI** 的歷史,在很大程度上也是其前身工具的歷史。 **FastAPI** 的歷史,在很大程度上也是其前身工具的歷史。
如在[替代方案](alternatives.md){.internal-link target=_blank}章節所述: 如在[替代方案](alternatives.md)節所述:
<blockquote markdown="1"> <blockquote markdown="1">
@ -44,9 +44,9 @@
我在最受歡迎的 Python 編輯器中測試了多個想法:PyCharm、VS Code、基於 Jedi 的編輯器。 我在最受歡迎的 Python 編輯器中測試了多個想法:PyCharm、VS Code、基於 Jedi 的編輯器。
根據最新的 <a href="https://www.jetbrains.com/research/python-developers-survey-2018/#development-tools" class="external-link" target="_blank">Python 開發者調查</a>,這些工具涵蓋約 80% 的使用者。 根據最新的 [Python 開發者調查](https://www.jetbrains.com/research/python-developers-survey-2018/#development-tools),這些工具涵蓋約 80% 的使用者。
這表示 **FastAPI** 已針對 80% 的 Python 開發者所使用的編輯器進行過專門測試。而由於其他多數編輯器的行為也類似,這些優點幾乎所有編輯器上都能生效 這表示 **FastAPI** 已針對 80% 的 Python 開發者所使用的編輯器進行過專門測試。而由於其他多數編輯器的行為也類似,這些優點擴及實際上幾乎所有編輯器。
藉此我找到了盡可能減少程式碼重複、在各處提供自動補全、型別與錯誤檢查等的最佳方式。 藉此我找到了盡可能減少程式碼重複、在各處提供自動補全、型別與錯誤檢查等的最佳方式。
@ -54,11 +54,11 @@
## 需求 { #requirements } ## 需求 { #requirements }
在測試多種替代方案後,我決定採用 <a href="https://docs.pydantic.dev/" class="external-link" target="_blank">**Pydantic**</a>,因為它的優勢。 在測試多種替代方案後,我決定採用 [**Pydantic**](https://docs.pydantic.dev/),因為它的優勢。
隨後我也對它做出貢獻,使其完全符合 JSON Schema、支援以不同方式定義約束,並依據在多款編輯器中的測試結果改進編輯器支援(型別檢查、自動補全)。 隨後我也對它做出貢獻,使其完全符合 JSON Schema、支援以不同方式定義約束,並依據在多款編輯器中的測試結果改進編輯器支援(型別檢查、自動補全)。
在開發過程中,我也對 <a href="https://www.starlette.dev/" class="external-link" target="_blank">**Starlette**</a>(另一個關鍵依賴)做出貢獻。 在開發過程中,我也對 [**Starlette**](https://www.starlette.dev/)(另一個關鍵依賴)做出貢獻。
## 開發 { #development } ## 開發 { #development }
@ -76,4 +76,4 @@
**FastAPI** 的前景非常光明。 **FastAPI** 的前景非常光明。
也非常感謝[你的幫助](help-fastapi.md){.internal-link target=_blank} 也非常感謝[你的幫助](help-fastapi.md)。

122
docs/zh-hant/docs/index.md

@ -11,25 +11,25 @@
<em>FastAPI 框架,高效能,易於學習,快速開發,適用於生產環境</em> <em>FastAPI 框架,高效能,易於學習,快速開發,適用於生產環境</em>
</p> </p>
<p align="center"> <p align="center">
<a href="https://github.com/fastapi/fastapi/actions?query=workflow%3ATest+event%3Apush+branch%3Amaster" target="_blank"> <a href="https://github.com/fastapi/fastapi/actions?query=workflow%3ATest+event%3Apush+branch%3Amaster">
<img src="https://github.com/fastapi/fastapi/actions/workflows/test.yml/badge.svg?event=push&branch=master" alt="Test"> <img src="https://github.com/fastapi/fastapi/actions/workflows/test.yml/badge.svg?event=push&branch=master" alt="Test">
</a> </a>
<a href="https://coverage-badge.samuelcolvin.workers.dev/redirect/fastapi/fastapi" target="_blank"> <a href="https://coverage-badge.samuelcolvin.workers.dev/redirect/fastapi/fastapi">
<img src="https://coverage-badge.samuelcolvin.workers.dev/fastapi/fastapi.svg" alt="Coverage"> <img src="https://coverage-badge.samuelcolvin.workers.dev/fastapi/fastapi.svg" alt="Coverage">
</a> </a>
<a href="https://pypi.org/project/fastapi" target="_blank"> <a href="https://pypi.org/project/fastapi">
<img src="https://img.shields.io/pypi/v/fastapi?color=%2334D058&label=pypi%20package" alt="Package version"> <img src="https://img.shields.io/pypi/v/fastapi?color=%2334D058&label=pypi%20package" alt="Package version">
</a> </a>
<a href="https://pypi.org/project/fastapi" target="_blank"> <a href="https://pypi.org/project/fastapi">
<img src="https://img.shields.io/pypi/pyversions/fastapi.svg?color=%2334D058" alt="Supported Python versions"> <img src="https://img.shields.io/pypi/pyversions/fastapi.svg?color=%2334D058" alt="Supported Python versions">
</a> </a>
</p> </p>
--- ---
**文件**: <a href="https://fastapi.tiangolo.com/zh-hant" target="_blank">https://fastapi.tiangolo.com/zh-hant</a> **文件**: [https://fastapi.tiangolo.com/zh-hant](https://fastapi.tiangolo.com/zh-hant)
**程式碼**: <a href="https://github.com/fastapi/fastapi" target="_blank">https://github.com/fastapi/fastapi</a> **程式碼**: [https://github.com/fastapi/fastapi](https://github.com/fastapi/fastapi)
--- ---
@ -44,7 +44,7 @@ FastAPI 是一個現代、快速(高效能)的 Web 框架,用於以 Python
* **簡單**:設計上易於使用與學習。更少的讀文件時間。 * **簡單**:設計上易於使用與學習。更少的讀文件時間。
* **簡潔**:最小化程式碼重複性。每個參數宣告可帶來多個功能。更少的錯誤。 * **簡潔**:最小化程式碼重複性。每個參數宣告可帶來多個功能。更少的錯誤。
* **穩健**:立即獲得可投入生產的程式碼,並自動生成互動式文件。 * **穩健**:立即獲得可投入生產的程式碼,並自動生成互動式文件。
* **標準化**:基於(且完全相容於)API 的開放標準:<a href="https://github.com/OAI/OpenAPI-Specification" class="external-link" target="_blank">OpenAPI</a>(之前稱為 Swagger)和 <a href="https://json-schema.org/" class="external-link" target="_blank">JSON Schema</a> * **標準化**:基於(且完全相容於)API 的開放標準:[OpenAPI](https://github.com/OAI/OpenAPI-Specification)(之前稱為 Swagger)和 [JSON Schema](https://json-schema.org/)
<small>* 基於內部開發團隊在建立生產應用程式時的測試預估。</small> <small>* 基於內部開發團隊在建立生產應用程式時的測試預估。</small>
@ -55,51 +55,51 @@ FastAPI 是一個現代、快速(高效能)的 Web 框架,用於以 Python
### 基石贊助商 { #keystone-sponsor } ### 基石贊助商 { #keystone-sponsor }
{% for sponsor in sponsors.keystone -%} {% for sponsor in sponsors.keystone -%}
<a href="{{ sponsor.url }}" target="_blank" title="{{ sponsor.title }}"><img src="{{ sponsor.img }}" style="border-radius:15px"></a> <a href="{{ sponsor.url }}" title="{{ sponsor.title }}"><img src="{{ sponsor.img }}" style="border-radius:15px"></a>
{% endfor -%} {% endfor -%}
### 金級與銀級贊助商 { #gold-and-silver-sponsors } ### 金級與銀級贊助商 { #gold-and-silver-sponsors }
{% for sponsor in sponsors.gold -%} {% for sponsor in sponsors.gold -%}
<a href="{{ sponsor.url }}" target="_blank" title="{{ sponsor.title }}"><img src="{{ sponsor.img }}" style="border-radius:15px"></a> <a href="{{ sponsor.url }}" title="{{ sponsor.title }}"><img src="{{ sponsor.img }}" style="border-radius:15px"></a>
{% endfor -%} {% endfor -%}
{%- for sponsor in sponsors.silver -%} {%- for sponsor in sponsors.silver -%}
<a href="{{ sponsor.url }}" target="_blank" title="{{ sponsor.title }}"><img src="{{ sponsor.img }}" style="border-radius:15px"></a> <a href="{{ sponsor.url }}" title="{{ sponsor.title }}"><img src="{{ sponsor.img }}" style="border-radius:15px"></a>
{% endfor %} {% endfor %}
<!-- /sponsors --> <!-- /sponsors -->
<a href="https://fastapi.tiangolo.com/zh-hant/fastapi-people/#sponsors" class="external-link" target="_blank">其他贊助商</a> [其他贊助商](https://fastapi.tiangolo.com/zh-hant/fastapi-people/#sponsors)
## 評價 { #opinions } ## 評價 { #opinions }
"_[...] 近期大量使用 **FastAPI**。[...] 我實際上打算在我在**微軟**團隊的所有**機器學習**服務上使用它。其中一些正在整合到核心的 **Windows** 產品,以及一些 **Office** 產品。_" "_[...] 近期大量使用 **FastAPI**。[...] 我實際上打算在我在**微軟**團隊的所有**機器學習**服務上使用它。其中一些正在整合到核心的 **Windows** 產品,以及一些 **Office** 產品。_"
<div style="text-align: right; margin-right: 10%;">Kabir Khan - <strong>Microsoft</strong> <a href="https://github.com/fastapi/fastapi/pull/26" target="_blank"><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>(ref)</small></a></div>
--- ---
"_我們採用了 **FastAPI** 函式庫來啟動一個 **REST** 伺服器,供查詢以取得**預測**。[for Ludwig]_" "_我們採用了 **FastAPI** 函式庫來啟動一個 **REST** 伺服器,供查詢以取得**預測**。[for Ludwig]_"
<div style="text-align: right; margin-right: 10%;">Piero Molino, Yaroslav Dudin, and Sai Sumanth Miryala - <strong>Uber</strong> <a href="https://eng.uber.com/ludwig-v0-2/" target="_blank"><small>(ref)</small></a></div> <div style="text-align: right; margin-right: 10%;">Piero Molino, Yaroslav Dudin, and Sai Sumanth Miryala - <strong>Uber</strong> <a href="https://eng.uber.com/ludwig-v0-2/"><small>(ref)</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" target="_blank"><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>(ref)</small></a></div>
--- ---
"_我對 **FastAPI** 興奮得不得了。超好玩!_" "_我對 **FastAPI** 興奮得不得了。超好玩!_"
<div style="text-align: right; margin-right: 10%;">Brian Okken - <strong><a href="https://pythonbytes.fm/episodes/show/123/time-to-right-the-py-wrongs?time_in_sec=855" target="_blank">Python Bytes</a> podcast 主持人</strong> <a href="https://x.com/brianokken/status/1112220079972728832" target="_blank"><small>(ref)</small></a></div> <div style="text-align: right; margin-right: 10%;">Brian Okken - <strong>[Python Bytes](https://pythonbytes.fm/episodes/show/123/time-to-right-the-py-wrongs?time_in_sec=855) podcast 主持人</strong> <a href="https://x.com/brianokken/status/1112220079972728832"><small>(ref)</small></a></div>
--- ---
"_老實說,你們做的看起來非常穩健又精緻。很多方面都正是我希望 **Hug** 成為的樣子——看到有人把它建出來真的很鼓舞人心。_" "_老實說,你們做的看起來非常穩健又精緻。很多方面都正是我希望 **Hug** 成為的樣子——看到有人把它建出來真的很鼓舞人心。_"
<div style="text-align: right; margin-right: 10%;">Timothy Crosley - <strong><a href="https://github.com/hugapi/hug" target="_blank">Hug</a> 創作者</strong> <a href="https://news.ycombinator.com/item?id=19455465" target="_blank"><small>(ref)</small></a></div> <div style="text-align: right; margin-right: 10%;">Timothy Crosley - <strong>[Hug](https://github.com/hugapi/hug) 創作者</strong> <a href="https://news.ycombinator.com/item?id=19455465"><small>(ref)</small></a></div>
--- ---
@ -107,27 +107,27 @@ FastAPI 是一個現代、快速(高效能)的 Web 框架,用於以 Python
"_我們的 **API** 已經改用 **FastAPI** [...] 我想你會喜歡它 [...]_" "_我們的 **API** 已經改用 **FastAPI** [...] 我想你會喜歡它 [...]_"
<div style="text-align: right; margin-right: 10%;">Ines Montani - Matthew Honnibal - <strong><a href="https://explosion.ai" target="_blank">Explosion AI</a> 創辦人 - <a href="https://spacy.io" target="_blank">spaCy</a> 創作者</strong> <a href="https://x.com/_inesmontani/status/1144173225322143744" target="_blank"><small>(ref)</small></a> - <a href="https://x.com/honnibal/status/1144031421859655680" target="_blank"><small>(ref)</small></a></div> <div style="text-align: right; margin-right: 10%;">Ines Montani - Matthew Honnibal - <strong>[Explosion AI](https://explosion.ai) 創辦人 - [spaCy](https://spacy.io) 創作者</strong> <a href="https://x.com/_inesmontani/status/1144173225322143744"><small>(ref)</small></a> - <a href="https://x.com/honnibal/status/1144031421859655680"><small>(ref)</small></a></div>
--- ---
"_如果有人想要建立一個可投入生產的 Python API,我強烈推薦 **FastAPI**。它**設計精美**、**使用簡單**且**高度可擴充**,已成為我們 API 優先開發策略中的**關鍵組件**,推動了許多自動化與服務,例如我們的 Virtual TAC Engineer。_" "_如果有人想要打造一個可用於生產環境的 Python API,我強力推薦 **FastAPI**。它**設計優雅**、**簡單易用**且**高度可擴展**,已經成為我們 API first 開發策略中的**關鍵元件**,推動了許多自動化與服務,例如我們的 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/" target="_blank"><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>(ref)</small></a></div>
--- ---
## FastAPI 迷你紀錄片 { #fastapi-mini-documentary } ## FastAPI 迷你紀錄片 { #fastapi-mini-documentary }
在 2025 年底發布了一支 <a href="https://www.youtube.com/watch?v=mpR8ngthqiE" class="external-link" target="_blank">FastAPI 迷你紀錄片</a>,你可以在線上觀看: 在 2025 年底發布了一支 [FastAPI 迷你紀錄片](https://www.youtube.com/watch?v=mpR8ngthqiE),你可以在線上觀看:
<a href="https://www.youtube.com/watch?v=mpR8ngthqiE" target="_blank"><img src="https://fastapi.tiangolo.com/img/fastapi-documentary.jpg" alt="FastAPI Mini Documentary"></a> <a href="https://www.youtube.com/watch?v=mpR8ngthqiE"><img src="https://fastapi.tiangolo.com/img/fastapi-documentary.jpg" alt="FastAPI Mini Documentary"></a>
## **Typer**,命令列的 FastAPI { #typer-the-fastapi-of-clis } ## **Typer**,命令列的 FastAPI { #typer-the-fastapi-of-clis }
<a href="https://typer.tiangolo.com" target="_blank"><img src="https://typer.tiangolo.com/img/logo-margin/logo-margin-vector.svg" style="width: 20%;"></a> <a href="https://typer.tiangolo.com"><img src="https://typer.tiangolo.com/img/logo-margin/logo-margin-vector.svg" style="width: 20%;"></a>
如果你不是在做 Web API,而是要建立一個在終端機中使用的 <abbr title="Command Line Interface - 命令列介面">CLI</abbr> 應用程式,可以看看 <a href="https://typer.tiangolo.com/" class="external-link" target="_blank">**Typer**</a> 如果你不是在做 Web API,而是要建立一個在終端機中使用的 <abbr title="Command Line Interface - 命令列介面">CLI</abbr> 應用程式,可以看看 [**Typer**](https://typer.tiangolo.com/)
**Typer** 是 FastAPI 的小老弟。他立志成為命令列世界的 **FastAPI**。⌨️ 🚀 **Typer** 是 FastAPI 的小老弟。他立志成為命令列世界的 **FastAPI**。⌨️ 🚀
@ -135,12 +135,12 @@ FastAPI 是一個現代、快速(高效能)的 Web 框架,用於以 Python
FastAPI 是站在以下巨人的肩膀上: FastAPI 是站在以下巨人的肩膀上:
* <a href="https://www.starlette.dev/" class="external-link" target="_blank">Starlette</a> 負責 Web 部分。 * [Starlette](https://www.starlette.dev/) 負責 Web 部分。
* <a href="https://docs.pydantic.dev/" class="external-link" target="_blank">Pydantic</a> 負責資料部分。 * [Pydantic](https://docs.pydantic.dev/) 負責資料部分。
## 安裝 { #installation } ## 安裝 { #installation }
建立並啟用一個<a href="https://fastapi.tiangolo.com/zh-hant/virtual-environments/" class="external-link" target="_blank">虛擬環境</a>,然後安裝 FastAPI: 建立並啟用一個[虛擬環境](https://fastapi.tiangolo.com/zh-hant/virtual-environments/),然後安裝 FastAPI:
<div class="termy"> <div class="termy">
@ -199,7 +199,7 @@ async def read_item(item_id: int, q: str | None = None):
**注意**: **注意**:
如果你不確定,請查看文件中 _"In a hurry?"_ 章節的 <a href="https://fastapi.tiangolo.com/zh-hant/async/#in-a-hurry" target="_blank">`async` 與 `await`</a> 如果你不確定,請查看文件中 _"In a hurry?"_ 章節的[關於文件中的 `async``await`](https://fastapi.tiangolo.com/zh-hant/async/#in-a-hurry)
</details> </details>
@ -210,7 +210,7 @@ async def read_item(item_id: int, q: str | None = None):
<div class="termy"> <div class="termy">
```console ```console
$ fastapi dev main.py $ fastapi dev
╭────────── FastAPI CLI - Development mode ───────────╮ ╭────────── FastAPI CLI - Development mode ───────────╮
│ │ │ │
@ -235,19 +235,19 @@ INFO: Application startup complete.
</div> </div>
<details markdown="1"> <details markdown="1">
<summary>關於指令 <code>fastapi dev main.py</code>...</summary> <summary>關於指令 <code>fastapi dev</code>...</summary>
指令 `fastapi dev` 會讀取你的 `main.py`,偵測其中的 **FastAPI** 應用,並使用 <a href="https://www.uvicorn.dev" class="external-link" target="_blank">Uvicorn</a> 啟動伺服器。 指令 `fastapi dev` 會讀取你的 `main.py`,偵測其中的 **FastAPI** 應用,並使用 [Uvicorn](https://www.uvicorn.dev) 啟動伺服器。
預設情況下,`fastapi dev` 會在本機開發時啟用自動重新載入。 預設情況下,`fastapi dev` 會在本機開發時啟用自動重新載入。
可在 <a href="https://fastapi.tiangolo.com/zh-hant/fastapi-cli/" target="_blank">FastAPI CLI 文件</a>中閱讀更多資訊。 可在 [FastAPI CLI 文件](https://fastapi.tiangolo.com/zh-hant/fastapi-cli/)中閱讀更多資訊。
</details> </details>
### 檢查 { #check-it } ### 檢查 { #check-it }
使用瀏覽器開啟 <a href="http://127.0.0.1:8000/items/5?q=somequery" class="external-link" target="_blank">http://127.0.0.1:8000/items/5?q=somequery</a> 使用瀏覽器開啟 [http://127.0.0.1:8000/items/5?q=somequery](http://127.0.0.1:8000/items/5?q=somequery)
你將會看到以下 JSON 回應: 你將會看到以下 JSON 回應:
@ -264,17 +264,17 @@ INFO: Application startup complete.
### 互動式 API 文件 { #interactive-api-docs } ### 互動式 API 文件 { #interactive-api-docs }
接著前往 <a href="http://127.0.0.1:8000/docs" class="external-link" target="_blank">http://127.0.0.1:8000/docs</a> 接著前往 [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs)
你會看到自動生成的互動式 API 文件(由 <a href="https://github.com/swagger-api/swagger-ui" class="external-link" target="_blank">Swagger UI</a> 提供): 你會看到自動生成的互動式 API 文件(由 [Swagger UI](https://github.com/swagger-api/swagger-ui) 提供):
![Swagger UI](https://fastapi.tiangolo.com/img/index/index-01-swagger-ui-simple.png) ![Swagger UI](https://fastapi.tiangolo.com/img/index/index-01-swagger-ui-simple.png)
### 替代 API 文件 { #alternative-api-docs } ### 替代 API 文件 { #alternative-api-docs }
現在前往 <a href="http://127.0.0.1:8000/redoc" class="external-link" target="_blank">http://127.0.0.1:8000/redoc</a> 現在前往 [http://127.0.0.1:8000/redoc](http://127.0.0.1:8000/redoc)
你會看到另一種自動文件(由 <a href="https://github.com/Rebilly/ReDoc" class="external-link" target="_blank">ReDoc</a> 提供): 你會看到另一種自動文件(由 [ReDoc](https://github.com/Rebilly/ReDoc) 提供):
![ReDoc](https://fastapi.tiangolo.com/img/index/index-02-redoc-simple.png) ![ReDoc](https://fastapi.tiangolo.com/img/index/index-02-redoc-simple.png)
@ -316,7 +316,7 @@ def update_item(item_id: int, item: Item):
### 互動式 API 文件升級 { #interactive-api-docs-upgrade } ### 互動式 API 文件升級 { #interactive-api-docs-upgrade }
前往 <a href="http://127.0.0.1:8000/docs" class="external-link" target="_blank">http://127.0.0.1:8000/docs</a> 前往 [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs)
* 互動式 API 文件會自動更新,包含新的 body: * 互動式 API 文件會自動更新,包含新的 body:
@ -332,7 +332,7 @@ def update_item(item_id: int, item: Item):
### 替代 API 文件升級 { #alternative-api-docs-upgrade } ### 替代 API 文件升級 { #alternative-api-docs-upgrade }
現在前往 <a href="http://127.0.0.1:8000/redoc" class="external-link" target="_blank">http://127.0.0.1:8000/redoc</a> 現在前往 [http://127.0.0.1:8000/redoc](http://127.0.0.1:8000/redoc)
* 替代文件也會反映新的查詢參數與 body: * 替代文件也會反映新的查詢參數與 body:
@ -433,7 +433,7 @@ item: Item
![editor support](https://fastapi.tiangolo.com/img/vscode-completion.png) ![editor support](https://fastapi.tiangolo.com/img/vscode-completion.png)
若想看包含更多功能的完整範例,請參考 <a href="https://fastapi.tiangolo.com/zh-hant/tutorial/">教學 - 使用者指南</a> 若想看包含更多功能的完整範例,請參考 <a href="https://fastapi.tiangolo.com/zh-hant/tutorial/">Tutorial - User Guide</a>
**劇透警告**:教學 - 使用者指南包含: **劇透警告**:教學 - 使用者指南包含:
@ -442,7 +442,7 @@ item: Item
* 一個非常強大且易用的 **<dfn title="也稱為:components、resources、providers、services、injectables">依賴注入</dfn>** 系統。 * 一個非常強大且易用的 **<dfn title="也稱為:components、resources、providers、services、injectables">依賴注入</dfn>** 系統。
* 安全與驗證,包含支援 **OAuth2** 搭配 **JWT tokens****HTTP Basic** 驗證。 * 安全與驗證,包含支援 **OAuth2** 搭配 **JWT tokens****HTTP Basic** 驗證。
* 宣告**深度巢狀 JSON 模型**的進階(但同樣簡單)技巧(感謝 Pydantic)。 * 宣告**深度巢狀 JSON 模型**的進階(但同樣簡單)技巧(感謝 Pydantic)。
* 與 <a href="https://strawberry.rocks" class="external-link" target="_blank">Strawberry</a> 及其他函式庫的 **GraphQL** 整合。 * 與 [Strawberry](https://strawberry.rocks) 及其他函式庫的 **GraphQL** 整合。
* 許多額外功能(感謝 Starlette),例如: * 許多額外功能(感謝 Starlette),例如:
* **WebSockets** * **WebSockets**
* 基於 HTTPX 與 `pytest` 的極其簡單的測試 * 基於 HTTPX 與 `pytest` 的極其簡單的測試
@ -452,24 +452,10 @@ item: Item
### 部署你的應用(可選) { #deploy-your-app-optional } ### 部署你的應用(可選) { #deploy-your-app-optional }
你也可以選擇將 FastAPI 應用部署到 <a href="https://fastapicloud.com" class="external-link" target="_blank">FastAPI Cloud</a>,如果你還沒加入,去登記等候名單吧。🚀 你也可以選擇將 FastAPI 應用部署到 [FastAPI Cloud](https://fastapicloud.com),如果你還沒加入,去登記等候名單吧。🚀
如果你已經有 **FastAPI Cloud** 帳號(我們已從等候名單邀請你 😉),你可以用一個指令部署你的應用。 如果你已經有 **FastAPI Cloud** 帳號(我們已從等候名單邀請你 😉),你可以用一個指令部署你的應用。
部署前,先確認你已登入:
<div class="termy">
```console
$ fastapi login
You are logged in to FastAPI Cloud 🚀
```
</div>
接著部署你的應用:
<div class="termy"> <div class="termy">
```console ```console
@ -488,7 +474,7 @@ Deploying to FastAPI Cloud...
#### 關於 FastAPI Cloud { #about-fastapi-cloud } #### 關於 FastAPI Cloud { #about-fastapi-cloud }
**<a href="https://fastapicloud.com" class="external-link" target="_blank">FastAPI Cloud</a>** 由 **FastAPI** 的同一位作者與團隊打造。 **[FastAPI Cloud](https://fastapicloud.com)** 由 **FastAPI** 的同一位作者與團隊打造。
它讓你以最小的努力精簡地完成 API 的**建置**、**部署**與**存取**流程。 它讓你以最小的努力精簡地完成 API 的**建置**、**部署**與**存取**流程。
@ -504,9 +490,9 @@ FastAPI 是開源且基於標準。你可以把 FastAPI 應用部署到任何你
## 效能 { #performance } ## 效能 { #performance }
獨立的 TechEmpower 基準測試顯示,在 Uvicorn 下運行的 **FastAPI** 應用是<a href="https://www.techempower.com/benchmarks/#section=test&runid=7464e520-0dc2-473d-bd34-dbdfd7e85911&hw=ph&test=query&l=zijzen-7" class="external-link" target="_blank">最快的 Python 框架之一</a>,僅次於 Starlette 與 Uvicorn 本身(FastAPI 內部使用它們)。(*) 獨立的 TechEmpower 基準測試顯示,在 Uvicorn 下運行的 **FastAPI** 應用是[最快的 Python 框架之一](https://www.techempower.com/benchmarks/#section=test&runid=7464e520-0dc2-473d-bd34-dbdfd7e85911&hw=ph&test=query&l=zijzen-7),僅次於 Starlette 與 Uvicorn 本身(FastAPI 內部使用它們)。(*)
想了解更多,請參閱<a href="https://fastapi.tiangolo.com/zh-hant/benchmarks/" class="internal-link" target="_blank">測試結果</a> 想了解更多,請參閱[測試結果](https://fastapi.tiangolo.com/zh-hant/benchmarks/)
## 依賴套件 { #dependencies } ## 依賴套件 { #dependencies }
@ -518,19 +504,19 @@ FastAPI 依賴 Pydantic 與 Starlette。
Pydantic 會使用: Pydantic 會使用:
* <a href="https://github.com/JoshData/python-email-validator" target="_blank"><code>email-validator</code></a> - 用於電子郵件驗證。 * [`email-validator`](https://github.com/JoshData/python-email-validator) - 用於電子郵件驗證。
Starlette 會使用: Starlette 會使用:
* <a href="https://www.python-httpx.org" target="_blank"><code>httpx</code></a> - 若要使用 `TestClient` 必須安裝。 * [`httpx`](https://www.python-httpx.org) - 若要使用 `TestClient` 必須安裝。
* <a href="https://jinja.palletsprojects.com" target="_blank"><code>jinja2</code></a> - 若要使用預設的模板設定必須安裝。 * [`jinja2`](https://jinja.palletsprojects.com) - 若要使用預設的模板設定必須安裝。
* <a href="https://github.com/Kludex/python-multipart" target="_blank"><code>python-multipart</code></a> - 若要支援表單 <dfn title="將來自 HTTP 請求的字串轉換為 Python 資料">"解析"</dfn>,搭配 `request.form()` * [`python-multipart`](https://github.com/Kludex/python-multipart) - 若要支援表單 <dfn title="將來自 HTTP 請求的字串轉換為 Python 資料">"解析"</dfn>,搭配 `request.form()`
FastAPI 會使用: FastAPI 會使用:
* <a href="https://www.uvicorn.dev" target="_blank"><code>uvicorn</code></a> - 用於載入並服務你的應用的伺服器。這包含 `uvicorn[standard]`,其中含有一些高效能服務所需的依賴(例如 `uvloop`)。 * [`uvicorn`](https://www.uvicorn.dev) - 用於載入並服務你的應用的伺服器。這包含 `uvicorn[standard]`,其中含有一些高效能服務所需的依賴(例如 `uvloop`)。
* `fastapi-cli[standard]` - 提供 `fastapi` 指令。 * `fastapi-cli[standard]` - 提供 `fastapi` 指令。
* 其中包含 `fastapi-cloud-cli`,可讓你將 FastAPI 應用部署到 <a href="https://fastapicloud.com" class="external-link" target="_blank">FastAPI Cloud</a> * 其中包含 `fastapi-cloud-cli`,可讓你將 FastAPI 應用部署到 [FastAPI Cloud](https://fastapicloud.com)
### 不含 `standard` 依賴套件 { #without-standard-dependencies } ### 不含 `standard` 依賴套件 { #without-standard-dependencies }
@ -546,13 +532,13 @@ FastAPI 會使用:
Pydantic 的額外可選依賴: Pydantic 的額外可選依賴:
* <a href="https://docs.pydantic.dev/latest/usage/pydantic_settings/" target="_blank"><code>pydantic-settings</code></a> - 設定管理。 * [`pydantic-settings`](https://docs.pydantic.dev/latest/usage/pydantic_settings/) - 設定管理。
* <a href="https://docs.pydantic.dev/latest/usage/types/extra_types/extra_types/" target="_blank"><code>pydantic-extra-types</code></a> - 與 Pydantic 一起使用的額外型別。 * [`pydantic-extra-types`](https://docs.pydantic.dev/latest/usage/types/extra_types/extra_types/) - 與 Pydantic 一起使用的額外型別。
FastAPI 的額外可選依賴: FastAPI 的額外可選依賴:
* <a href="https://github.com/ijl/orjson" target="_blank"><code>orjson</code></a> - 若要使用 `ORJSONResponse` 必須安裝。 * [`orjson`](https://github.com/ijl/orjson) - 若要使用 `ORJSONResponse` 必須安裝。
* <a href="https://github.com/esnme/ultrajson" target="_blank"><code>ujson</code></a> - 若要使用 `UJSONResponse` 必須安裝。 * [`ujson`](https://github.com/esnme/ultrajson) - 若要使用 `UJSONResponse` 必須安裝。
## 授權 { #license } ## 授權 { #license }

2
docs/zh-hant/docs/project-generation.md

@ -4,7 +4,7 @@
你可以使用此範本快速起步,裡面已替你完成大量初始設定、安全性、資料庫,以及部分 API 端點。 你可以使用此範本快速起步,裡面已替你完成大量初始設定、安全性、資料庫,以及部分 API 端點。
GitHub 儲存庫:<a href="https://github.com/tiangolo/full-stack-fastapi-template" class="external-link" target="_blank">全端 FastAPI 範本</a> GitHub 儲存庫:[全端 FastAPI 範本](https://github.com/tiangolo/full-stack-fastapi-template)
## 全端 FastAPI 範本 - 技術堆疊與功能 { #full-stack-fastapi-template-technology-stack-and-features } ## 全端 FastAPI 範本 - 技術堆疊與功能 { #full-stack-fastapi-template-technology-stack-and-features }

10
docs/zh-hant/docs/python-types.md

@ -269,7 +269,7 @@ def some_function(data: Any):
## Pydantic 模型 { #pydantic-models } ## Pydantic 模型 { #pydantic-models }
<a href="https://docs.pydantic.dev/" class="external-link" target="_blank">Pydantic</a> 是一個用來做資料驗證的 Python 程式庫。 [Pydantic](https://docs.pydantic.dev/) 是一個用來做資料驗證的 Python 程式庫。
你以帶有屬性的類別來宣告資料的「形狀」。 你以帶有屬性的類別來宣告資料的「形狀」。
@ -285,13 +285,13 @@ def some_function(data: Any):
/// info | 資訊 /// info | 資訊
想了解更多 <a href="https://docs.pydantic.dev/" class="external-link" target="_blank">Pydantic,請查看它的文件</a> 想了解更多 [Pydantic,請查看它的文件](https://docs.pydantic.dev/)
/// ///
**FastAPI** 完全是以 Pydantic 為基礎。 **FastAPI** 完全是以 Pydantic 為基礎。
你會在[教學 - 使用者指南](tutorial/index.md){.internal-link target=_blank}中看到更多實際範例。 你會在[教學 - 使用者指南](tutorial/index.md)中看到更多實際範例。
## 含中繼資料的型別提示 { #type-hints-with-metadata-annotations } ## 含中繼資料的型別提示 { #type-hints-with-metadata-annotations }
@ -337,12 +337,12 @@ Python 本身不會對這個 `Annotated` 做任何事。對編輯器與其他工
* 使用 OpenAPI 書寫 API 文件: * 使用 OpenAPI 書寫 API 文件:
* 之後會由自動的互動式文件介面所使用 * 之後會由自動的互動式文件介面所使用
這些現在聽起來可能有點抽象。別擔心。你會在[教學 - 使用者指南](tutorial/index.md){.internal-link target=_blank}中看到它們的實際運作。 這些現在聽起來可能有點抽象。別擔心。你會在[教學 - 使用者指南](tutorial/index.md)中看到它們的實際運作。
重點是,透過在單一位置使用標準的 Python 型別(而不是新增更多類別、裝飾器等),**FastAPI** 會幫你完成很多工作。 重點是,透過在單一位置使用標準的 Python 型別(而不是新增更多類別、裝飾器等),**FastAPI** 會幫你完成很多工作。
/// info | 資訊 /// info | 資訊
如果你已經完整讀完整個教學,並回來想多看一些關於型別的內容,一個不錯的資源是 <a href="https://mypy.readthedocs.io/en/latest/cheat_sheet_py3.html" class="external-link" target="_blank">`mypy` 的「小抄」</a> 如果你已經完整讀完整個教學,並回來想多看一些關於型別的內容,一個不錯的資源是 [`mypy` 的「小抄」](https://mypy.readthedocs.io/en/latest/cheat_sheet_py3.html)
/// ///

47
docs/zh-hant/docs/tutorial/bigger-applications.md

@ -123,7 +123,7 @@ from app.routers import items
為了簡化範例,我們使用了一個虛構的標頭。 為了簡化範例,我們使用了一個虛構的標頭。
但在真實情況下,使用內建的[安全工具](security/index.md){.internal-link target=_blank}會有更好的效果。 但在真實情況下,使用內建的 [安全工具](security/index.md) 會有更好的效果。
/// ///
@ -169,7 +169,7 @@ async def read_item(item_id: str):
/// tip | 提示 /// tip | 提示
請注意,就像在[路徑操作裝飾器中的相依性](dependencies/dependencies-in-path-operation-decorators.md){.internal-link target=_blank}一樣,不會把任何值傳遞給你的路徑操作函式(path operation function)。 請注意,就像在[路徑操作裝飾器中的相依性](dependencies/dependencies-in-path-operation-decorators.md)一樣,不會把任何值傳遞給你的路徑操作函式(path operation function)。
/// ///
@ -185,8 +185,8 @@ async def read_item(item_id: str):
* 它們都會包含預先定義的 `responses` * 它們都會包含預先定義的 `responses`
* 這些路徑操作都會在執行前評估 / 執行其 `dependencies` 清單。 * 這些路徑操作都會在執行前評估 / 執行其 `dependencies` 清單。
* 如果你也在特定的路徑操作中宣告了相依性,這些相依性也會被執行。 * 如果你也在特定的路徑操作中宣告了相依性,這些相依性也會被執行。
* Router 的相依性會先執行,然後是[裝飾器中的 `dependencies`](dependencies/dependencies-in-path-operation-decorators.md){.internal-link target=_blank},最後是一般參數相依性。 * Router 的相依性會先執行,然後是[裝飾器中的 `dependencies`](dependencies/dependencies-in-path-operation-decorators.md),最後是一般參數相依性。
* 你也可以加入帶有 `scopes` 的 [`Security` 相依性](../advanced/security/oauth2-scopes.md){.internal-link target=_blank} * 你也可以加入帶有 `scopes` 的 [`Security` 相依性](../advanced/security/oauth2-scopes.md)。
/// tip | 提示 /// tip | 提示
@ -303,7 +303,7 @@ from ...dependencies import get_token_header
照常匯入並建立 `FastAPI` 類別。 照常匯入並建立 `FastAPI` 類別。
我們甚至可以宣告[全域相依性](dependencies/global-dependencies.md){.internal-link target=_blank},它們會與各 `APIRouter` 的相依性合併: 我們甚至可以宣告[全域相依性](dependencies/global-dependencies.md),它們會與各 `APIRouter` 的相依性合併:
{* ../../docs_src/bigger_applications/app_an_py310/main.py hl[1,3,7] title["app/main.py"] *} {* ../../docs_src/bigger_applications/app_an_py310/main.py hl[1,3,7] title["app/main.py"] *}
@ -353,7 +353,7 @@ from .routers import items, users
from app.routers import items, users from app.routers import items, users
``` ```
想了解更多關於 Python 套件與模組,請閱讀<a href="https://docs.python.org/3/tutorial/modules.html" class="external-link" target="_blank">官方的模組說明文件</a> 想了解更多關於 Python 套件與模組,請閱讀[官方的模組說明文件](https://docs.python.org/3/tutorial/modules.html)
/// ///
@ -465,6 +465,37 @@ from .routers.users import router
/// ///
## 在 `pyproject.toml` 設定 `entrypoint` { #configure-the-entrypoint-in-pyproject-toml }
因為你的 FastAPI `app` 物件位在 `app/main.py`,你可以在 `pyproject.toml` 檔案中這樣設定 `entrypoint`
```toml
[tool.fastapi]
entrypoint = "app.main:app"
```
這等同於這樣匯入:
```python
from app.main import app
```
如此一來 `fastapi` 指令就會知道去哪裡找到你的 app。
/// Note | 注意
你也可以把路徑直接傳給指令,例如:
```console
$ fastapi dev app/main.py
```
但你每次呼叫 `fastapi` 指令時都得記得傳入正確的路徑。
此外,其他工具可能找不到它,例如 [VS Code 擴充套件](../editor-support.md) 或 [FastAPI Cloud](https://fastapicloud.com),因此建議在 `pyproject.toml` 中使用 `entrypoint`
///
## 檢查自動產生的 API 文件 { #check-the-automatic-api-docs } ## 檢查自動產生的 API 文件 { #check-the-automatic-api-docs }
現在,執行你的應用: 現在,執行你的應用:
@ -472,14 +503,14 @@ from .routers.users import router
<div class="termy"> <div class="termy">
```console ```console
$ fastapi dev app/main.py $ fastapi dev
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) <span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
``` ```
</div> </div>
然後開啟位於 <a href="http://127.0.0.1:8000/docs" class="external-link" target="_blank">http://127.0.0.1:8000/docs</a> 的文件。 然後開啟位於 [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs) 的文件。
你會看到自動產生的 API 文件,包含來自所有子模組的路徑,使用正確的路徑(與前綴)與正確的標籤: 你會看到自動產生的 API 文件,包含來自所有子模組的路徑,使用正確的路徑(與前綴)與正確的標籤:

6
docs/zh-hant/docs/tutorial/body-updates.md

@ -2,7 +2,7 @@
## 使用 `PUT` 取代式更新 { #update-replacing-with-put } ## 使用 `PUT` 取代式更新 { #update-replacing-with-put }
要更新一個項目,你可以使用 <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT" class="external-link" target="_blank">HTTP `PUT`</a> 操作。 要更新一個項目,你可以使用 [HTTP `PUT`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT) 操作。
你可以使用 `jsonable_encoder` 將輸入資料轉換為可儲存為 JSON 的資料(例如用於 NoSQL 資料庫)。例如把 `datetime` 轉成 `str` 你可以使用 `jsonable_encoder` 將輸入資料轉換為可儲存為 JSON 的資料(例如用於 NoSQL 資料庫)。例如把 `datetime` 轉成 `str`
@ -28,7 +28,7 @@
## 使用 `PATCH` 進行部分更新 { #partial-updates-with-patch } ## 使用 `PATCH` 進行部分更新 { #partial-updates-with-patch }
你也可以使用 <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH" class="external-link" target="_blank">HTTP `PATCH`</a> 操作來進行*部分*更新。 你也可以使用 [HTTP `PATCH`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH) 操作來進行*部分*更新。
這表示你只需傳送想要更新的資料,其餘保持不變。 這表示你只需傳送想要更新的資料,其餘保持不變。
@ -95,6 +95,6 @@
因此,如果你希望接收可以省略所有屬性的部分更新,你需要一個所有屬性皆為可選(具預設值或為 `None`)的模型。 因此,如果你希望接收可以省略所有屬性的部分更新,你需要一個所有屬性皆為可選(具預設值或為 `None`)的模型。
為了區分用於更新(全部可選)與用於建立(欄位為必填)的模型,你可以參考 [額外模型](extra-models.md){.internal-link target=_blank} 中的做法。 為了區分用於更新(全部可選)與用於建立(欄位為必填)的模型,你可以參考 [額外模型](extra-models.md) 中的做法。
/// ///

8
docs/zh-hant/docs/tutorial/cors.md

@ -1,6 +1,6 @@
# CORS(跨來源資源共用) { #cors-cross-origin-resource-sharing } # CORS(跨來源資源共用) { #cors-cross-origin-resource-sharing }
<a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS" class="external-link" target="_blank">CORS 或「Cross-Origin Resource Sharing」</a>指的是:當在瀏覽器中執行的前端以 JavaScript 與後端通訊,而後端與前端位於不同「來源(origin)」時的情境。 [CORS 或「Cross-Origin Resource Sharing」](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS)指的是:當在瀏覽器中執行的前端以 JavaScript 與後端通訊,而後端與前端位於不同「來源(origin)」時的情境。
## 來源(Origin) { #origin } ## 來源(Origin) { #origin }
@ -55,10 +55,10 @@
* `allow_origins` - 允許進行跨來源請求的來源清單。例如 `['https://example.org', 'https://www.example.org']`。你可以使用 `['*']` 來允許任何來源。 * `allow_origins` - 允許進行跨來源請求的來源清單。例如 `['https://example.org', 'https://www.example.org']`。你可以使用 `['*']` 來允許任何來源。
* `allow_origin_regex` - 允許進行跨來源請求的來源,使用正規表示式字串比對。例如 `'https://.*\.example\.org'` * `allow_origin_regex` - 允許進行跨來源請求的來源,使用正規表示式字串比對。例如 `'https://.*\.example\.org'`
* `allow_methods` - 允許跨來源請求的 HTTP 方法清單。預設為 `['GET']`。你可以使用 `['*']` 來允許所有標準方法。 * `allow_methods` - 允許跨來源請求的 HTTP 方法清單。預設為 `['GET']`。你可以使用 `['*']` 來允許所有標準方法。
* `allow_headers` - 允許跨來源請求所支援的 HTTP 請求標頭清單。預設為 `[]`。你可以使用 `['*']` 來允許所有標頭。對於<a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#simple_requests" class="external-link" rel="noopener" target="_blank">簡單 CORS 請求</a>,`Accept`、`Accept-Language`、`Content-Language` 與 `Content-Type` 標頭一律被允許。 * `allow_headers` - 允許跨來源請求所支援的 HTTP 請求標頭清單。預設為 `[]`。你可以使用 `['*']` 來允許所有標頭。對於[簡單 CORS 請求](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#simple_requests),`Accept`、`Accept-Language`、`Content-Language` 與 `Content-Type` 標頭一律被允許。
* `allow_credentials` - 指示是否支援跨來源請求的 Cookie。預設為 `False` * `allow_credentials` - 指示是否支援跨來源請求的 Cookie。預設為 `False`
`allow_credentials` 設為 `True` 時,`allow_origins`、`allow_methods` 與 `allow_headers` 都不能設為 `['*']`。上述各項必須<a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#credentialed_requests_and_wildcards" class="external-link" rel="noopener" target="_blank">明確指定</a> `allow_credentials` 設為 `True` 時,`allow_origins`、`allow_methods` 與 `allow_headers` 都不能設為 `['*']`。上述各項必須[明確指定](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#credentialed_requests_and_wildcards)
* `expose_headers` - 指示哪些回應標頭應該讓瀏覽器可存取。預設為 `[]` * `expose_headers` - 指示哪些回應標頭應該讓瀏覽器可存取。預設為 `[]`
* `max_age` - 設定瀏覽器快取 CORS 回應的最長秒數。預設為 `600` * `max_age` - 設定瀏覽器快取 CORS 回應的最長秒數。預設為 `600`
@ -77,7 +77,7 @@
## 更多資訊 { #more-info } ## 更多資訊 { #more-info }
想進一步了解 <abbr title="Cross-Origin Resource Sharing - 跨來源資源共用">CORS</abbr>,請參考 <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS" class="external-link" target="_blank">Mozilla 的 CORS 文件</a> 想進一步了解 <abbr title="Cross-Origin Resource Sharing - 跨來源資源共用">CORS</abbr>,請參考 [Mozilla 的 CORS 文件](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS)
/// note | 技術細節 /// note | 技術細節

4
docs/zh-hant/docs/tutorial/debugging.md

@ -59,7 +59,7 @@ $ python myapp.py
```Python ```Python
from myapp import app from myapp import app
# Some more code # 其他程式碼
``` ```
在那種情況下,`myapp.py` 中自動建立的變數 `__name__` 就不會是 `"__main__"` 在那種情況下,`myapp.py` 中自動建立的變數 `__name__` 就不會是 `"__main__"`
@ -74,7 +74,7 @@ from myapp import app
/// info | 說明 /// info | 說明
想了解更多,參考 <a href="https://docs.python.org/3/library/__main__.html" class="external-link" target="_blank">Python 官方文件</a> 想了解更多,參考 [Python 官方文件](https://docs.python.org/3/library/__main__.html)
/// ///

4
docs/zh-hant/docs/tutorial/encoder.md

@ -12,7 +12,7 @@
例如,它不接受 `datetime` 物件,因為那與 JSON 不相容。 例如,它不接受 `datetime` 物件,因為那與 JSON 不相容。
因此,必須將 `datetime` 物件轉為一個以 <a href="https://en.wikipedia.org/wiki/ISO_8601" class="external-link" target="_blank">ISO 格式</a> 表示資料的 `str` 因此,必須將 `datetime` 物件轉為一個以 [ISO 格式](https://en.wikipedia.org/wiki/ISO_8601) 表示資料的 `str`
同樣地,這個資料庫不會接受 Pydantic 模型(帶有屬性的物件),只接受 `dict` 同樣地,這個資料庫不會接受 Pydantic 模型(帶有屬性的物件),只接受 `dict`
@ -24,7 +24,7 @@
在此範例中,它會把 Pydantic 模型轉成 `dict`,並將 `datetime` 轉成 `str` 在此範例中,它會把 Pydantic 模型轉成 `dict`,並將 `datetime` 轉成 `str`
呼叫後的結果可以用 Python 標準的 <a href="https://docs.python.org/3/library/json.html#json.dumps" class="external-link" target="_blank">`json.dumps()`</a> 進行編碼。 呼叫後的結果可以用 Python 標準的 [`json.dumps()`](https://docs.python.org/3/library/json.html#json.dumps) 進行編碼。
它不會回傳一個包含 JSON 內容的大型 `str`(字串)。它會回傳 Python 標準的資料結構(例如 `dict`),其中的值與子值都與 JSON 相容。 它不會回傳一個包含 JSON 內容的大型 `str`(字串)。它會回傳 Python 標準的資料結構(例如 `dict`),其中的值與子值都與 JSON 相容。

4
docs/zh-hant/docs/tutorial/extra-data-types.md

@ -36,7 +36,7 @@
* `datetime.timedelta` * `datetime.timedelta`
* Python 的 `datetime.timedelta` * Python 的 `datetime.timedelta`
* 在請求與回應中會以總秒數的 `float` 表示。 * 在請求與回應中會以總秒數的 `float` 表示。
* Pydantic 也允許用「ISO 8601 time diff encoding」來表示,<a href="https://docs.pydantic.dev/latest/concepts/serialization/#custom-serializers" class="external-link" target="_blank">詳情見文件</a> * Pydantic 也允許用「ISO 8601 time diff encoding」來表示,[詳情見文件](https://docs.pydantic.dev/latest/concepts/serialization/#custom-serializers)
* `frozenset` * `frozenset`
* 在請求與回應中與 `set` 相同處理: * 在請求與回應中與 `set` 相同處理:
* 在請求中,會讀取一個 list,去除重複並轉為 `set` * 在請求中,會讀取一個 list,去除重複並轉為 `set`
@ -49,7 +49,7 @@
* `Decimal` * `Decimal`
* 標準的 Python `Decimal` * 標準的 Python `Decimal`
* 在請求與回應中,與 `float` 的處理方式相同。 * 在請求與回應中,與 `float` 的處理方式相同。
* 你可以在此查閱所有可用的 Pydantic 資料型別:<a href="https://docs.pydantic.dev/latest/usage/types/types/" class="external-link" target="_blank">Pydantic 資料型別</a> * 你可以在此查閱所有可用的 Pydantic 資料型別:[Pydantic 資料型別](https://docs.pydantic.dev/latest/usage/types/types/)
## 範例 { #example } ## 範例 { #example }

6
docs/zh-hant/docs/tutorial/extra-models.md

@ -12,7 +12,7 @@
切勿儲存使用者的明文密碼。務必只儲存可供驗證的「安全雜湊」。 切勿儲存使用者的明文密碼。務必只儲存可供驗證的「安全雜湊」。
若你還不清楚,稍後會在[安全性章節](security/simple-oauth2.md#password-hashing){.internal-link target=_blank}學到什麼是「密碼雜湊」。 若你還不清楚,稍後會在[安全性章節](security/simple-oauth2.md#password-hashing)學到什麼是「密碼雜湊」。
/// ///
@ -162,11 +162,11 @@ UserInDB(
在 OpenAPI 中會以 `anyOf` 定義。 在 OpenAPI 中會以 `anyOf` 定義。
要達成這點,使用標準的 Python 型別提示 <a href="https://docs.python.org/3/library/typing.html#typing.Union" class="external-link" target="_blank">`typing.Union`</a> 要達成這點,使用標準的 Python 型別提示 [`typing.Union`](https://docs.python.org/3/library/typing.html#typing.Union)
/// note /// note
在定義 <a href="https://docs.pydantic.dev/latest/concepts/types/#unions" class="external-link" target="_blank">`Union`</a> 時,請先放置「更具體」的型別,再放「較不具體」的型別。以下範例中,較具體的 `PlaneItem` 置於 `CarItem` 之前:`Union[PlaneItem, CarItem]`。 在定義 [`Union`](https://docs.pydantic.dev/latest/concepts/types/#unions) 時,請先放置「更具體」的型別,再放「較不具體」的型別。以下範例中,較具體的 `PlaneItem` 置於 `CarItem` 之前:`Union[PlaneItem, CarItem]`。
/// ///

75
docs/zh-hant/docs/tutorial/first-steps.md

@ -11,7 +11,7 @@
<div class="termy"> <div class="termy">
```console ```console
$ <font color="#4E9A06">fastapi</font> dev <u style="text-decoration-style:solid">main.py</u> $ <font color="#4E9A06">fastapi</font> dev
<span style="background-color:#009485"><font color="#D3D7CF"> FastAPI </font></span> Starting development server 🚀 <span style="background-color:#009485"><font color="#D3D7CF"> FastAPI </font></span> Starting development server 🚀
@ -58,7 +58,7 @@ INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
### 查看它 { #check-it } ### 查看它 { #check-it }
在瀏覽器中打開 <a href="http://127.0.0.1:8000" class="external-link" target="_blank">http://127.0.0.1:8000</a>. 在瀏覽器中打開 [http://127.0.0.1:8000](http://127.0.0.1:8000)。
你將看到如下的 JSON 回應: 你將看到如下的 JSON 回應:
@ -68,17 +68,17 @@ INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
### 互動式 API 文件 { #interactive-api-docs } ### 互動式 API 文件 { #interactive-api-docs }
現在,前往 <a href="http://127.0.0.1:8000/docs" class="external-link" target="_blank">http://127.0.0.1:8000/docs</a>. 現在,前往 [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs)。
你將看到自動的互動式 API 文件(由 <a href="https://github.com/swagger-api/swagger-ui" class="external-link" target="_blank">Swagger UI</a> 提供): 你將看到自動的互動式 API 文件(由 [Swagger UI](https://github.com/swagger-api/swagger-ui) 提供):
![Swagger UI](https://fastapi.tiangolo.com/img/index/index-01-swagger-ui-simple.png) ![Swagger UI](https://fastapi.tiangolo.com/img/index/index-01-swagger-ui-simple.png)
### 替代 API 文件 { #alternative-api-docs } ### 替代 API 文件 { #alternative-api-docs }
現在,前往 <a href="http://127.0.0.1:8000/redoc" class="external-link" target="_blank">http://127.0.0.1:8000/redoc</a>. 現在,前往 [http://127.0.0.1:8000/redoc](http://127.0.0.1:8000/redoc)。
你將看到另一種自動文件(由 <a href="https://github.com/Rebilly/ReDoc" class="external-link" target="_blank">ReDoc</a> 提供): 你將看到另一種自動文件(由 [ReDoc](https://github.com/Rebilly/ReDoc) 提供):
![ReDoc](https://fastapi.tiangolo.com/img/index/index-02-redoc-simple.png) ![ReDoc](https://fastapi.tiangolo.com/img/index/index-02-redoc-simple.png)
@ -92,7 +92,7 @@ INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
#### API 「schema」 { #api-schema } #### API 「schema」 { #api-schema }
在這種情況下,<a href="https://github.com/OAI/OpenAPI-Specification" class="external-link" target="_blank">OpenAPI</a> 是一個規範,它規定了如何定義 API 的 schema。 在這種情況下,[OpenAPI](https://github.com/OAI/OpenAPI-Specification) 是一個規範,它規定了如何定義 API 的 schema。
這個 schema 定義包含了你的 API 路徑、可能接收的參數等內容。 這個 schema 定義包含了你的 API 路徑、可能接收的參數等內容。
@ -110,7 +110,7 @@ OpenAPI 為你的 API 定義了 API 的 schema。而該 schema 會包含你的 A
如果你好奇原始的 OpenAPI schema 長什麼樣子,FastAPI 會自動生成一個包含所有 API 描述的 JSON(schema)。 如果你好奇原始的 OpenAPI schema 長什麼樣子,FastAPI 會自動生成一個包含所有 API 描述的 JSON(schema)。
你可以直接在 <a href="http://127.0.0.1:8000/openapi.json" class="external-link" target="_blank">http://127.0.0.1:8000/openapi.json</a> 查看它。 你可以直接在 [http://127.0.0.1:8000/openapi.json](http://127.0.0.1:8000/openapi.json) 查看它。
它會顯示一個 JSON,類似於: 它會顯示一個 JSON,類似於:
@ -143,9 +143,58 @@ OpenAPI schema 驅動了兩個互動式文件系統。
你也可以用它自動生成程式碼,讓用戶端與你的 API 通訊。例如前端、手機或物聯網(IoT)應用程式。 你也可以用它自動生成程式碼,讓用戶端與你的 API 通訊。例如前端、手機或物聯網(IoT)應用程式。
### 在 `pyproject.toml` 設定應用的 `entrypoint` { #configure-the-app-entrypoint-in-pyproject-toml }
你可以在 `pyproject.toml` 中設定你的應用位置,例如:
```toml
[tool.fastapi]
entrypoint = "main:app"
```
這個 `entrypoint` 會告訴 `fastapi` 指令應該用下面的方式匯入 app:
```python
from main import app
```
如果你的程式碼結構像是:
```
.
├── backend
│   ├── main.py
│   ├── __init__.py
```
那你應該把 `entrypoint` 設為:
```toml
[tool.fastapi]
entrypoint = "backend.main:app"
```
這等同於:
```python
from backend.main import app
```
### 搭配路徑使用 `fastapi dev` { #fastapi-dev-with-path }
你也可以把檔案路徑傳給 `fastapi dev` 指令,它會自動猜測要使用的 FastAPI app 物件:
```console
$ fastapi dev main.py
```
但這樣每次執行 `fastapi` 指令時都要記得傳入正確路徑。
此外,其他工具可能找不到它,例如 [VS Code 擴充套件](../editor-support.md) 或 [FastAPI Cloud](https://fastapicloud.com),因此建議在 `pyproject.toml` 中使用 `entrypoint`
### 部署你的應用程式(可選) { #deploy-your-app-optional } ### 部署你的應用程式(可選) { #deploy-your-app-optional }
你可以選擇將你的 FastAPI 應用程式部署到 <a href="https://fastapicloud.com" class="external-link" target="_blank">FastAPI Cloud</a>,如果還沒有,去加入候補名單吧。🚀 你可以選擇將你的 FastAPI 應用程式部署到 [FastAPI Cloud](https://fastapicloud.com),如果還沒有,去加入候補名單吧。🚀
如果你已經有 **FastAPI Cloud** 帳號(我們已從候補名單邀請你 😉),你可以用一個指令部署你的應用程式。 如果你已經有 **FastAPI Cloud** 帳號(我們已從候補名單邀請你 😉),你可以用一個指令部署你的應用程式。
@ -191,7 +240,7 @@ Deploying to FastAPI Cloud...
`FastAPI` 是一個直接繼承自 `Starlette` 的類別。 `FastAPI` 是一個直接繼承自 `Starlette` 的類別。
你同樣可以透過 `FastAPI` 來使用 <a href="https://www.starlette.dev/" class="external-link" target="_blank">Starlette</a> 所有的功能。 你同樣可以透過 `FastAPI` 來使用 [Starlette](https://www.starlette.dev/) 所有的功能。
/// ///
@ -336,7 +385,7 @@ Python 中的 `@something` 語法被稱為「裝飾器」。
/// note /// note
如果你不知道差別,請查看 [Async: *"In a hurry?"*](../async.md#in-a-hurry){.internal-link target=_blank}. 如果你不知道差別,請查看 [Async: *"In a hurry?"*](../async.md#in-a-hurry)
/// ///
@ -352,11 +401,11 @@ Python 中的 `@something` 語法被稱為「裝飾器」。
### 第六步:部署 { #step-6-deploy-it } ### 第六步:部署 { #step-6-deploy-it }
用一行指令將你的應用程式部署到 **<a href="https://fastapicloud.com" class="external-link" target="_blank">FastAPI Cloud</a>**:`fastapi deploy`。🎉 用一行指令將你的應用程式部署到 **[FastAPI Cloud](https://fastapicloud.com)**:`fastapi deploy`。🎉
#### 關於 FastAPI Cloud { #about-fastapi-cloud } #### 關於 FastAPI Cloud { #about-fastapi-cloud }
**<a href="https://fastapicloud.com" class="external-link" target="_blank">FastAPI Cloud</a>** 由 **FastAPI** 的作者與團隊打造。 **[FastAPI Cloud](https://fastapicloud.com)** 由 **FastAPI** 的作者與團隊打造。
它讓你以最小的成本完成 API 的**建置**、**部署**與**存取**流程。 它讓你以最小的成本完成 API 的**建置**、**部署**與**存取**流程。

2
docs/zh-hant/docs/tutorial/handling-errors.md

@ -81,7 +81,7 @@
## 安裝自訂例外處理器 { #install-custom-exception-handlers } ## 安裝自訂例外處理器 { #install-custom-exception-handlers }
你可以使用 <a href="https://www.starlette.dev/exceptions/" class="external-link" target="_blank">Starlette 的相同例外工具</a> 來加入自訂例外處理器。 你可以使用 [Starlette 的相同例外工具](https://www.starlette.dev/exceptions/) 來加入自訂例外處理器。
假設你有一個自訂例外 `UnicornException`,你(或你使用的函式庫)可能會 `raise` 它。 假設你有一個自訂例外 `UnicornException`,你(或你使用的函式庫)可能會 `raise` 它。

22
docs/zh-hant/docs/tutorial/index.md

@ -15,7 +15,7 @@
<div class="termy"> <div class="termy">
```console ```console
$ <font color="#4E9A06">fastapi</font> dev <u style="text-decoration-style:solid">main.py</u> $ <font color="#4E9A06">fastapi</font> dev
<span style="background-color:#009485"><font color="#D3D7CF"> FastAPI </font></span> Starting development server 🚀 <span style="background-color:#009485"><font color="#D3D7CF"> FastAPI </font></span> Starting development server 🚀
@ -42,12 +42,12 @@ $ <font color="#4E9A06">fastapi</font> dev <u style="text-decoration-style:solid
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Will watch for changes in these directories: <span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Will watch for changes in these directories:
<b>[</b><font color="#4E9A06">&apos;/home/user/code/awesomeapp&apos;</font><b>]</b> <b>[</b><font color="#4E9A06">&apos;/home/user/code/awesomeapp&apos;</font><b>]</b>
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Uvicorn running on <font color="#729FCF"><u style="text-decoration-style:solid">http://127.0.0.1:8000</u></font> <b>(</b>Press CTRL+C <span style="background-color="#007166"><font color="#D3D7CF"> INFO </font></span> Uvicorn running on <font color="#729FCF"><u style="text-decoration-style:solid">http://127.0.0.1:8000</u></font> <b>(</b>Press CTRL+C
to quit<b>)</b> to quit<b>)</b>
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Started reloader process <b>[</b><font color="#34E2E2"><b>383138</b></font><b>]</b> using WatchFiles <span style="background-color="#007166"><font color="#D3D7CF"> INFO </font></span> Started reloader process <b>[</b><font color="#34E2E2"><b>383138</b></font><b>]</b> using WatchFiles
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Started server process <b>[</b><font color="#34E2E2"><b>383153</b></font><b>]</b> <span style="background-color="#007166"><font color="#D3D7CF"> INFO </font></span> Started server process <b>[</b><font color="#34E2E2"><b>383153</b></font><b>]</b>
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Waiting for application startup. <span style="background-color="#007166"><font color="#D3D7CF"> INFO </font></span> Waiting for application startup.
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Application startup complete. <span style="background-color="#007166"><font color="#D3D7CF"> INFO </font></span> Application startup complete.
``` ```
</div> </div>
@ -62,7 +62,7 @@ $ <font color="#4E9A06">fastapi</font> dev <u style="text-decoration-style:solid
第一步是安裝 FastAPI。 第一步是安裝 FastAPI。
確保你建立一個[虛擬環境](../virtual-environments.md){.internal-link target=_blank},啟用它,然後**安裝 FastAPI**: 確保你建立一個[虛擬環境](../virtual-environments.md),啟用它,然後**安裝 FastAPI**:
<div class="termy"> <div class="termy">
@ -76,7 +76,7 @@ $ pip install "fastapi[standard]"
/// note | 注意 /// note | 注意
當你使用 `pip install "fastapi[standard]"` 安裝時,會包含一些預設的可選標準依賴項,其中包括 `fastapi-cloud-cli`,它可以讓你部署到 <a href="https://fastapicloud.com" class="external-link" target="_blank">FastAPI Cloud</a> 當你使用 `pip install "fastapi[standard]"` 安裝時,會包含一些預設的可選標準依賴項,其中包括 `fastapi-cloud-cli`,它可以讓你部署到 [FastAPI Cloud](https://fastapicloud.com)
如果你不想包含那些可選的依賴項,你可以改為安裝 `pip install fastapi` 如果你不想包含那些可選的依賴項,你可以改為安裝 `pip install fastapi`
@ -84,6 +84,12 @@ $ pip install "fastapi[standard]"
/// ///
/// tip
FastAPI 提供了 [VS Code 官方擴充功能](https://marketplace.visualstudio.com/items?itemName=FastAPILabs.fastapi-vscode)(以及 Cursor),包含許多功能,例如路徑操作探索器、路徑操作搜尋、測試中的 CodeLens 導航(從測試跳到定義)、以及 FastAPI Cloud 的部署與日誌,全部可直接在你的編輯器中完成。
///
## 進階使用者指南 { #advanced-user-guide } ## 進階使用者指南 { #advanced-user-guide }
還有一個**進階使用者指南**你可以在讀完這個**教學 - 使用者指南**後再閱讀。 還有一個**進階使用者指南**你可以在讀完這個**教學 - 使用者指南**後再閱讀。

10
docs/zh-hant/docs/tutorial/middleware.md

@ -15,7 +15,7 @@
如果你有使用帶有 `yield` 的相依性,其釋放階段的程式碼會在中介軟體之後執行。 如果你有使用帶有 `yield` 的相依性,其釋放階段的程式碼會在中介軟體之後執行。
若有背景工作(在[背景工作](background-tasks.md){.internal-link target=_blank}一節會介紹,你稍後會看到),它們會在所有中介軟體之後執行。 若有背景工作(在[背景工作](background-tasks.md)一節會介紹,你稍後會看到),它們會在所有中介軟體之後執行。
/// ///
@ -35,9 +35,9 @@
/// tip /// tip
請記得,自訂的非標準標頭可以<a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers" class="external-link" target="_blank">使用 `X-` 前綴</a> 請記得,自訂的非標準標頭可以[使用 `X-` 前綴](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers)
但如果你有自訂標頭並希望瀏覽器端的用戶端能看到它們,你需要在 CORS 設定([CORS(Cross-Origin Resource Sharing)](cors.md){.internal-link target=_blank})中使用 <a href="https://www.starlette.dev/middleware/#corsmiddleware" class="external-link" target="_blank">Starlette 的 CORS 文件</a>所記載的參數 `expose_headers` 將它們加入。 但如果你有自訂標頭並希望瀏覽器端的用戶端能看到它們,你需要在 CORS 設定([CORS(跨來源資源共用)](cors.md))中使用 [Starlette 的 CORS 文件](https://www.starlette.dev/middleware/#corsmiddleware)所記載的參數 `expose_headers` 將它們加入。
/// ///
@ -61,7 +61,7 @@
/// tip /// tip
這裡我們使用 <a href="https://docs.python.org/3/library/time.html#time.perf_counter" class="external-link" target="_blank">`time.perf_counter()`</a> 而不是 `time.time()`,因為在這些用例中它可能更精確。🤓 這裡我們使用 [`time.perf_counter()`](https://docs.python.org/3/library/time.html#time.perf_counter) 而不是 `time.time()`,因為在這些用例中它可能更精確。🤓
/// ///
@ -90,6 +90,6 @@ app.add_middleware(MiddlewareB)
## 其他中介軟體 { #other-middlewares } ## 其他中介軟體 { #other-middlewares }
你之後可以在[進階使用者指南:進階中介軟體](../advanced/middleware.md){.internal-link target=_blank}閱讀更多關於其他中介軟體的內容。 你之後可以在[進階使用者指南:進階中介軟體](../advanced/middleware.md)閱讀更多關於其他中介軟體的內容。
下一節你將會讀到如何使用中介軟體處理 <abbr title="Cross-Origin Resource Sharing - 跨來源資源共用">CORS</abbr> 下一節你將會讀到如何使用中介軟體處理 <abbr title="Cross-Origin Resource Sharing - 跨來源資源共用">CORS</abbr>

16
docs/zh-hant/docs/tutorial/path-params.md

@ -6,7 +6,7 @@
路徑參數 `item_id` 的值會作為引數 `item_id` 傳入你的函式。 路徑參數 `item_id` 的值會作為引數 `item_id` 傳入你的函式。
所以,如果你執行這個範例並前往 <a href="http://127.0.0.1:8000/items/foo" class="external-link" target="_blank">http://127.0.0.1:8000/items/foo</a>,你會看到這樣的回應: 所以,如果你執行這個範例並前往 [http://127.0.0.1:8000/items/foo](http://127.0.0.1:8000/items/foo),你會看到這樣的回應:
```JSON ```JSON
{"item_id":"foo"} {"item_id":"foo"}
@ -28,7 +28,7 @@
## 資料 <dfn title="也稱為:序列化、解析、封送">轉換</dfn> { #data-conversion } ## 資料 <dfn title="也稱為:序列化、解析、封送">轉換</dfn> { #data-conversion }
如果你執行這個範例並在瀏覽器開啟 <a href="http://127.0.0.1:8000/items/3" class="external-link" target="_blank">http://127.0.0.1:8000/items/3</a>,你會看到這樣的回應: 如果你執行這個範例並在瀏覽器開啟 [http://127.0.0.1:8000/items/3](http://127.0.0.1:8000/items/3),你會看到這樣的回應:
```JSON ```JSON
{"item_id":3} {"item_id":3}
@ -44,7 +44,7 @@
## 資料驗證 { #data-validation } ## 資料驗證 { #data-validation }
但如果你在瀏覽器前往 <a href="http://127.0.0.1:8000/items/foo" class="external-link" target="_blank">http://127.0.0.1:8000/items/foo</a>,你會看到漂亮的 HTTP 錯誤: 但如果你在瀏覽器前往 [http://127.0.0.1:8000/items/foo](http://127.0.0.1:8000/items/foo),你會看到漂亮的 HTTP 錯誤:
```JSON ```JSON
{ {
@ -64,7 +64,7 @@
因為路徑參數 `item_id` 的值是 `"foo"`,它不是 `int` 因為路徑參數 `item_id` 的值是 `"foo"`,它不是 `int`
同樣的錯誤也會在你提供 `float` 而不是 `int` 時出現,例如:<a href="http://127.0.0.1:8000/items/4.2" class="external-link" target="_blank">http://127.0.0.1:8000/items/4.2</a> 同樣的錯誤也會在你提供 `float` 而不是 `int` 時出現,例如:[http://127.0.0.1:8000/items/4.2](http://127.0.0.1:8000/items/4.2)
/// check /// check
@ -78,7 +78,7 @@
## 文件 { #documentation } ## 文件 { #documentation }
當你在瀏覽器開啟 <a href="http://127.0.0.1:8000/docs" class="external-link" target="_blank">http://127.0.0.1:8000/docs</a>,你會看到自動產生、可互動的 API 文件,例如: 當你在瀏覽器開啟 [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs),你會看到自動產生、可互動的 API 文件,例如:
<img src="/img/tutorial/path-params/image01.png"> <img src="/img/tutorial/path-params/image01.png">
@ -92,9 +92,9 @@
## 基於標準的優勢與替代文件 { #standards-based-benefits-alternative-documentation } ## 基於標準的優勢與替代文件 { #standards-based-benefits-alternative-documentation }
而且因為產生的 schema 來自 <a href="https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md" class="external-link" target="_blank">OpenAPI</a> 標準,有很多相容的工具可用。 而且因為產生的 schema 來自 [OpenAPI](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md) 標準,有很多相容的工具可用。
因此,**FastAPI** 本身也提供另一種 API 文件(使用 ReDoc),你可以在 <a href="http://127.0.0.1:8000/redoc" class="external-link" target="_blank">http://127.0.0.1:8000/redoc</a> 存取: 因此,**FastAPI** 本身也提供另一種 API 文件(使用 ReDoc),你可以在 [http://127.0.0.1:8000/redoc](http://127.0.0.1:8000/redoc) 存取:
<img src="/img/tutorial/path-params/image02.png"> <img src="/img/tutorial/path-params/image02.png">
@ -102,7 +102,7 @@
## Pydantic { #pydantic } ## Pydantic { #pydantic }
所有資料驗證都由 <a href="https://docs.pydantic.dev/" class="external-link" target="_blank">Pydantic</a> 在底層處理,因此你能直接受惠。而且你可以放心使用。 所有資料驗證都由 [Pydantic](https://docs.pydantic.dev/) 在底層處理,因此你能直接受惠。而且你可以放心使用。
你可以用相同的型別宣告搭配 `str`、`float`、`bool` 與許多更複雜的資料型別。 你可以用相同的型別宣告搭配 `str`、`float`、`bool` 與許多更複雜的資料型別。

6
docs/zh-hant/docs/tutorial/query-params-str-validations.md

@ -157,7 +157,7 @@ q: str = Query(default="rick")
若不使用 `Annotated`、改用「(舊式)預設值」寫法,你在沒有 FastAPI 的「其他地方」呼叫該函式時,就得「記得」傳入正確參數,否則值會和預期不同(例如會得到 `QueryInfo` 或類似的東西,而不是 `str`)。你的編輯器不會提示,Python 執行該函式時也不會抱怨,只有在內部操作失敗時才會出錯。 若不使用 `Annotated`、改用「(舊式)預設值」寫法,你在沒有 FastAPI 的「其他地方」呼叫該函式時,就得「記得」傳入正確參數,否則值會和預期不同(例如會得到 `QueryInfo` 或類似的東西,而不是 `str`)。你的編輯器不會提示,Python 執行該函式時也不會抱怨,只有在內部操作失敗時才會出錯。
因為 `Annotated` 可以有多個中繼資料註解,你甚至可以用同一個函式配合其他工具,例如 <a href="https://typer.tiangolo.com/" class="external-link" target="_blank">Typer</a>。🚀 因為 `Annotated` 可以有多個中繼資料註解,你甚至可以用同一個函式配合其他工具,例如 [Typer](https://typer.tiangolo.com/)。🚀
## 加入更多驗證 { #add-more-validations } ## 加入更多驗證 { #add-more-validations }
@ -369,11 +369,11 @@ http://127.0.0.1:8000/items/?item-query=foobaritems
這種情況下,你可以使用「自訂驗證函式」,它會在一般驗證之後套用(例如先確認值是 `str` 之後)。 這種情況下,你可以使用「自訂驗證函式」,它會在一般驗證之後套用(例如先確認值是 `str` 之後)。
你可以在 `Annotated` 中使用 <a href="https://docs.pydantic.dev/latest/concepts/validators/#field-after-validator" class="external-link" target="_blank">Pydantic 的 `AfterValidator`</a> 來達成。 你可以在 `Annotated` 中使用 [Pydantic 的 `AfterValidator`](https://docs.pydantic.dev/latest/concepts/validators/#field-after-validator) 來達成。
/// tip | 提示 /// tip | 提示
Pydantic 也有 <a href="https://docs.pydantic.dev/latest/concepts/validators/#field-before-validator" class="external-link" target="_blank">`BeforeValidator`</a> 等等。🤓 Pydantic 也有 [`BeforeValidator`](https://docs.pydantic.dev/latest/concepts/validators/#field-before-validator) 等等。🤓
/// ///

6
docs/zh-hant/docs/tutorial/request-forms.md

@ -4,9 +4,9 @@
/// info /// info
要使用表單,請先安裝 <a href="https://github.com/Kludex/python-multipart" class="external-link" target="_blank">`python-multipart`</a> 要使用表單,請先安裝 [`python-multipart`](https://github.com/Kludex/python-multipart)
請先建立並啟用一個[虛擬環境](../virtual-environments.md){.internal-link target=_blank},然後再安裝,例如: 請先建立並啟用一個[虛擬環境](../virtual-environments.md),然後再安裝,例如:
```console ```console
$ pip install python-multipart $ pip install python-multipart
@ -56,7 +56,7 @@ HTML 表單(`<form></form>`)向伺服器傳送資料時,通常會使用一
但當表單包含檔案時,會使用 `multipart/form-data`。你會在下一章閱讀如何處理檔案。 但當表單包含檔案時,會使用 `multipart/form-data`。你會在下一章閱讀如何處理檔案。
若想進一步了解這些編碼與表單欄位,請參考 <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST" class="external-link" target="_blank"><abbr title="Mozilla Developer Network - Mozilla 開發者網路">MDN</abbr><code>POST</code> 網頁文件</a> 若想進一步了解這些編碼與表單欄位,請參考 [<abbr title="Mozilla Developer Network - Mozilla 開發者網路">MDN</abbr> web docs 的 `POST`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST)
/// ///

9
docs/zh-hant/docs/tutorial/response-model.md

@ -13,6 +13,7 @@ FastAPI 會使用這個回傳型別來:
* 在 OpenAPI 的「路徑操作」中為回應新增 JSON Schema。 * 在 OpenAPI 的「路徑操作」中為回應新增 JSON Schema。
* 這會被自動文件使用。 * 這會被自動文件使用。
* 也會被自動用戶端程式碼產生工具使用。 * 也會被自動用戶端程式碼產生工具使用。
* 使用 Pydantic 將回傳資料**序列化**為 JSON,而 Pydantic 是用 **Rust** 撰寫的,因此會 **更快很多**
但更重要的是: 但更重要的是:
@ -73,9 +74,9 @@ FastAPI 會使用這個 `response_model` 來做所有的資料文件、驗證等
/// info | 說明 /// info | 說明
要使用 `EmailStr`,請先安裝 <a href="https://github.com/JoshData/python-email-validator" class="external-link" target="_blank">`email-validator`</a> 要使用 `EmailStr`,請先安裝 [`email-validator`](https://github.com/JoshData/python-email-validator)
請先建立一個[虛擬環境](../virtual-environments.md){.internal-link target=_blank}、啟用它,然後安裝,例如: 請先建立一個[虛擬環境](../virtual-environments.md)、啟用它,然後安裝,例如:
```console ```console
$ pip install email-validator $ pip install email-validator
@ -181,7 +182,7 @@ FastAPI 在內部會搭配 Pydantic 做一些事情,來確保不會把類別
### 直接回傳 Response { #return-a-response-directly } ### 直接回傳 Response { #return-a-response-directly }
最常見的情況是[直接回傳 Response(在進階文件中稍後會解釋)](../advanced/response-directly.md){.internal-link target=_blank} 最常見的情況是[直接回傳 Response(在進階文件中稍後會解釋)](../advanced/response-directly.md)。
{* ../../docs_src/response_model/tutorial003_02_py310.py hl[8,10:11] *} {* ../../docs_src/response_model/tutorial003_02_py310.py hl[8,10:11] *}
@ -257,7 +258,7 @@ FastAPI 在內部會搭配 Pydantic 做一些事情,來確保不會把類別
* `response_model_exclude_defaults=True` * `response_model_exclude_defaults=True`
* `response_model_exclude_none=True` * `response_model_exclude_none=True`
<a href="https://docs.pydantic.dev/1.10/usage/exporting_models/#modeldict" class="external-link" target="_blank">Pydantic 文件</a>中對 `exclude_defaults``exclude_none` 的說明。 [Pydantic 文件](https://docs.pydantic.dev/1.10/usage/exporting_models/#modeldict)中對 `exclude_defaults``exclude_none` 的說明。
/// ///

6
docs/zh-hant/docs/tutorial/response-status-code.md

@ -20,7 +20,7 @@
/// info | 資訊 /// info | 資訊
`status_code` 也可以接收一個 `IntEnum`,例如 Python 的 <a href="https://docs.python.org/3/library/http.html#http.HTTPStatus" class="external-link" target="_blank">`http.HTTPStatus`</a> `status_code` 也可以接收一個 `IntEnum`,例如 Python 的 [`http.HTTPStatus`](https://docs.python.org/3/library/http.html#http.HTTPStatus)
/// ///
@ -66,7 +66,7 @@ FastAPI 知道這點,並會產生聲明「無回應本文」的 OpenAPI 文件
/// tip | 提示 /// tip | 提示
想深入瞭解各狀態碼與其用途,請參考 <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status" class="external-link" target="_blank"><abbr title="Mozilla Developer Network - Mozilla 開發者網路">MDN</abbr> 關於 HTTP 狀態碼的文件</a> 想深入瞭解各狀態碼與其用途,請參考 [<abbr title="Mozilla Developer Network - Mozilla 開發者網路">MDN</abbr> 關於 HTTP 狀態碼的文件](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
/// ///
@ -98,4 +98,4 @@ FastAPI 知道這點,並會產生聲明「無回應本文」的 OpenAPI 文件
## 變更預設值 { #changing-the-default } ## 變更預設值 { #changing-the-default }
稍後在 [進階使用者指南](../advanced/response-change-status-code.md){.internal-link target=_blank} 中,你會看到如何回傳一個不同於此處所宣告預設值的狀態碼。 稍後在 [進階使用者指南](../advanced/response-change-status-code.md) 中,你會看到如何回傳一個不同於此處所宣告預設值的狀態碼。

28
docs/zh-hant/docs/virtual-environments.md

@ -22,7 +22,7 @@
這個頁面將教你如何使用**虛擬環境**以及了解它們的工作原理。 這個頁面將教你如何使用**虛擬環境**以及了解它們的工作原理。
如果你計畫使用一個**可以為你管理一切的工具**(包括安裝 Python),試試 <a href="https://github.com/astral-sh/uv" class="external-link" target="_blank">uv</a> 如果你計畫使用一個**可以為你管理一切的工具**(包括安裝 Python),試試 [uv](https://github.com/astral-sh/uv)
/// ///
@ -86,7 +86,7 @@ $ python -m venv .venv
//// tab | `uv` //// tab | `uv`
如果你安裝了 <a href="https://github.com/astral-sh/uv" class="external-link" target="_blank">`uv`</a>,你也可以使用它來建立一個虛擬環境。 如果你安裝了 [`uv`](https://github.com/astral-sh/uv),你也可以使用它來建立一個虛擬環境。
<div class="termy"> <div class="termy">
@ -150,7 +150,7 @@ $ .venv\Scripts\Activate.ps1
//// tab | Windows Bash //// tab | Windows Bash
或者,如果你在 Windows 上使用 Bash(例如 <a href="https://gitforwindows.org/" class="external-link" target="_blank">Git Bash</a>): 或者,如果你在 Windows 上使用 Bash(例如 [Git Bash](https://gitforwindows.org/)):
<div class="termy"> <div class="termy">
@ -216,7 +216,7 @@ C:\Users\user\code\awesome-project\.venv\Scripts\python
/// tip /// tip
如果你使用 <a href="https://github.com/astral-sh/uv" class="external-link" target="_blank">`uv`</a> 來安裝內容,而不是 `pip`,那麼你就不需要升級 `pip`。😎 如果你使用 [`uv`](https://github.com/astral-sh/uv) 來安裝內容,而不是 `pip`,那麼你就不需要升級 `pip`。😎
/// ///
@ -268,7 +268,7 @@ $ python -m ensurepip --upgrade
/// tip /// tip
如果你使用 <a href="https://github.com/astral-sh/uv" class="external-link" target="_blank">`uv`</a> 來建立虛擬環境,它會自動為你完成這個操作,你可以跳過這一步。😎 如果你使用 [`uv`](https://github.com/astral-sh/uv) 來建立虛擬環境,它會自動為你完成這個操作,你可以跳過這一步。😎
/// ///
@ -340,7 +340,7 @@ $ pip install "fastapi[standard]"
//// tab | `uv` //// tab | `uv`
如果你有 <a href="https://github.com/astral-sh/uv" class="external-link" target="_blank">`uv`</a>: 如果你有 [`uv`](https://github.com/astral-sh/uv):
<div class="termy"> <div class="termy">
@ -372,7 +372,7 @@ $ pip install -r requirements.txt
//// tab | `uv` //// tab | `uv`
如果你有 <a href="https://github.com/astral-sh/uv" class="external-link" target="_blank">`uv`</a>: 如果你有 [`uv`](https://github.com/astral-sh/uv):
<div class="termy"> <div class="termy">
@ -416,8 +416,8 @@ Hello World
例如: 例如:
* <a href="https://code.visualstudio.com/docs/python/environments#_select-and-activate-an-environment" class="external-link" target="_blank">VS Code</a> * [VS Code](https://code.visualstudio.com/docs/python/environments#_select-and-activate-an-environment)
* <a href="https://www.jetbrains.com/help/pycharm/creating-virtual-environment.html" class="external-link" target="_blank">PyCharm</a> * [PyCharm](https://www.jetbrains.com/help/pycharm/creating-virtual-environment.html)
/// tip /// tip
@ -455,7 +455,7 @@ $ deactivate
## 為什麼要使用虛擬環境 { #why-virtual-environments } ## 為什麼要使用虛擬環境 { #why-virtual-environments }
你需要安裝 <a href="https://www.python.org/" class="external-link" target="_blank">Python</a> 才能使用 FastAPI。 你需要安裝 [Python](https://www.python.org/) 才能使用 FastAPI。
接下來,你需要**安裝** FastAPI 以及你想使用的其他**套件**。 接下來,你需要**安裝** FastAPI 以及你想使用的其他**套件**。
@ -564,7 +564,7 @@ $ pip install "fastapi[standard]"
</div> </div>
這會從 <a href="https://pypi.org/project/fastapi/" class="external-link" target="_blank">PyPI</a> 下載一個壓縮檔案,其中包含 FastAPI 的程式碼。 這會從 [PyPI](https://pypi.org/project/fastapi/) 下載一個壓縮檔案,其中包含 FastAPI 的程式碼。
它還會**下載** FastAPI 所依賴的其他套件的檔案。 它還會**下載** FastAPI 所依賴的其他套件的檔案。
@ -627,7 +627,7 @@ $ .venv\Scripts\Activate.ps1
//// tab | Windows Bash //// tab | Windows Bash
或者如果你在 Windows 上使用 Bash(例如 <a href="https://gitforwindows.org/" class="external-link" target="_blank">Git Bash</a>): 或者如果你在 Windows 上使用 Bash(例如 [Git Bash](https://gitforwindows.org/)):
<div class="termy"> <div class="termy">
@ -819,7 +819,7 @@ Traceback (most recent call last):
</div> </div>
但如果你停用虛擬環境並啟用 `prisoner-of-askaban` 的新虛擬環境,那麼當你執行 `python` 時,它會使用 `prisoner-of-askaban` 中虛擬環境的 Python。 但如果你停用虛擬環境並啟用 `prisoner-of-azkaban` 的新虛擬環境,那麼當你執行 `python` 時,它會使用 `prisoner-of-azkaban` 中虛擬環境的 Python。
<div class="termy"> <div class="termy">
@ -846,7 +846,7 @@ I solemnly swear 🐺
有許多**替代方案**來管理虛擬環境、套件依賴(requirements)、專案。 有許多**替代方案**來管理虛擬環境、套件依賴(requirements)、專案。
當你準備好並想要使用一個工具來**管理整個專案**、套件依賴、虛擬環境等,建議你嘗試 <a href="https://github.com/astral-sh/uv" class="external-link" target="_blank">uv</a> 當你準備好並想要使用一個工具來**管理整個專案**、套件依賴、虛擬環境等,建議你嘗試 [uv](https://github.com/astral-sh/uv)
`uv` 可以執行許多操作,它可以: `uv` 可以執行許多操作,它可以:

Loading…
Cancel
Save