flask 数据迁移
By reading this piece, you will learn about the fundamental concepts behind FastAPI
and the steps involved in transitioning from Flask
to FastAPI
. Side by side comparisons will be provided for your references. At the end of this tutorial, you should be able to migrate your Flask
server entirely into aFastAPI
server. For your information, FastAPI
framework is:
通过阅读本文,您将了解FastAPI
的基本概念以及从Flask
过渡到FastAPI
涉及的步骤。 将提供并排比较,以供您参考。 在本教程的最后,您应该能够将Flask
服务器完全迁移到FastAPI
服务器中。 供您参考, FastAPI
框架是:
“… a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints.”
“…一种现代,快速(高性能)的网络框架,用于基于标准Python类型提示使用Python 3.6+构建API。”
The official documentation outlines the following key features for FastAPI
(Estimation is based on tests on an internal development team, building production applications.):
官方文档概述了FastAPI
的以下关键功能(估算基于内部开发团队的测试,构建生产应用程序。):
Fast: Very high performance, on par with
NodeJS
andGo
.快速:很高的性能,与
NodeJS
和Go
相当。- Fast to code: Increase the speed to develop features by about 200% to 300%. 快速编写代码:将功能开发的速度提高大约200%至300%。
- Fewer bugs: Reduce about 40% of human (developer) induced errors. 更少的错误:减少约40%的人为错误(开发人员)。
- Intuitive: Great editor support. Completion everywhere. Less time debugging. 直观:强大的编辑器支持。 完成无处不在。 调试时间更少。
- Easy: Designed to be easy to use and learn. Less time reading docs. 简易:旨在易于使用和学习。 减少阅读文档的时间。
- Short: Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs. 简短:减少代码重复。 每个参数声明中的多个功能。 更少的错误。
- Robust: Get production-ready code. With automatic interactive documentation. 稳健:获取可用于生产环境的代码。 具有自动交互式文档。
Standards-based: Based on (and fully compatible with) the open standards for APIs: OpenAPI (previously known as Swagger) and JSON Schema.
基于标准:基于(并完全兼容)API的开放标准: OpenAPI (以前称为Swagger)和JSON Schema 。
As you have known, Flask
is a micro web framework which is 100% WSGI
(Web Server Gateway Interface). Moving forward, ASGI
(Asynchronous Server Gateway Interface) is the spiritual successor to WSGI
as ASGI
is able to achieve high throughput in IO-bound contexts with supports for HTTP/2
and WebSockets
, which cannot be handled by WSGI
.
众所周知, Flask
是一个微型Web框架,它是100% WSGI
(Web服务器网关接口)。 向前进, ASGI
(异步服务器网关接口)是WSGI
的精神继承者,因为ASGI
能够在IO绑定上下文中实现对HTTP/2
和WebSockets
支持的高吞吐量,而WSGI
无法处理。
With the advancement of technology, a lightning-fast ASGI
server called Uvicorn
was born. However, Uvicorn
is just a web server without any routing capabilities. Then, Starlette
came along, which provides a complete ASGI
toolkit on top an ASGI
server such as Uvicorn
, Daphne
, or Hypercorn
. If you are looking for a direct comparison, Starlette
is an ASGI
web framework while Flask
is a WSGI
web framework.
随着技术的进步,诞生了名为Uvicorn
的闪电般快速的ASGI
服务器。 但是, Uvicorn
只是一个没有任何路由功能的Web服务器。 然后, Starlette
来了,它提供了一个完整的ASGI
顶部的工具上ASGI
服务器如Uvicorn
, Daphne
,或Hypercorn
。 如果您想直接比较, Starlette
是ASGI
Web框架,而Flask
是WSGI
Web框架。
The FastAPI
framework makes full use of the functionalities behind Starlette
and the programming style of Flask
making it a Flask-like ASGI
web framework. In addition, it incorporate the following features which make it an ideal web framework for creating RESTful API:
FastAPI
框架充分利用了Starlette
背后的功能和Flask
的编程风格,使其成为类似Flask的ASGI
Web框架。 此外,它还整合了以下功能,使其成为创建RESTful API的理想Web框架:
Data validation: Uses
Pydantic
which enforces type hints at runtime. It also provides user friendly errors when data is invalid.数据验证:使用
Pydantic
,在运行时强制类型提示。 当数据无效时,它还会提供用户友好的错误。Documentation: Has support for automatic data model documentation with JSON Schema. Comes with either
Swagger UI
orReDoc
.文档:支持使用JSON Schema的自动数据模型文档。 随附
Swagger UI
或ReDoc
。
By using FastAPI
instead of Flask
, your web server will have data validation and data model documentation readily available even though the amount of time you spend to code in either framework is more or less the same. Hence, it helps to improve the development process in your team.
通过使用FastAPI
而不是Flask
,即使您在任一框架中进行编码所花费的时间或多或少都相同,您的Web服务器仍可以轻松获得数据验证和数据模型文档。 因此,它有助于改善团队中的开发过程。
Let’s proceed to the next section and start installing the necessary modules.
让我们继续下一节并开始安装必要的模块。
1.设定 (1. Setup)
It is highly recommended to create a virtual environment before you continue with the installation. If you intend to test out FastAPI, you only need to install FastAPI
and Uvicorn
. The rest of the packages are optional and only required if you intend to follow along with this tutorial.
强烈建议您在继续安装之前创建一个虚拟环境。 如果您打算测试FastAPI,则只需安装FastAPI
和Uvicorn
。 其余的软件包是可选的,只有在您打算遵循本教程的情况下才需要。
FastAPI (FastAPI)
Installation