How to divide flask app into multiple py files?

后端 未结 5 750
一生所求
一生所求 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.

    0 讨论(0)
  • 2020-11-29 17:26

    You can use the usual Python package structure to divide your App into multiple modules, see the Flask docs.

    However,

    Flask uses a concept of blueprints for making application components and supporting common patterns within an application or across applications.

    You can create a sub-component of your app as a Blueprint in a separate file:

    simple_page = Blueprint('simple_page', __name__, template_folder='templates')
    @simple_page.route('/<page>')
    def show(page):
        # stuff
    

    And then use it in the main part:

    from yourapplication.simple_page import simple_page
    
    app = Flask(__name__)
    app.register_blueprint(simple_page)
    

    Blueprints can also bundle specific resources: templates or static files. Please refer to the Flask docs for all the details.

    0 讨论(0)
  • 2020-11-29 17:28

    Dividing the app into blueprints is a great idea. However, if this isn't enough, and if you want to then divide the Blueprint itself into multiple py files, this is also possible using the regular Python module import system, and then looping through all the routes that get imported from the other files.

    I created a Gist with the code for doing this:

    https://gist.github.com/Jaza/61f879f577bc9d06029e

    As far as I'm aware, this is the only feasible way to divide up a Blueprint at the moment. It's not possible to create "sub-blueprints" in Flask, although there's an issue open with a lot of discussion about this:

    https://github.com/mitsuhiko/flask/issues/593

    Also, even if it were possible (and it's probably do-able using some of the snippets from that issue thread), sub-blueprints may be too restrictive for your use case anyway - e.g. if you don't want all the routes in a sub-module to have the same URL sub-prefix.

    0 讨论(0)
  • 2020-11-29 17:33

    I would like to recommend flask-empty at GitHub.

    It provides an easy way to understand Blueprints, multiple views and extensions.

    0 讨论(0)
  • 2020-11-29 17:37

    This task can be accomplished without blueprints and tricky imports using Centralized URL Map

    app.py

    import views
    from flask import Flask
    
    app = Flask(__name__)
    
    app.add_url_rule('/', view_func=views.index)
    app.add_url_rule('/other', view_func=views.other)
    
    if __name__ == '__main__':
        app.run(debug=True, use_reloader=True)
    

    views.py

    from flask import render_template
    
    def index():
        return render_template('index.html')
    
    def other():
        return render_template('other.html')
    
    0 讨论(0)
提交回复
热议问题