随笔分类 -  FastAPI

摘要:from fastapi import FastAPI from fastapi.responses import RedirectResponse app = FastAPI() @app.get("/") def go_to_baidu(): return RedirectResponse("h 阅读全文
posted @ 2023-03-30 14:21 LeoShi2020 阅读(162) 评论(0) 推荐(0) 编辑
摘要:1. StreamingResponse支持文件类型的操作 from fastapi import FastAPI from fastapi.responses import StreamingResponse app = FastAPI() @app.get("/") def index(): d 阅读全文
posted @ 2023-03-30 14:20 LeoShi2020 阅读(582) 评论(0) 推荐(0) 编辑
摘要:1.PlainTextResponse用来响应纯文本的数据 from fastapi import FastAPI from fastapi.responses import PlainTextResponse app = FastAPI() @app.get("/", response_class 阅读全文
posted @ 2023-03-30 14:17 LeoShi2020 阅读(164) 评论(0) 推荐(0) 编辑
摘要:1. 文件结构 ├── main.py └── routers ├── blog.py └── user.py 2. blog.py from fastapi import APIRouter router = APIRouter() @router.get("/blogs") def blogs( 阅读全文
posted @ 2023-03-30 13:40 LeoShi2020 阅读(152) 评论(0) 推荐(0) 编辑
摘要:代码结构 ── sql_app ├── main.py # 程序入口 ├── crud.py # 增删查改的方法 ├── database.py # 数据库相关 ├── models.py # ORM模型类相关 └── schemas.py # Pydantic的BaseModel,校验相关 阅读全文
posted @ 2023-03-28 20:51 LeoShi2020 阅读(35) 评论(0) 推荐(0) 编辑
摘要:import typing from fastapi import FastAPI, HTTPException, Depends import pymysql from sqlalchemy import create_engine, Column, String, Integer from sq 阅读全文
posted @ 2023-03-28 20:37 LeoShi2020 阅读(179) 评论(0) 推荐(0) 编辑
摘要:import typing from fastapi import FastAPI, HTTPException, Depends import pymysql from sqlalchemy import create_engine, Column, String, Integer from sq 阅读全文
posted @ 2023-03-28 19:21 LeoShi2020 阅读(86) 评论(0) 推荐(0) 编辑
摘要:from fastapi import Depends, FastAPI app = FastAPI(title="依赖注入yield", description="在路径操作函数结束时,会自动关闭db连接回收资源。及时在路径函数会出现异常报错,最终也会关闭连接。") def get_db(): d 阅读全文
posted @ 2023-03-26 22:14 LeoShi2020 阅读(24) 评论(0) 推荐(0) 编辑
摘要:from fastapi import Depends, FastAPI app = FastAPI(title="基于对象的依赖注入", description="检查指定的文本是否在查询参数q中") class FixedContentQueryChecker: def __init__(sel 阅读全文
posted @ 2023-03-26 21:59 LeoShi2020 阅读(35) 评论(0) 推荐(0) 编辑
摘要:from fastapi import Depends, FastAPI app = FastAPI() ''' 基于类的依赖注入 ''' BOOKS = [{"id": i, "name": f"book{i}", "status": i % 4 != 0} for i in range(1, 1 阅读全文
posted @ 2023-03-26 21:41 LeoShi2020 阅读(34) 评论(0) 推荐(0) 编辑
摘要:1. 路径装饰器 from fastapi import FastAPI, Header, HTTPException, Depends,status app = FastAPI() def verify_token(x_token: str = Header()): if x_token != " 阅读全文
posted @ 2023-03-26 21:03 LeoShi2020 阅读(126) 评论(0) 推荐(0) 编辑
摘要:from fastapi import Depends, FastAPI app = FastAPI() ''' 依赖注入缓存现象 - 依赖条件`get_num`被依赖了两次,但是你会发现其内部打印语句只打印了一次。也就是说, 第二次使用这个依赖条件时FastAPI并没有真正执行这个函数,而是直接使 阅读全文
posted @ 2023-03-26 20:46 LeoShi2020 阅读(187) 评论(0) 推荐(0) 编辑
摘要:from typing import Union from fastapi import Depends, FastAPI app = FastAPI() ''' 嵌套注入 - 路径函数get_name需要的形参`username_or_nickname`有依赖条件,所以FastAPI会调用 `us 阅读全文
posted @ 2023-03-26 20:35 LeoShi2020 阅读(30) 评论(0) 推荐(0) 编辑
摘要:from fastapi import FastAPI, Depends app = FastAPI(title="依赖注入") ''' 依赖注入 - 共享一块相同逻辑的代码块 - 共享数据库连接 - 权限认证,登录状态认证 ''' BOOKS = [{"id": i, "name": f"book 阅读全文
posted @ 2023-03-26 20:02 LeoShi2020 阅读(76) 评论(0) 推荐(0) 编辑
摘要:import typing from fastapi import FastAPI, Form, File, UploadFile from pydantic import BaseModel app = FastAPI(title="注册接口") ''' 1. 需要输入 账号 密码 头像 ''' 阅读全文
posted @ 2023-03-26 18:25 LeoShi2020 阅读(21) 评论(0) 推荐(0) 编辑
摘要:import typing from fastapi import FastAPI, File, UploadFile app = FastAPI(title="Form表单") ''' 上传多个文件 ''' @app.post("/files",summary="通过内存缓存上传多个文件") de 阅读全文
posted @ 2023-03-26 17:57 LeoShi2020 阅读(50) 评论(0) 推荐(0) 编辑
摘要:import typing from fastapi import FastAPI, File, UploadFile app = FastAPI(title="Form表单") ''' 上传文件为可选项 ''' @app.post("/upload_large_file", summary="上传 阅读全文
posted @ 2023-03-26 17:38 LeoShi2020 阅读(86) 评论(0) 推荐(0) 编辑
摘要:from fastapi import FastAPI,Form,File,UploadFile app = FastAPI(title="Form表单") ''' Form表单接收数据 ''' @app.post("/login",summary="登录") def login(username 阅读全文
posted @ 2023-03-26 17:04 LeoShi2020 阅读(182) 评论(0) 推荐(0) 编辑
摘要:import typing from fastapi import FastAPI, Query, HTTPException, status from fastapi.encoders import jsonable_encoder from pydantic import BaseModel a 阅读全文
posted @ 2023-03-26 12:51 LeoShi2020 阅读(14) 评论(0) 推荐(0) 编辑
摘要:import typing import json from fastapi import FastAPI, Response from fastapi.encoders import jsonable_encoder from datetime import datetime from pydan 阅读全文
posted @ 2023-03-25 17:41 LeoShi2020 阅读(169) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示