[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

相关推荐
我是华为OD~HR~栗栗呀33 分钟前
华为od-21届考研-C++面经
java·c语言·c++·python·华为od·华为·面试
明月(Alioo)42 分钟前
机器学习入门,无监督学习之K-Means聚类算法完全指南:面向Java开发者的Python实现详解
python·算法·机器学习
鱼鱼说测试1 小时前
Linux下运行Jmeter
开发语言·python
CodeCraft Studio2 小时前
国产化Excel开发组件Spire.XLS教程:将Python列表转换为Excel表格(3种实用场景)
开发语言·python·excel·spire.xls·python列表转excel·国产化文档开发
企鹅侠客2 小时前
基于python写的PDF表格提取到excel文档
python·pdf·excel·pdf文档表格转excel
mortimer2 小时前
Python 中那些鲜为人知但实用的工具函数
python
weixin_421133412 小时前
Django 的文档接口
python·django·sqlite
LK_073 小时前
【Open3D】Ch.3:顶点法向量估计 | Python
开发语言·笔记·python
小码哥0683 小时前
智能化招聘系统设计与实现-Java
开发语言·python
饮浊酒3 小时前
Python学习-----小游戏之人生重开模拟器(普通版)
python·学习·游戏程序