Flask: Using multiple packages in one app Flask: Using multiple packages in one app flask flask

Flask: Using multiple packages in one app


Your auth_app.run() method blocks your program from continuing to run. This is why the the posts_apps app doesn't get run. The entire process of serving up pages happens within Flask's run() method. Therefore, you can conclude that you can't run two Flask apps in the same process.

If you wish to split up your application into two like this, the recommended way is to use blueprints. Rather than creating two apps (auth and posts), you create two blueprints. You then create one application like so...

from flask import Flaskfrom auth import auth_blueprintfrom posts import post_blueprintapp = Flask(__name__)app.register_blueprint(auth_blueprint)app.register_blueprint(post_blueprint)app.run()


Though it seems as though Mark's approach using blueprints fits your project well, if you want to use separate applications for each package you should look into werkzeug.wsgi.DispatcherMiddleware.

A single process cannot run a second app after you run the first (like in your question), but that's not a problem with DispatcherMiddleware. You can use it to define a main application as well as others based on URL prefixes.

The example on the docs distinguishes between two applications --frontend and backend-- which are run depending on the URL the user requests.

If you want to learn more, read Matt Wright's "How I Structure My Flask Applications" and look at Overholt, his sample project. He decides to use two apps: one for the main website (the frontend) and another for the API, and he creates the distinction between the two based on the URL prefix. From his code*:

    from werkzeug.serving import run_simple    from werkzeug.wsgi import DispatcherMiddleware    from overholt import api, frontend    application = DispatcherMiddleware(frontend.create_app(), {        '/api': api.create_app()    })    if __name__ == "__main__":        run_simple('0.0.0.0', 5000, application, use_reloader=True, use_debugger=True)

This way, he creates two applications wherein each has its defined set of views, configurations, etc. and is able to run them from the same Python interpreter process.

*Note that run_simple() is only meant to be used for development --not production.