How to share the global app object in flask?

You can import current_app from flask. It stores a reference to the global application object.

from flask import current_app as app

def home():
    return render_template('base.html', name=app.name)

First, I would suggest to take a look at Blueprints http://flask.pocoo.org/docs/blueprints/ This will help to organize the app easily.

Also take a look at http://flask.pocoo.org/docs/api/#flask.current_app flask.current_app, the way how to get your app instance in other modules.

This link also could be helpful on how to organize and build flask app (it is not ideal for sure, but can give you some ideas) - Large-app-how-to.md

Have fun :)


One way is to create an overall package and adding a __init__.py file under that where you declare all global variables. In your case for example, you can create something like:

myapplication/
    *        __init__.py
    *        myviews/
        *         __init__.py
        *         view.py
        *         tags.py

etc

Now you add the following code in the __init__.py file:

app = Flask(__name__)

You can now use this app variable anywhere as long as you import the package myapplication.

import myapplication.myviews.view

Just import it from your other files. Perhaps the best way to do this is to put your app object in one single file and have everything else import from it.

For example, your main.py could still have:

from flask import Flask
from view import tags

app = Flask(__name__)

And then in other files, you could do:

from .main import app

or, if outside your package, just use the complete string

from mypackagename.main import app

One thing to be careful of is circular imports. The easiest way to handle this issue is to create your app first and then import whatever else you need to from your base file after you create it.

So for example:

from flask import Flask
app = Flask(__name__)

# do some stuff with app
from .views import view1, view2
from .server import run

So long as you put the imports after you've created app, like the above, you shouldn't have an issue with circular imports.


Regarding import and use of current_app from flask in a "helper" python function in a separate source file, this works as long as a current app context has already been set up (E.g. web request received). I have a case where, during application initialization (app.run not yet invoked), app.logger is invoked in the helper function.

Before I fixed it (see below), I got a stack trace punctuated by "RuntimeError: Working outside of application context".

Sample solution:
main.py:

import helper
...
app = Flask(__name__.split('.')[0],
        template_folder="templates",
        static_folder="static")
... 
# Fix: Create an app context
with app.app_context():
    helper.dbopen(...)
...
app.run(...)

helper.py:

from flask import current_app as app
...
def dbopen():
    app.logger.info(...)
    ...