flask 数据迁移_从Flask顺利迁移到FastAPI

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 and Go.

    快速:很高的性能,与NodeJSGo相当。

  • 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/2WebSockets支持的高吞吐量,而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服务器如UvicornDaphne ,或Hypercorn 。 如果您想直接比较, StarletteASGI Web框架,而FlaskWSGI 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 or ReDoc.

    文档:支持使用JSON Schema的自动数据模型文档。 随附Swagger UIReDoc

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,则只需安装FastAPIUvicorn 。 其余的软件包是可选的,只有在您打算遵循本教程的情况下才需要。

FastAPI (FastAPI)

Installation

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值