在本部落格系列中,讀者將學習如何使用 FastAPI 建立待辦事項應用程式。本系列將逐步指導您使用 FastAPI(一種現代高效能 Python Web 框架)從頭開始建立待辦事項應用程式。內容涵蓋從設定開發環境到部署應用程式的所有內容。在本系列結束時,讀者將創建自己的待辦事項應用程式並牢牢掌握 FastAPI。
該系列中的每篇文章都將重點放在開發過程的特定方面,提供清晰的解釋和實用的程式碼範例。目標是讓讀者掌握使用 FastAPI 建立 Web 應用程式所需的知識和技能。無論您是想要學習 Web 開發的初學者,還是探索 FastAPI 的經驗豐富的開發人員,本系列都旨在提供寶貴的見解和實踐經驗。
第一篇文章將介紹 FastAPI,幫助您設定開發環境並建立 FastAPI 應用程式。
Todo_Part1 目錄中的部落格程式碼:GitHub - jamesbmour/blog_tutorials
FastAPI 是一個現代、快速(高效能)的 Web 框架,用於基於標準 Python 類型提示使用 Python 3.6+ 建立 API。它被設計為易於使用、快速編碼、可用於生產,並且基於最新的 Python 功能。
與 Flask 或 Django 等其他流行的 Python Web 框架相比,FastAPI 提供:
對於我們的 Todo 應用程序,FastAPI 是一個絕佳的選擇,因為:
確保您安裝了 Python 3.7 或更高版本。您可以從 python.org 下載它。
最佳實務是為 Python 專案使用虛擬環境。建立方法如下:
python -m venv todo-env source todo-env/bin/activate # On Windows, use `todo-env\Scripts\activate`
conda create --name todo-env python=3.9 conda activate todo-env
poetry install # activate the virtual environment poetry shell
現在,讓我們安裝 FastAPI 和 Uvicorn(ASGI 伺服器):
pip install fastapi uvicorn
建立一個名為main.py的新檔案並新增以下程式碼:
from fastapi import FastAPI app = FastAPI() @app.get("/") async def root(): return {"message": "Hello, FastAPI!"}
要執行應用程序,請使用以下命令:
uvicorn main:app --reload
此指令告訴 Uvicorn:
Once your server is running, you can access the automatic API documentation by navigating to http://127.0.0.1:8000/docs in your web browser.
Create a new directory for your project:
mkdir fastapi-todo-app cd fastapi-todo-app
Create the following files in your project directory:
Add the following to your requirements.txt:
fastapi uvicorn
And add this to your .gitignore:
__pycache__ *.pyc todo-env/
We've already created a root endpoint in our main.py. Let's modify it slightly:
from fastapi import FastAPI app = FastAPI() @app.get("/") async def root(): return {"message": "Welcome to the Todo App!"}
FastAPI automatically converts the dictionary we return into a JSON response.
Run your server with uvicorn main:app --reload, then navigate to http://127.0.0.1:8000 in your web browser. You should see the JSON response.
Navigate to http://127.0.0.1:8000/docs to see the Swagger UI documentation for your API. You can test your endpoint directly from this interface.
In the upcoming blog post, we will explore FastAPI in more detail by developing the fundamental features of our Todo App. We will establish endpoints for adding, retrieving, updating, and deleting todos. As an exercise, you can add a new endpoint that provides the current date and time when accessed.
@app.get("/current-time") async def get_current_time(): current_time = datetime.now() return { "current_date": current_time.strftime("%Y-%m-%d"), "current_time": current_time.strftime("%H:%M:%S"), "timezone": current_time.astimezone().tzname() }
Congratulations! You've successfully set up your development environment, created your first FastAPI application, and learned about the basic structure of a FastAPI project.
We've covered a lot of ground in this post. We've discussed what FastAPI is and why it's a great choice for our Todo App. We've also written our first endpoint and run our application.
In the next post, we'll start building the core functionality of our Todo App. Stay tuned, and happy coding!
If you would like to support me or buy me a beer feel free to join my Patreon jamesbmour
以上是FastAPI Todo 應用程式:設定您的 Todo 應用程式項目的詳細內容。更多資訊請關注PHP中文網其他相關文章!