有" />

国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

15158846557 在線咨詢 在線咨詢
15158846557 在線咨詢
所在位置: 首頁 > 營銷資訊 > 網(wǎng)站運(yùn)營 > API 接口開發(fā)沒那么難,Python FastApi Web 框架教程來了!

API 接口開發(fā)沒那么難,Python FastApi Web 框架教程來了!

時間:2023-05-27 12:00:01 | 來源:網(wǎng)站運(yùn)營

時間:2023-05-27 12:00:01 來源:網(wǎng)站運(yùn)營

API 接口開發(fā)沒那么難,Python FastApi Web 框架教程來了?。?blockquote data-first-child data-pid="m8_-YmPy">歡迎關(guān)注 @Python與數(shù)據(jù)挖掘 ,專注 Python、數(shù)據(jù)分析、數(shù)據(jù)挖掘、好玩工具!大家好,前幾天分享了一篇文章:API接口開發(fā)其實特簡單,Python Flask Web 框架教程來了

有粉絲朋友就私信,是否可以講一下FastAPI,今天我就來分享一下如何使用 FastApi ,喜歡記得收藏、點(diǎn)贊、關(guān)注。

一、FastApi

1.FastAPI可以做什么

它由 Sebastian Ramirez 開發(fā)

2.為什么要學(xué)習(xí) FastAPI

二、準(zhǔn)備工作

1.編譯器工具

Python、Pycharm

2.python安裝教程

https://www.runoob.com/python/python-install.html

3.pycharm安裝教程

runoob.com/w3cnote/pycharm-windows-install.html

4.安裝虛擬環(huán)境

1、創(chuàng)建項目工程

2、安裝環(huán)境

3、安裝fastapi

三、教程

1.開啟服務(wù)和接口訪問

main.py

import uvicornfrom fastapi import FastAPIapp=FastAPI()if __name__ == '__main__': uvicorn.run(app)服務(wù)器運(yùn)行

添加接口

main

import uvicornfrom fastapi import FastAPIapp=FastAPI()# 添加首頁@app.get("/")def index(): return "This is Home Page."if __name__ == '__main__': uvicorn.run(app)重新運(yùn)行項目


2.json數(shù)據(jù)

重新項目

json數(shù)據(jù)格式解析

https://www.json.cn/

3.api文檔在線生成


文檔解析


文檔備注信息

4.發(fā)送請求

POST請求

使用ApiPost接口測試工具來訪問接口

定義多種請求格式

5.獲取URL參數(shù)







main.py

import uvicornfrom fastapi import FastAPIapp=FastAPI()@app.get("/user/{id}")def user(id): return {"id":id}if __name__ == '__main__': uvicorn.run(app)

6.獲取請求頭參數(shù)

main.py

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)

7.表單獲取數(shù)據(jù)

安裝包

python-multipartmain.py

import 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)

8.自定義返回JSON信息

main.py

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)

8.自定義返回HTML




main.py

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)

9.自定義返回文件







main.py

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)

10.自定義返回HTML頁面

main.py

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)返回結(jié)果




11.代辦事項小案例

main.py

index.html

運(yùn)行項目

main.py

index.html

運(yùn)行項目

12.綁定數(shù)據(jù)庫

安裝 tortoise-orm

安裝 aiomysql

main.py


13.數(shù)據(jù)庫訪問

models.py

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.py

import 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")# 數(shù)據(jù)庫綁定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)運(yùn)行項目


14.數(shù)據(jù)庫寫入

main.py

運(yùn)行項目

數(shù)據(jù)庫里就多了個字段

此時就是顯示有問題

index.html

技術(shù)交流群

建了技術(shù)交流群!想要進(jìn)交流群、獲取資料、崗位推薦的同學(xué),可以直接加微信號:dkl88191。加的時候備注一下:研究方向 +學(xué)校/公司+本站,即可。然后就可以拉你進(jìn)群了。

強(qiáng)烈推薦大家關(guān)注 Python與數(shù)據(jù)挖掘 本站賬號和 Python學(xué)習(xí)與數(shù)據(jù)挖掘 微信公眾號,可以快速了解到最新優(yōu)質(zhì)文章。

技術(shù)交流、求職內(nèi)推、干貨匯總、與 5000+來自百度、阿里、頭條、騰訊等名企開發(fā)者互動交流~

文章推薦

盤點(diǎn)10個讓工作效率倍增且有趣的 Python庫!

prettytable:一款可完美格式化輸出的 Python 庫

推薦收藏!機(jī)器學(xué)習(xí)建模調(diào)參方法總結(jié)!

推薦收藏!23個機(jī)器學(xué)習(xí)最佳入門項目(附源代碼)

沒看完這11 條,別說你精通 Python 裝飾器!

60個 VS Code 神級插件,助力打造最強(qiáng)代碼編輯器!

太實用了!Schedule模塊, Python 周期任務(wù)神器!

這4款數(shù)據(jù)自動化探索 Python 神器,解決99%的數(shù)據(jù)分析問題!

20個數(shù)據(jù)分析師必會的數(shù)據(jù)模型,建議收藏!

真香??!讓 Python 編程起飛的 24 個神操作!

深度盤點(diǎn):30個用于深度學(xué)習(xí)、自然語言處理和計算機(jī)視覺的頂級 Python

全網(wǎng)超詳細(xì)!用戶畫像標(biāo)簽體系建設(shè)指南!

機(jī)器學(xué)習(xí)模型驗證,這3個 Python 包可輕松解決95%的需求!

精選 30 個炫酷的可視化大屏模板,拿走就用!

夠強(qiáng)大!Python 這款可視化大屏不足百行代碼!

深度盤點(diǎn):8000字詳細(xì)介紹 Python 中的 7 種交叉驗證方法

關(guān)鍵詞:教程

74
73
25
news

版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點(diǎn)擊下載Chrome瀏覽器
關(guān)閉