zoukankan      html  css  js  c++  java
  • FastAPI框架入门 基本使用, 模版渲染, form表单数据交互, 文件交互, 静态文件配置

    FastAPI框架

    该框架的速度(天然支持异步)比一般的django和flask要快N多倍,号称可以比肩Go

    使用该框架需要保证你的python解释器版本是3.6及以上

    Ps:django3.X版本也支持异步,但是它的异步功能并没有真正的实现,还有很多bug

    官网: https://fastapi.tiangolo.com/

     

    安装

    pip3 install fastapi
    pip3 install unicorn

    基本使用(不能同时支持,get, post方法等要分开写)

    需要安装uvicorn模块

    from fastapi import FastAPI
    
    app = FastAPI()
    
    @app.get('/')  # 点get就支持get请求
    def read_root():
        return {"hello":'world'}
    
    if __name__ == '__main__':
        import uvicorn
        uvicorn.run(app,host='127.0.0.1',port=8080)

    模版渲染

    fastapi本身是没有模版渲染功能的,需要你借助于第三方的模版工具

    该框架默认情况下也是借助于jinja2来做模版渲染(flask也是使用jinja2, 如果用过flask, 默认是装过jinja2)

    安装

    pip3 install jinja2

    基本使用

    from starlette.requests import Request
    from fastapi import FastAPI
    from starlette.templating import Jinja2Templates
    
    app = FastAPI()
    # 挂载模版文件夹
    tmp = Jinja2Templates(directory='templates')
    
    @app.get('/')
    async def get_tmp(request:Request):  # async加了就支持异步  把Request赋值给request
        return tmp.TemplateResponse('index.html',
                                    {'request':request,  # 一定要返回request
                                     'args':'hello world'  # 额外的参数可有可无
                                     }
                                    )
    
    @app.get('/{item_id}/')  # url后缀 
    async def get_item(request:Request,item_id):
        return tmp.TemplateResponse('index.html',
                                    {'request':request,
                                     'kw':item_id
                                     })
    
    if __name__ == '__main__':
        import uvicorn
        uvicorn.run(app,host='127.0.0.1',port=8080)
    {#  index.html  #}
    <body>
    <h1>index页面</h1>
    <h1>{{ args }}</h1>
    <h1>{{ kw }}</h1>
    </body>

    form表单数据交互

    基本数据

    注意: 如果要使用request.form()支持表单“解析”,则为必需 python-multipart 。

    pip install python-multipart

    from starlette.requests import Request
    from fastapi import FastAPI,Form
    from starlette.templating import Jinja2Templates
    
    
    app = FastAPI()
    tmp = Jinja2Templates(directory='templates')
    
    
    @app.get('/')  # 接受get请求
    async def get_user(request:Request):
        return tmp.TemplateResponse('form.html',{'request':request})
    
    
    @app.post('/user/')  # 接受post请求
    async def get_user(request:Request,
                       username:str=Form(...),  # 直接去请求体里面获取username键对应的值并自动转化成字符串类型
                       pwd:int=Form(...)  # 直接去请求体里面获取pwd键对应的值并自动转化成整型
                       ):
        print(username,type(username))
        print(pwd,type(pwd))
        return tmp.TemplateResponse('form.html',{
            'request':request,
            'username':username,
            'pwd':pwd
        })
    
    
    if __name__ == '__main__':
        import uvicorn
        uvicorn.run(app,host='127.0.0.1',port=8080)
    {#  form.html  #}
    <body>
    <form action="/user/" method="post">
        <p>username<input type="text" name="username"></p>
        <p>password<input type="password" name="pwd"></p>
        <input type="submit">
    </form>
    <h1>{{ username }}</h1>
    <h1>{{ pwd }}</h1>
    </body>

    文件交互

    from starlette.requests import Request
    from fastapi import FastAPI, Form, File, UploadFile
    from starlette.templating import Jinja2Templates
    from typing import List
    
    app = FastAPI()
    # 挂载模板文件夹
    tmp = Jinja2Templates(directory='templates')
    
    @app.get('/')  # 接受get请求
    async def get_file(request: Request):
        return tmp.TemplateResponse('file.html', {'request': request})
    
    # 单个文件
    @app.post('/file/')  # 接受post请求
    async def get_user(request: Request,
                       file: bytes = File(...),       # # 把文件对象转为bytes类型
                       file_obj: UploadFile = File(...),    # UploadFile转为文件对象
                       info: str = Form(...)    # 获取普通键值对
                       ):
        return tmp.TemplateResponse('index.html', {
            'request': request,
            'file_size': len(file),
            'file_name': file_obj.filename,
            'info':info,
            'file_content_type':file_obj.content_type
        })
    
    # 多个文件
    @app.post('/files/')
    async def get_files(request:Request,    
                        files_list:List[bytes] = File(...),  # [文件1的二进制数据,文件2的二进制数据]
                        files_obj_list:List[UploadFile]=File(...)  # [file_obj1,file_obj2,....]
                        ):
        return tmp.TemplateResponse('index.html',
                                    {'request':request,
                                     'file_sizes':[len(file) for file in files_list],
                                     'file_names':[file_obj.filename for file_obj in files_obj_list]
                                     }
                                    )
    
    if __name__ == '__main__':
        import uvicorn
    
        uvicorn.run(app, host='127.0.0.1', port=8080)
    {#  file.html  #}
    <body>
    <h1>单个文件</h1>
    <form action="/file/" method="post" enctype="multipart/form-data">
        <input type="file" name="file">
        <input type="file" name="file_obj">
        <input type="text" name="info">
        <input type="submit">
    </form>
    
    <h1>多份个文件</h1>
    <form action="/files/" method="post" enctype="multipart/form-data">
        <input type="file" name="files_list" multiple>   {# multiple参数支持一次性传多个文件 #}
        <input type="file" name="files_obj_list" multiple>
        <input type="submit">
    </form>
    </body>
    
    {#  index.html  #}
    <body>
    <h2>单个文件</h2>
    <h1>{{ file_size }}</h1>
    <h1>{{ file_name }}</h1>
    <h1>{{ info }}</h1>
    <h1>{{ file_content_type }}</h1>
    
    <h2>多个文件</h2>
    <h1>{{ file_sizes }}</h1>
    <h1>{{ file_names }}</h1>
    </body>
    </html>

    静态文件配置

    需要安装aiofiles模块

    from starlette.staticfiles import StaticFiles
    # 挂载静态文件夹
    app.mount('/static',StaticFiles(directory='static'),name='static')    # mount挂载 第一个参数为应用的前缀,第二个为路径,第三个为别名
    
    
    # 前端
    <link rel="stylesheet" href="{{ url_for('static',path='/css/111.css') }}">    {# url_for后的第一个参数为别名 #}
    <script src="{{ url_for('static',path='/js/111.js') }}"></script>
  • 相关阅读:
    css 面试学习
    关于CSS的图像放大问题的解决,需要借助jQuery等直接用css3设置
    DEBUG使用
    crontab
    od
    dumpe2fs
    mke2fs
    dd
    GDB
    rm
  • 原文地址:https://www.cnblogs.com/ludingchao/p/12734935.html
Copyright © 2011-2022 走看看