提示:
面向有一丢丢后端基础的
文章为个人学习心得 如有冒犯 call me!
快点做!!!!**
--------连--------接--------线----
由Sebastian Ramirez开发
--------连--------接--------线----
开发网站
开发 web API
做一个测试平台
做一个持续集成工具
生成文档
--------连--------接--------线----
FastAPI的使用方法和设计参考了几十个python明星项目 博采众长 吸收各家精华
FastAPI和Flask一样简约
FastAPI非常新 并且基于python3.6的类型声明系统
--------连--------接--------线----
自动生成文档
类型声明和数据检验
内置身份认证
性能强
原生支持异步
强大的依赖注入系统
GraphQL和WebSocket支持
--------连--------接--------线----
Python
Pycharm
https://www.runoob.com/python/python-install.html
runoob.com/w3cnote/pycharm-windows-install.html
main.py
import uvicorn
from fastapi import FastAPI
app=FastAPI()
if __name__ == '__main__':
uvicorn.run(app)
服务器运行
--------连--------接--------线----
添加接口
main
import uvicorn
from fastapi import FastAPI
app=FastAPI()
# 添加首页
@app.get("/")
def index():
return "This is Home Page."
if __name__ == '__main__':
uvicorn.run(app)
重新运行项目
--------连--------接--------线----
https://www.json.cn/
POST请求
使用ApiPost接口测试工具来访问接口
定义多种请求格式
import uvicorn
from fastapi import FastAPI
app=FastAPI()
@app.get("/user/{id}")
def user(id):
return {"id":id}
if __name__ == '__main__':
uvicorn.run(app)
--------连--------接--------线----
main.py
import uvicorn
from fastapi import FastAPI,Header
app=FastAPI()
@app.get("/user")
def user(id, token=Header(None)):
return {"id":id,"token":token}
if __name__ == '__main__':
uvicorn.run(app)
安装包
python-multipart
import uvicorn
from fastapi import FastAPI,Header,Form
app=FastAPI()
@app.get("/user")
def user(id, token=Header(None)):
return {"id":id,"token":token}
@app.post("/login")
def login(username=Form(None),password=Form(None)):
return {"data":{"username":username,"password":password}}
if __name__ == '__main__':
uvicorn.run(app)
import uvicorn
from fastapi import FastAPI
from fastapi.responses import JSONResponse
app=FastAPI()
@app.get("/user")
def user():
return JSONResponse(content={"msg":"get user"},
status_code=202,
headers={"a":"b"})
if __name__ == '__main__':
uvicorn.run(app)
import uvicorn
from fastapi import FastAPI
from fastapi.responses import JSONResponse,HTMLResponse
app=FastAPI()
@app.get("/user")
def user():
return JSONResponse(content={"msg":"get user"},
status_code=202,
headers={"a":"b"})
@app.get("/")
def user():
html_content="""
">Hello World
"""
return HTMLResponse(content=html_content)
if __name__ == '__main__':
uvicorn.run(app)
import uvicorn
from fastapi import FastAPI
from fastapi.responses import JSONResponse,HTMLResponse
from starlette.responses import FileResponse
app=FastAPI()
@app.get("/user")
def user():
return JSONResponse(content={"msg":"get user"},
status_code=202,
headers={"a":"b"})
@app.get("/")
def user():
html_content="""
">Hello World
"""
return HTMLResponse(content=html_content)
@app.get("/avatar")
def user():
avatar='./static/violet.jpg'
return FileResponse(avatar)
if __name__ == '__main__':
uvicorn.run(app)
import uvicorn
from fastapi import FastAPI,Request
from fastapi.templating import Jinja2Templates
app=FastAPI()
template=Jinja2Templates("pages")
@app.get("/")
def user(req:Request):
return template.TemplateResponse("index.html",context={"request":req})
if __name__ == '__main__':
uvicorn.run(app)
main.py
index.html
运行项目
main.py
index.html
运行项目
from tortoise import Model,fields
class Todo(Model):
id=fields.IntField(pk=True)
content=fields.CharField(max_length=500)
create_at=fields.DatetimeField(auto_now_add=True)
updated_at=fields.DatetimeField(auto_now=True)
import uvicorn
from fastapi import FastAPI, Request, Form
from fastapi.responses import RedirectResponse
from starlette.templating import Jinja2Templates
from tortoise.contrib.fastapi import register_tortoise
from dao.models import Todo
app=FastAPI()
template=Jinja2Templates("pages")
# 数据库绑定
register_tortoise(app,db_url="mysql://root:123456@localhost:3306/fastapi",
modules={"models":['dao.models']},
add_exception_handlers=True,
generate_schemas=True)
todos = ["写日记", "看电影", "玩游戏"]
# 添加首页
@app.get("/")
async def user(req:Request):
todos=await Todo.all()
print(todos)
return template.TemplateResponse("index.html",context={"request":req,"todos":todos})
@app.post("/todo")
def todo(todo=Form(None)):
todos.insert(0,todo)
return RedirectResponse("/",status_code=302)
if __name__ == '__main__':
uvicorn.run(app)