知乎上有人问,个低Python的架学哪个Web框架学习周期短,学习成本低?习周学习

很多人推荐Flask,老牌轻量级web框架,期短确实是成本初学者的首选。这几天我在Github上看到FastApi,个低觉得比Flask更轻量。架学
FastApi是习周学习这两年异军突起的网红web框架,适合新手快速入门。期短。成本
总的个低来说,FastAPI有三个优点:快、架学简、习周学习强。期短
它的成本自我标签就是:
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints.
为什么说快、高防服务器简、强呢?首先,FastApi利用异步和轻量级的特点,而且使用强类型,大大提升了性能,甚至可以媲美GO和NodeJS;其次能快速编程、人为bug少、调试成本低、设计简单,使得web搭建速度能提升2-3倍,很适合新手去操作。它和Django相比有哪些异同点?和Django相比,FastAPI 是一个轻量级的 Web 框架。
Django 是 battery included,虽然配置麻烦,但默认就带了许多功能,包括很好用的 ORM、亿华云计算migration 工具,也包括很多安全方面的中间件等等。还有比如模板系统、静态资源管理系统等等,对于一般的业务网站来说,Django 是开箱即用的。
FastAPI 则非常轻量,它本身什么都不带,没有 ORM、没有 migration,没有中间件,什么都没有。这是缺点也是有优点。
案例main.py:
from typing import Optional
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: Optional[str] = None):
return {"item_id": item_id, "q": q}运行服务器:
$ uvicorn main:app --reload
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [28720]
INFO: Started server process [28722]
INFO: Waiting for application startup.
INFO: Application startup complete.进入http://127.0.0.1:8000/docs,会看到自动生成的交互式 API 文档。服务器租用

学习文档:https://fastapi.tiangolo.com

GIthub地址:https://github.com/tiangolo/fastapi

(责任编辑:IT科技)