Show HN:FastOpenAPI——针对多种Python框架的自动化文档生成工具
Show HN: FastOpenAPI – automated docs for many Python frameworks

原始链接: https://github.com/mr-fatalyst/fastopenapi

FastOpenAPI是一个使用Pydantic生成OpenAPI模式的库,其灵感来源于FastAPI的开发者友好型方法。它简化了跨框架(如Falcon、Flask、Quart、Sanic、Starlette和Tornado)的API文档和数据验证。 使用它,请使用你需要的框架安装FastOpenAPI(例如,`pip install fastopenapi[flask]`)。然后,使用FastOpenAPI提供的特定于框架的路由器定义你的API路由,用`@router.get`、`@router.post`等装饰函数。使用Pydantic模型作为请求和响应的数据结构。 设置好你的应用程序后,文档会自动在`/docs`(Swagger UI)和`/redoc`(ReDoc UI)处提供。FastOpenAPI利用Pydantic进行数据验证,从而促进了更简洁的代码和强大的API设计。该库提供了类似FastAPI的路由体验,并为每个支持的框架提供了集成示例。

Mr_Fatalyst 在 Hacker News 上介绍了 FastOpenAPI,这是一个新的 Python 库,旨在自动为 Flask、Sanic、Falcon 和 Starlette 等多个 Web 框架生成 OpenAPI 文档。FastOpenAPI 受 FastAPI 直观的路由机制启发,旨在提供类似的简化 OpenAPI 文档创建方法,从而避免在使用不同框架时需要单独的解决方案。它本质上为偏好其他框架的开发者带来了 FastAPI 风格的路由体验。该项目目前正在积极开发中,创建者正在积极寻求社区反馈和测试以进一步完善和改进项目。该项目已在 Github 上发布。

原文

Logo

FastOpenAPI is a library for generating and integrating OpenAPI schemas using Pydantic and various frameworks.

This project was inspired by FastAPI and aims to provide a similar developer-friendly experience.

PyPI Downloads


Install only FastOpenAPI:

Install FastOpenAPI with a specific framework:

pip install fastopenapi[falcon]
pip install fastopenapi[flask]
pip install fastopenapi[sanic]
pip install fastopenapi[starlette]
pip install fastopenapi[tornado]

Step 1. Create an application

  • Create the main.py file
  • Copy the code from an example
  • For some examples uvicorn is required (pip install uvicorn)
  • Falcon

    Click to expand the Falcon Example
    import falcon.asgi
    import uvicorn
    from pydantic import BaseModel
    
    from fastopenapi.routers import FalconRouter
    
    app = falcon.asgi.App()
    router = FalconRouter(app=app)
    
    
    class HelloResponse(BaseModel):
        message: str
    
    
    @router.get("/hello", tags=["Hello"], status_code=200, response_model=HelloResponse)
    async def hello(name: str):
        """Say hello from Falcon"""
        return HelloResponse(message=f"Hello, {name}! It's Falcon!")
    
    
    if __name__ == "__main__":
        uvicorn.run(app, host="127.0.0.1", port=8000)
  • Flask

    Click to expand the Flask Example
    from flask import Flask
    from pydantic import BaseModel
    
    from fastopenapi.routers import FlaskRouter
    
    app = Flask(__name__)
    router = FlaskRouter(app=app)
    
    
    class HelloResponse(BaseModel):
        message: str
    
    
    @router.get("/hello", tags=["Hello"], status_code=200, response_model=HelloResponse)
    def hello(name: str):
        """Say hello from Flask"""
        return HelloResponse(message=f"Hello, {name}! It's Flask!")
    
    
    if __name__ == "__main__":
        app.run(port=8000)
  • Quart

    Click to expand the Quart Example
    from pydantic import BaseModel
    from quart import Quart
    
    from fastopenapi.routers import QuartRouter
    
    app = Quart(__name__)
    router = QuartRouter(app=app)
    
    
    class HelloResponse(BaseModel):
        message: str
    
    
    @router.get("/hello", tags=["Hello"], status_code=200, response_model=HelloResponse)
    async def hello(name: str):
        """Say hello from Quart"""
        return HelloResponse(message=f"Hello, {name}! It's Quart!")
    
    
    if __name__ == "__main__":
        app.run(port=8000)
  • Sanic

    Click to expand the Sanic Example
    from pydantic import BaseModel
    from sanic import Sanic
    
    from fastopenapi.routers import SanicRouter
    
    app = Sanic("MySanicApp")
    router = SanicRouter(app=app)
    
    
    class HelloResponse(BaseModel):
        message: str
    
    
    @router.get("/hello", tags=["Hello"], status_code=200, response_model=HelloResponse)
    async def hello(name: str):
        """Say hello from Sanic"""
        return HelloResponse(message=f"Hello, {name}! It's Sanic!")
    
    
    if __name__ == "__main__":
        app.run(host="0.0.0.0", port=8000)
  • Starlette

    Click to expand the Starlette Example
    import uvicorn
    from pydantic import BaseModel
    from starlette.applications import Starlette
    
    from fastopenapi.routers import StarletteRouter
    
    app = Starlette()
    router = StarletteRouter(app=app)
    
    
    class HelloResponse(BaseModel):
        message: str
    
    
    @router.get("/hello", tags=["Hello"], status_code=200, response_model=HelloResponse)
    async def hello(name: str):
        """Say hello from Starlette"""
        return HelloResponse(message=f"Hello, {name}! It's Starlette!")
    
    if __name__ == "__main__":
        uvicorn.run(app, host="127.0.0.1", port=8000)
  • Tornado

    Click to expand the Tornado Example
    import asyncio
    
    from pydantic import BaseModel
    from tornado.web import Application
    
    from fastopenapi.routers.tornado import TornadoRouter
    
    app = Application()
    
    router = TornadoRouter(app=app)
    
    
    class HelloResponse(BaseModel):
        message: str
    
    
    @router.get("/hello", tags=["Hello"], status_code=200, response_model=HelloResponse)
    def hello(name: str):
        """Say hello from Tornado"""
        return HelloResponse(message=f"Hello, {name}! It's Tornado!")
    
    
    async def main():
        app.listen(8000)
        await asyncio.Event().wait()
    
    
    if __name__ == "__main__":
        asyncio.run(main())

Launch the application:

Once launched, the documentation will be available at:

Swagger UI:

http://127.0.0.1:8000/docs

ReDoc UI:

http://127.0.0.1:8000/redoc

  • Generate OpenAPI schemas with Pydantic v2.
  • Data validation using Pydantic models.
  • Supports multiple frameworks: Falcon, Flask, Quart, Sanic, Starlette, Tornado.
  • Proxy routing provides FastAPI-style routing

Explore the Docs for an overview of FastOpenAPI, its core components, and usage guidelines. The documentation is continuously updated and improved.


Examples of integration and detailed usage for each framework are available in the examples directory.


📊 Quick & Dirty Benchmarks

Fast but not perfect benchmarks. Check the benchmarks directory for details.


✅ Development Recommendations

  • Use Pydantic models for strict typing and data validation.
  • Follow the project structure similar to provided examples for easy scalability.
  • Regularly update dependencies and monitor library updates for new features.

If you have suggestions or find a bug, please open an issue or create a pull request on GitHub.


This project is licensed under the terms of the MIT license.

联系我们 contact @ memedata.com