How to divide flask app into multiple py files?

后端 未结 5 773
一生所求
一生所求 2020-11-29 17:06

My flask application currently consists of a single test.py file with multiple routes and the main() route defined. Is there some way I could creat

5条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 17:19

    You can use simple trick which is import flask app variable from main inside another file, like:

    test-routes.py

    from __main__ import app
    
    @app.route('/test', methods=['GET'])
    def test():
        return 'it works!'
    

    and in your main files, where you declared flask app, import test-routes, like:

    app.py

    from flask import Flask, request, abort
    
    app = Flask(__name__)
    
    # import declared routes
    import test-routes
    

    It works from my side.

提交回复
热议问题