时间:2023-05-27 12:00:01 | 来源:网站运营
时间:2023-05-27 12:00:01 来源:网站运营
API 接口开发没那么难,Python FastApi Web 框架教程来了!:欢迎关注 @Python与数据挖掘 ,专注 Python、数据分析、数据挖掘、好玩工具!大家好,前几天分享了一篇文章:API接口开发其实特简单,Python Flask Web 框架教程来了
https://www.runoob.com/python/python-install.html
runoob.com/w3cnote/pycharm-windows-install.html
import uvicornfrom fastapi import FastAPIapp=FastAPI()if __name__ == '__main__': uvicorn.run(app)
服务器运行import uvicornfrom fastapi import FastAPIapp=FastAPI()# 添加首页@app.get("/")def index(): return "This is Home Page."if __name__ == '__main__': uvicorn.run(app)
重新运行项目https://www.json.cn/
import uvicornfrom fastapi import FastAPIapp=FastAPI()@app.get("/user/{id}")def user(id): return {"id":id}if __name__ == '__main__': uvicorn.run(app)
import uvicornfrom fastapi import FastAPI,Headerapp=FastAPI()@app.get("/user")def user(id, token=Header(None)): return {"id":id,"token":token}if __name__ == '__main__': uvicorn.run(app)
python-multipart
main.pyimport uvicornfrom fastapi import FastAPI,Header,Formapp=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 uvicornfrom fastapi import FastAPIfrom fastapi.responses import JSONResponseapp=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 uvicornfrom fastapi import FastAPIfrom fastapi.responses import JSONResponse,HTMLResponseapp=FastAPI()@app.get("/user")def user(): return JSONResponse(content={"msg":"get user"}, status_code=202, headers={"a":"b"})@app.get("/")def user(): html_content=""" <html> <body><p style="color:red">Hello World</p></body> </html> """ return HTMLResponse(content=html_content)if __name__ == '__main__': uvicorn.run(app)
import uvicornfrom fastapi import FastAPIfrom fastapi.responses import JSONResponse,HTMLResponsefrom starlette.responses import FileResponseapp=FastAPI()@app.get("/user")def user(): return JSONResponse(content={"msg":"get user"}, status_code=202, headers={"a":"b"})@app.get("/")def user(): html_content=""" <html> <body><p style="color:red">Hello World</p></body> </html> """ return HTMLResponse(content=html_content)@app.get("/avatar")def user(): avatar='./static/violet.jpg' return FileResponse(avatar)if __name__ == '__main__': uvicorn.run(app)
import uvicornfrom fastapi import FastAPI,Requestfrom fastapi.templating import Jinja2Templatesapp=FastAPI()template=Jinja2Templates("pages")@app.get("/")def user(req:Request): return template.TemplateResponse("index.html",context={"request":req})if __name__ == '__main__': uvicorn.run(app)
返回结果from tortoise import Model,fieldsclass 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)
main.pyimport uvicornfrom fastapi import FastAPI, Request, Formfrom fastapi.responses import RedirectResponsefrom starlette.templating import Jinja2Templatesfrom tortoise.contrib.fastapi import register_tortoisefrom dao.models import Todoapp=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)
运行项目关键词:教程