How to share the global app object in flask?


I am using flask and trying to the following.

I have defined a file through which I want to run my app ie python -

from flask import Flask
from view import tags

app = Flask(__name__)

def hello_world():
    return 'Hello World!'

if __name__ == '__main__':

I have defined a package named view in which I will be declaring my different view modules, each having its own routes. -

from flask import Flask
app = Flask(__name__)

def hello_world2():
    return 'Hello World!'

So I need to have the global app object in my for running the server, as well as in the view classes of my package for registering the routes. So how do I create the global app object and share it between all classes ?

Thanks, Murtaza

11/8/2012 8:02:10 AM

Accepted Answer

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

    *        myviews/


Now you add the following code in the file:

app = Flask(__name__)

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

import myapplication.myviews.view
11/9/2012 8:08:29 PM

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',

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow