Setting global attributes in Flask framework

Issue

I am working on a small web project using Flask/Python. This is a simple client side application without database.

I want to set the REST service address as a global attribute, but haven’t figured out how to do that.

I know that attributes can be seted in flask.config like this:

app = Flask(__name__)
app.config['attribute_name'] = the_service_address

but the Blueprint module cannot access the ‘app’ object.

Thanks a lot for your time.

Solution

Within a request context (i.e. in a view/handler) you can access the config on the current_app

from flask import current_app
current_app.config['attribute_name']

Answered By – Rob Cowie

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published