[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

相关推荐
青瓷程序设计12 小时前
动物识别系统【最新版】Python+TensorFlow+Vue3+Django+人工智能+深度学习+卷积神经网络算法
人工智能·python·深度学习
tobebetter952712 小时前
How to manage python versions on windows
开发语言·windows·python
F_D_Z12 小时前
数据集相关类代码回顾理解 | sns.distplot\%matplotlib inline\sns.scatterplot
python·深度学习·matplotlib
daidaidaiyu13 小时前
一文入门 LangGraph 开发
python·ai
不知更鸟14 小时前
前端报错:快速解决Django接口404问题
前端·python·django
4***721314 小时前
【玩转全栈】----Django模板语法、请求与响应
数据库·python·django
梁正雄14 小时前
1、python基础语法
开发语言·python
ituff15 小时前
微软认证考试又免费了
后端·python·flask
梁正雄16 小时前
2、Python流程控制
开发语言·python
Eric.Lee202117 小时前
ubuntu 安装 Miniconda
linux·运维·python·ubuntu·miniconda