没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
FastAPI
[https://fastapi.tiangolo.com]
FastAPI framework, high performance, easy to learn, fast to code, ready for
production
build
build
passing
passing
[https://travis-ci.com/tiangolo/fastapi]
codecov
codecov
99%
99%
[https://codecov.io/gh/tiangolo/fastapi]
pypi package
pypi package
0.49.0
0.49.0
[https://pypi.org/project/fastapi] [https://gitter.im/tiangolo/fastapi?
utm_source=badge&utm_medium=badge&utm_campaign=pr-
badge&utm_content=badge]
Documentation: https://fastapi.tiangolo.com [https://fastapi.tiangolo.com]
Source Code: https://github.com/tiangolo/fastapi
[https://github.com/tiangolo/fastapi]
FastAPI is a modern, fast (high-performance), web framework for building APIs
with Python 3.6+ based on standard Python type hints.
The key features are:
Fast: Very high performance, on par with NodeJS and Go (thanks to
Starlette and Pydantic). One of the fastest Python frameworks available
[#performance].
Fast to code: Increase the speed to develop features by about 200% to
300% *.
Fewer bugs: Reduce about 40% of human (developer) induced errors. *
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[↪].
Opinions
"[...] I'm using FastAPI a ton these days. [...] I'm actually planning to use it for all
of my team's ML services at Microsoft. Some of them are getting integrated into
the core Windows product and some Office products."
Kabir Khan - Microsoft
[https://github.com/tiangolo/fastapi/pull/26]
"I’m over the moon excited about FastAPI. It’s so fun!"
Brian Okken - Python Bytes
[https://pythonbytes.fm/episodes/show/123/time-to-right-the-py-
wrongs?time_in_sec=855] podcast host
[https://twitter.com/brianokken/status/1112220079972728832]
* estimation based on tests on an internal development team, building production applications.
(ref)
(ref)
"Honestly, what you've built looks super solid and polished. In many ways, it's
what I wanted Hug to be - it's really inspiring to see someone build that."
Timothy Crosley - Hug [http://www.hug.rest/] creator
[https://news.ycombinator.com/item?id=19455465]
"If you're looking to learn one modern framework for building REST APIs, check
out FastAPI [...] It's fast, easy to use and easy to learn [...]"
"We've switched over to FastAPI for our APIs [...] I think you'll like it [...]"
Ines Montani - Matthew Honnibal - Explosion AI [https://explosion.ai]
founders - spaCy [https://spacy.io] creators
[https://twitter.com/_inesmontani/status/1144173225322143744] -
[https://twitter.com/honnibal/status/1144031421859655680]
"We adopted the FastAPI library to spawn a REST server that can be queried to
obtain predictions. [for Ludwig]"
Piero Molino, Yaroslav Dudin, and Sai Sumanth Miryala - Uber
[https://eng.uber.com/ludwig-v0-2/]
Requirements
Python 3.6+
FastAPI stands on the shoulders of giants:
Starlette[↪] for the web parts.
Pydantic[↪] for the data parts.
Installation
(ref)
(ref)
(ref)
(ref)
You will also need an ASGI server, for production such as Uvicorn[↪] or
Hypercorn[↪].
Example
Create it
Create a file main.py with:
pip install fastapi
pip install uvicorn
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: str = None):
return {"item_id": item_id, "q": q}
Or use async def ...
If your code uses async / await , use async def :
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
async def read_item(item_id: int, q: str = None):
return {"item_id": item_id, "q": q}
Run it
Run the server with:
Check it
Open your browser at http://127.0.0.1:8000/items/5?q=somequery[↪].
You will see the JSON response as:
You already created an API that:
Receives HTTP requests in the paths / and /items/{item_id} .
Both paths take GET operations (also known as HTTP methods).
The path /items/{item_id} has a path parameter item_id that should be
an int .
The path /items/{item_id} has an optional str query parameter q .
Interactive API docs
Note:
If you don't know, check the "In a hurry?" section about async and await in the docs
[https://fastapi.tiangolo.com/async/#in-a-hurry].
uvicorn main:app --reload
About the command uvicorn main:app --reload ...
The command uvicorn main:app refers to:
main : the file main.py (the Python "module").
app : the object created inside of main.py with the line app = FastAPI() .
--reload : make the server restart after code changes. Only do this for development.
{"item_id": 5, "q": "somequery"}
剩余609页未读,继续阅读
资源评论
dna911
- 粉丝: 16
- 资源: 39
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功