FASTAPI学习笔记

FASTAPI

官方文档:http://fastapi.tiangolo.com

Github:http://github.com/tiangolo/fastapi

示例代码:http://github.com/oinsd/FastAPI-Learning-Example


#依赖包:
pip install fastapi
pip install uvicorn

#最简单的fastapi文件
#main.py
#-*- coding: UTF-8 -*-
from fastapi import FastAPI
app = FastAPI()

@app.get("/")

#告诉FastAPI是正下方的功能是负责处理该去请求:
#路径 /
#使用get操作
#其他操作有
"#@app.post(),@app.put(),@app.delet(),@app.options(),@app.head(),@app.patch,@app.trace()"


async def root():#接收到url请求时,FastAPI就会调用,async再这样情况下是一个功能
      return{"message":"helloworld,FastAPI"}
      
if _name_ == '_main_':
    import uvicorn
    uvicorn.run(app,host="127.0.0.1",port=8000)
    

template_and_url模板渲染与url

#任何所需要的模板引擎可以和FastAPI一起使用
#exp:pip install jinja2
from starlette.requests import Request
from fastapi import FastAPI
from starlette.templating import Jinja2Templates

app = FastAPI()
templates = Jinja2Templates(directory="templates")#生成示例

@app.get("/")#get请求
async def main(request: Request):
#进入get请求后进入模板
    return templates.TemplateResponse('index.html', {'request': request, 'hello': 'HI...'})

@app.get("/{item_id}/")
async def item_id(request: Request, item_id):
    return templates.TemplateResponse('index.html', {'request': request, "item_id": item_id})


if __name__ == '__main__':
    import uvicorn
    uvicorn.run(app, host="127.0.0.1", port=8000)


# uvicorn FastAPI_No.1_helloworld.py:app --reload

from表单与登录界面

# -*- coding: UTF-8 -*-
from starlette.requests import Request
from fastapi import FastAPI, Form
from starlette.templating import Jinja2Templates

app = FastAPI()
templates = Jinja2Templates(directory="templates")#生成jinja2的示例


@app.post("/user/")#un来源于post页面输入的值,通过from函数,参数名要与html一致
async def form_text(request: Request, username: str = Form(...), password: str = Form(...)):
    
    print('username',username)
    print('password',password)
    
    # return {'text_1':text_1 , 'text_2': text_2}
    return templates.TemplateResponse('index.html', {'request': request, 'username': username, 'password': password})


@app.get("/")
async def main(request: Request):
    return templates.TemplateResponse('post.html', {'request': request})

if __name__ == '__main__':
    import uvicorn
    uvicorn.run(app, host="127.0.0.1", port=8000)

上传文件

# -*- coding: UTF-8 -*-
from typing import List
from starlette.requests import Request
from fastapi import FastAPI, Form, File, UploadFile
from starlette.templating import Jinja2Templates

app = FastAPI()
templates = Jinja2Templates(directory="templates")


@app.post("/files/")#多文件上传
async def files(
                    request: Request,
                    files_list: List[bytes]         = File(...),#引入list可以上传多个文件
                    files_name: List[UploadFile]    = File(...),
                ):
    return templates.TemplateResponse("index.html", 
            {
                "request":      request,#请求参数
                "file_sizes":   [len(file) for file in files_list], #文件的大小
                "filenames":    [file.filename for file in files_name], #以文件名属性罗列出上传的文件   
             })


@app.post("/create_file/")#单个文件上传
async def create_file(
                        request: Request,
                        file: bytes         = File(...), 
                        fileb: UploadFile   = File(...), 
                        notes: str          = Form(...),
                      ):
    return templates.TemplateResponse("index.html", 
            {
                "request":               request,
                "file_size":             len(file),
                "notes":                 notes,
                "fileb_content_type":    fileb.content_type,
             })


@app.get("/")
async def main(request: Request):
    return templates.TemplateResponse('post.html', {'request': request})


if __name__ == '__main__':
    import uvicorn
    uvicorn.run(app, host="127.0.0.1", port=8000)


html模板下载:https://v3.bootcss.com/

bootstrap-3.4.1.zip

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值