代码之家  ›  专栏  ›  技术社区  ›  ng.newbie

flask_restplus没有生成正确的Swagger UI

  •  0
  • ng.newbie  · 技术社区  · 6 年前

    enter image description here

    内容 test_ns.py 具体如下:

    from flask_restplus import Api
    from flask_restplus import Resource
    from flask_restplus import reqparse
    
    api = Api(version='1.0', title='My Blog API',
              description='A simple demonstration of a Flask RestPlus powered API')
    
    ns = api.namespace('blog/posts', description='Operations related to blog posts')
    
    parser = reqparse.RequestParser()
    parser.add_argument('Hello', required=True, location='form')
    
    
    @ns.route('/')
    class PostsCollection(Resource):
    
        @api.expect(parser)
        def get(self):
            """
            Returns list of blog posts.
            """
    
            return "Hello World from Swagger"
    

    内容 server.py 具体如下:

    from web.test_ns import ns, api
    from flask import Flask, Blueprint
    
    app = Flask(__name__)
    
    
    def configure_app(flask_app):
        flask_app.config['SWAGGER_UI_DOC_EXPANSION'] = 'List'
        flask_app.config['RESTPLUS_VALIDATE'] = True
        flask_app.config['RESTPLUS_MASK_SWAGGER'] = False
    
    
    def initialize_app(flask_app):
        configure_app(flask_app)
    
        blueprint = Blueprint('api', __name__, url_prefix='/api')
    
        api.init_app(blueprint)
        api.add_namespace(ns)
    
        flask_app.register_blueprint(blueprint)
    
    
    def main():
        initialize_app(app)
        app.run(host="127.0.0.1", port="5000")
    
    
    if __name__ == '__main__':
        main()
    

    问题是我没有得到合适的招摇过市的用户界面。当我跑的时候服务器.py在我的浏览器中访问它我得到了:

    enter image description here

    它基本上不是我想要的真正的招摇过市的用户界面。

    flask_restplus site :

    enter image description here

    我在GitHub上完成了一个项目 over here 但我不明白我在做什么不同或错误的事。该项目使用了与我相同的软件包,但我无法获得正确的输出。我哪里出错了?

    here .

    我的项目和GitHub项目的唯一区别是后者只是使用了更多的模块。

    查看Flask的服务器日志,可以看到GitHub项目正在对后端进行更多的调用来生成UI:

    enter image description here

    而对我来说,我只会打几个电话来生成错误的Swagger用户界面:

    * Serving Flask app "server" (lazy loading)
     * Environment: production
       WARNING: Do not use the development server in a production environment.
       Use a production WSGI server instead.
     * Debug mode: off
     * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
    127.0.0.1 - - [08/Feb/2019 23:37:00] "GET / HTTP/1.1" 404 -
    127.0.0.1 - - [08/Feb/2019 23:37:04] "GET /api/ HTTP/1.1" 200 -
    127.0.0.1 - - [08/Feb/2019 23:37:04] "GET /swaggerui/favicon-16x16.png HTTP/1.1" 200 -
    127.0.0.1 - - [08/Feb/2019 23:37:04] "GET /api/swagger.json HTTP/1.1" 200 -
    

    我找不到医生了。我错过了什么?

    即使用一个来自 flask_restplus网站 未能提供正确的招摇过市用户界面,如广告所示。

    GitHub项目有什么神奇之处为什么它是唯一有效的项目?它在做什么?我显然没有。

    1 回复  |  直到 6 年前
        1
  •  2
  •   Helen    6 年前

    这两个图像都是夸张的用户界面,只是不同的版本而已。

    • 在SwaggerUIV.3中的第一个图像(你得到什么),这是当前版本的UI。您可以在官方的Swagger UI演示中看到 https://pestore.swagger.io .

    flask_restplus通过使其依赖项(包括Swagger UI)保持最新而正常工作。问题出在flask_restplus的身上 文档 ,因为它包含了一个过时的swaggerui屏幕截图,与用户实际得到的不同。考虑向flask_restplus项目提交文档更新请求。