FastAPI+Vue3零基础开发ERP系统项目实战课 20240906 上课笔记 fastapi的各种练习
回顾练习
用FastAPI写一个接口,这个接口能够返回九九乘法表的字符串。
获取九九乘法表:
for i in range(1, 10):
for j in range(1, i + 1):
print(f"{j} x {i} = {j * i}", end="\t")
print()
# 得到字符串
talbe99 = ""
for i in range(1, 10):
for j in range(1, i + 1):
talbe99 += f"{j} x {i} = {j * i}\t"
talbe99 += "\n"
print(talbe99)
def get_99table():
talbe99 = ""
for i in range(1, 10):
for j in range(1, i + 1):
talbe99 += f"{j} x {i} = {j * i}\t"
talbe99 += "\n"
return talbe99
print(get_99table())
使用FastAPI封装为接口。
from fastapi import FastAPI
app = FastAPI()
def get_99table():
talbe99 = ""
for i in range(1, 10):
for j in range(1, i + 1):
talbe99 += f"{j} x {i} = {j * i}\t"
talbe99 += "\n"
return talbe99
@app.get("/")
async def root():
return {"message": get_99table()}
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host='0.0.0.0', port=8888)
练习
使用FastAPI封装一个接口,这个接口支持传入两个整数a和b,接口返回a和b相加的结果。
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root(a: int, b: int):
return {"message": a + b}
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host='0.0.0.0', port=8888)
练习
求幂运算。传入两个数a和b,接口里面求a的b次方,然后将结果返回。
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root(a: int, b: int):
return {"message": a ** b}
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host='0.0.0.0', port=8888)
升级加法
原来只能实现两个数相加,我现在想要实现五个数相加。
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root(a: int, b: int, c: int, d: int, e: int):
return {"message": a + b + c + d + e}
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host='0.0.0.0', port=8888)
JSON参数
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class AddRequest(BaseModel):
arr: list[int]
@app.get("/")
async def root(req_data: AddRequest):
return req_data
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host='0.0.0.0', port=8888)
练习
把刚才五个数的加法,改成通过JSON传递,对JSON中的arr数组进行求和并返回,这样能够支持任意个数的加法。
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class AddRequest(BaseModel):
arr: list[int]
@app.get("/")
async def root(req_data: AddRequest):
return {"result": sum(req_data.arr)}
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host='0.0.0.0', port=8888)
练习
刚才:传入一个数组,求数组所有的和。
现在:传入一个数组,求这个数组的最大值和最小值。
内置方法:
- sum:求和
- max:求最大值
- min:求最小值
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class AddRequest(BaseModel):
arr: list[int]
@app.get("/")
async def root(req_data: AddRequest):
return {
"sum": sum(req_data.arr),
"min": min(req_data.arr),
"max": max(req_data.arr),
}
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host='0.0.0.0', port=8888)
练习
传入一个数组,求平均值。
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class AddRequest(BaseModel):
arr: list[int]
@app.get("/")
async def root(req_data: AddRequest):
return {
"sum": sum(req_data.arr),
"min": min(req_data.arr),
"max": max(req_data.arr),
"avg": sum(req_data.arr) / len(req_data.arr),
}
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host='0.0.0.0', port=8888)