Upgrade to Pro — share decks privately, control downloads, hide ads and more …

たのしい FastAPI

たのしい FastAPI

社内勉強会用につくった FastAPI の布教資料です。
書けなかった内容は改めて会社ブログに書く予定です。

kimihiro_n

May 08, 2020
Tweet

More Decks by kimihiro_n

Other Decks in Programming

Transcript

  1. ͨͷ͍͠
    ര଎Ͱͭ͘ΕͯɺAPI࢓༷ॻʹ΋ͳΔʂ
    @kimihiro_n
    3෼μΠδΣετ൛

    View full-size slide

  2. Hello FastAPI

    View full-size slide

  3. FastAPI Ͱ HelloWorld
    from fastapi import FastAPI
    app = FastAPI()
    @app.get("/")
    def hello():
    return {"Hello": "World"}
    $ pip install fastapi uvicorn
    $ uvicorn main:app --reload
    ref. https://fastapi.tiangolo.com/

    View full-size slide

  4. FastAPIͷԿ͕͏Ε͍͔͠
    ● FlaskʹࣅͨγϯϓϧͳViewఆٛ
    ● ϗοτϦϩʔυରԠ
    ● ϦΫΤετɾϨεϙϯεͷܕఆٛ
    ● υΩϡϝϯτࣗಈੜ੒
    ● ඇಉظ(asyncio)ରԠ

    View full-size slide

  5. FastAPI Ͱܕఆٛ

    View full-size slide

  6. FastAPI Ͱ ܕΛ͚ͭΔ
    from fastapi import FastAPI
    from pydantic import BaseModel
    app = FastAPI()
    class Response(BaseModel):
    hello: str
    @app.get("/", response_model=Response)
    def hello():
    return Response(hello="world")

    View full-size slide

  7. FastAPI Ͱ ܕΛ͚ͭΔ
    from fastapi import FastAPI
    from pydantic import BaseModel
    app = FastAPI()
    class Response(BaseModel):
    hello: str
    @app.get("/", response_model=Response)
    def hello():
    return Response(hello="world")
    ϨεϙϯεͷܕΛఆٛ

    View full-size slide

  8. FastAPI Ͱ ܕΛ͚ͭΔ
    from fastapi import FastAPI
    from pydantic import BaseModel
    app = FastAPI()
    class Response(BaseModel):
    hello: str
    @app.get("/", response_model=Response)
    def hello():
    return Response(hello="world")
    View ͷϨεϙϯε͕
    ͜ͷܕͰ͋Δ͜ͱΛએݴ

    View full-size slide

  9. FastAPI Ͱ ܕΛ͚ͭΔ
    from fastapi import FastAPI
    from pydantic import BaseModel
    app = FastAPI()
    class Response(BaseModel):
    hello: str
    @app.get("/", response_model=Response)
    def hello():
    return Response(hello="world")
    σʔλΛೖΕͯ
    ͦͷ··ฦ͢

    View full-size slide

  10. ܕఆٛͰԿ͕ى͜Δͷ͔
    ● ࣙॻΛ͔ͭΘͳ͍҆શͳίʔσΟϯά
    ● ϨεϙϯεͷܕͷνΣοΫ
    ○ ܕ͕Ϛον͠ͳ͍৔߹Τϥʔʹ
    ○ ΋ͪΖΜϦΫΤετͷόϦσʔγϣϯ΋
    ● API υΩϡϝϯτ΁൓ө

    View full-size slide

  11. API ఆٛͷࣗಈੜ੒

    View full-size slide

  12. ● APIͷୟ͖ํ͕Θ͔Βͳ͍
    ● APIఆٛͱϨεϙϯε͕߹ͬͯͳ͍
    ● ࠷৽ͷ࢓༷ʹ௥ैͯ͠ͳ͍
    ● OpenAPI(Swagger)ͷYAML͸
    ਓྨ͕ॻ͘΋ͷ͡Όͳ͍
    อक͕େม
    APIυΩϡϝϯτͷ೰Έ

    View full-size slide

  13. Ͱ΋ FastAPIͳΒ…ʁ

    View full-size slide

  14. /doc ΁ΞΫηε

    View full-size slide

  15. OpenAPI ͷ࢓༷ॻ͕ࣗಈੜ੒
    ● ࢓༷ͱ࣮૷͕ৗʹಉظ
    ○ લड़ͷ௨ΓܕνΣοΫ΋͋ΔͷͰ҆৺
    ● SwaggerUI ্ͰAPIୟ͍ͯಈ࡞֬ೝͰ͖Δ
    ● ຊ൪؀ڥͰ͸ແޮԽ͢Δ͜ͱ΋Մೳ

    View full-size slide

  16. ຊ౰ʹඞཁͩͬͨ΋ͷ

    View full-size slide

  17. ● router ʹΑΔ View ϑΝΠϧ෼ׂ
    ● asyncio ʹΑΔඇಉظॲཧ
    ● middleware Ͱڞ௨ͷॲཧΛ௥Ճ
    ● Dependency ʹΑΔґଘ஫ೖ
    ● 200 Ҏ֎ͷϨεϙϯεͷఆٛ
    ॻ͚ͳ͔ͬͨ͜ͱ
    ଓ͖͸ Web Ͱ(Ծ)

    View full-size slide