[python3] tornado 使用swagger编写接口文档

  • python3.12

  • tornado 6.1

  • swagger 1.2.3

    pip install tornado-swagger==1.2.3

示例代码

python 复制代码
import tornado.ioloop
import tornado.web
from tornado_swagger.setup import setup_swagger


class ItemHandler(tornado.web.RequestHandler):
    def get(self, item_id):
        """
        ---
        tags:
          - Items
        summary: Get an item by ID
        description: Retrieve item details by its ID
        parameters:
          - name: item_id
            in: path
            required: true
            description: ID of the item
            type: integer
        responses:
          200:
            description: Successful operation
            schema:
              type: object
              properties:
                name:
                  type: string
                price:
                  type: number
          404:
            description: Item not found
        """
        item = {"name": "ExampleItem", "price": 99.99}
        print('item_id: ', item_id)
        if int(item_id) == 1:
            self.write(item)
        else:
            self.write({"error": "Item not found"})


def make_app():
    routes = [
        tornado.web.url(r"/item/([0-9]+)", ItemHandler),
    ]
    setup_swagger(routes, swagger_url="/api/doc",
                  description="使用 setup_swagger 设置 Swagger 文档")
    return tornado.web.Application(routes)


if __name__ == "__main__":
    app = make_app()
    app.listen(38881)
    tornado.ioloop.IOLoop.current().start()

浏览器打开

http://IP:38881/api/doc

相关推荐
Awesome Baron2 分钟前
《Learning Langchain》阅读笔记8-RAG(4)在vector store中存储embbdings
python·jupyter·chatgpt·langchain·llm
阡之尘埃5 分钟前
Python数据分析案例73——基于多种异常值监测算法探查内幕交易信息
人工智能·python·机器学习·数据分析·异常检测·无监督学习
蓝莓味柯基1 小时前
Python3:文件操作
python
xiaoh_71 小时前
解决视频处理中的 HEVC 解码错误:Could not find ref with POC xxx【已解决】
python·ffmpeg·音视频
明月与玄武2 小时前
Python编程的真谛:超越语法,理解编程本质
python·编程语言
CodeCraft Studio2 小时前
Excel处理控件Aspose.Cells教程:使用 Python 在 Excel 中进行数据验
开发语言·python·excel
拾忆-eleven3 小时前
C语言实战:用Pygame打造高难度水果消消乐游戏
c语言·python·pygame
旦莫3 小时前
Python 教程:我们可以给 Python 文件起中文名吗?
开发语言·python
豌豆花下猫3 小时前
Python 潮流周刊#99:如何在生产环境中运行 Python?(摘要)
后端·python·ai
小杨4043 小时前
python入门系列二十(peewee)
人工智能·python·pycharm