22

I'm trying to create a simple, but not trivial application with Flask and I keep running into a situation which is not explained well in the docs.

I'd like my app to be split between different modules, but it looks like it's hard to access the main app object (or db from flask-sqlalchemy) from any other place than the main module. It looks like you have to really write your own way of treating it as a global that everything has access to, which requires some questionable code and a bit of time.

Both app and db are created at the level where the main application is instantiated and every example assumes they're available at the top level. But if I split different url handlers into different modules, I lose the ability to use @app.route decorator. I can work around that with app.add_url_rule('/...', some.module...), but then again, app is not passed to that function.

Same thing happens with the db - I don't know if I should stick it into g, or make it accessible via app somehow. Doing that doesn't help me with moving the models into a separate module though - how am I supposed to access db when importing them? All examples again assume it's just available as a local variable.

Am I missing something obvious, or am I trying to use flask for the wrong task here?

viraptor
  • 33,322
  • 10
  • 107
  • 191
  • Decorators are just function wrappers. You can make a class that encapsulates all of the server logic and store all of the `db` and the `app` variables inside of the class. – Blender Jul 13 '12 at 00:12
  • @Blender Sure, there are many ways to solve this by hand. What I really want to find out is if there's any standard way to deal with it and if it seems to be completely missing from docs, are there any more surprises along the way. I was expecting some more obvious solution from flask itself presented somewhere. – viraptor Jul 13 '12 at 00:19
  • You can use Blueprint – user956424 Jun 24 '13 at 07:33
  • 1
    possible duplicate of [What's your folder layout for a Flask app divided in modules?](http://stackoverflow.com/questions/6089020/whats-your-folder-layout-for-a-flask-app-divided-in-modules) – user Sep 16 '13 at 09:08

2 Answers2

21

The answer to your question is blueprints. In fact, if you look at documentation, it is mentioned that blueprints are there to enable division of application in modules.

Sample code:

    from flask import Blueprint

    mod = Blueprint(blueprint_name, __name__, template_folder="folder_name"
                    static_folder="folder_name")

    @mod.route('/mymodule')
    def view():
         # your code

In app.py

    from yourmodule import mod 

    app.register_blueprint(mod)

This is the right way to divide app into modules. Check out blueprint docs for more details.

Moreover if you require to access app in any other module, you should use current_app proxy of the app.

    from flask import current_app

Check out this document which guides how to divide flask app into modules for large projects by the creator of flask.

codecool
  • 5,886
  • 4
  • 29
  • 40
  • Very nice link to documentation! ;) – Ignas Butėnas Aug 15 '12 at 19:41
  • 1
    @IgnasB. Thanks! Check out this repo on github with basic large flask app structure by me https://github.com/codecool/flask-app-structure – codecool Aug 16 '12 at 11:14
  • 6
    This doesn't really explain how to configure models in your Blueprints in a reusable manner, since they (if using Flask-SQLAlchemy) rely on the `db` object (which you have to import somehow). – vicvicvic Oct 24 '12 at 21:58
2

Have you seen the new blueprints feature (I haven't used it yet, but sounds like this may be helpful in your case).

Eran
  • 387,369
  • 54
  • 702
  • 768
malangi
  • 2,692
  • 4
  • 28
  • 42