How to share the global app object in flask?

Question:

I am using flask and trying to the following.

I have defined a main.py file through which I want to run my app ie python main.py –

from flask import Flask
from view import tags

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    app.run()

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

from flask import Flask
app = Flask(__name__)

@app.route('/e')
def hello_world2():
    return 'Hello World!'

So I need to have the global app object in my main.py 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

Asked By: murtaza52

||

Answers:

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.

Answered By: Jeff Tratner

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 🙂

Answered By: Ignas ButÄ—nas

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
Answered By: codegeek

If you have a file AppName.py in which you define app, and then you have another file Foobar.py that needs it, you can always say in AppName.py:

import Foobar
Foobar.app = app

Then in Foobar.py you should be able to use app in your functions. One thing you want to be careful of is that you can’t have code in Foobar.py that runs immediately when the file is called the depends on the app variable which is passed in after the import.

Answered By: Kvass

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)
Answered By: Juan Odicio

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(...)
    ...
Answered By: Richard Elkins
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.