在本博客系列中,读者将学习如何使用 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中文网其他相关文章!