How to access app.config in a project?

I am trying to access the configuration of an access application inside the authorisation.py drawing, which is in the api package. I initialize the project in __init__.py , which is used in authorisation.py .

__ __ INIT. RU

 from flask import Blueprint api_blueprint = Blueprint("xxx.api", __name__, None) from api import authorisation 

authorization.py

 from flask import request, jsonify, current_app from ..oauth_adapter import OauthAdapter from api import api_blueprint as api client_id = current_app.config.get('CLIENT_ID') client_secret = current_app.config.get('CLIENT_SECRET') scope = current_app.config.get('SCOPE') callback = current_app.config.get('CALLBACK') auth = OauthAdapter(client_id, client_secret, scope, callback) @api.route('/authorisation_url') def authorisation_url(): url = auth.get_authorisation_url() return str(url) 

I get RuntimeError: working out of application context

I understand why this is so, but what is the correct way to access these configuration settings?

---- ---- Update Temporary, I did it.

 @api.route('/authorisation_url') def authorisation_url(): client_id, client_secret, scope, callback = config_helper.get_config() auth = OauthAdapter(client_id, client_secret, scope, callback) url = auth.get_authorisation_url() return str(url) 
+42
flask
Aug 13 '13 at 16:39
source share
7 answers

You can use flask.current_app to replace the application in the project.

 from flask import current_app as app @api.route('/info/', methods = ['GET']) def get_account_num(): num = app.config["INFO"] 

Note. that the proxy server current_app is available only in the context of the request.

+28
Jul 08 '16 at 9:04 on
source share

Overloading the record method seems pretty simple:

 api_blueprint = Blueprint('xxx.api', __name__, None) api_blueprint.config = {} @api_blueprint.record def record_params(setup_state): app = setup_state.app api_blueprint.config = dict([(key,value) for (key,value) in app.config.iteritems()]) 
+15
Nov 01 '14 at 18:17
source share

To build the tbicr response, here is an example of overriding the register method example:

 from flask import Blueprint auth = None class RegisteringExampleBlueprint(Blueprint): def register(self, app, options, first_registration=False): global auth config = app.config client_id = config.get('CLIENT_ID') client_secret = config.get('CLIENT_SECRET') scope = config.get('SCOPE') callback = config.get('CALLBACK') auth = OauthAdapter(client_id, client_secret, scope, callback) super(RegisteringExampleBlueprint, self).register(app, options, first_registration) the_blueprint = RegisteringExampleBlueprint('example', __name__) 

And an example using record decorator :

 from flask import Blueprint from api import api_blueprint as api auth = None # Note there also a record_once decorator @api.record def record_auth(setup_state): global auth config = setup_state.app.config client_id = config.get('CLIENT_ID') client_secret = config.get('CLIENT_SECRET') scope = config.get('SCOPE') callback = config.get('CALLBACK') auth = OauthAdapter(client_id, client_secret, scope, callback) 
+8
Apr 12 '14 at 22:49
source share

In the drawings there is a register method , which is called during the registration form . That way, you can override this method or use record decorator to describe logic that depends on the app .

+4
Aug 14 '13 at 7:51
source share

You either need to import the main app variable (or that you called it), which is returned by Flask() :

 from someplace import app app.config.get('CLIENT_ID') 

Or do it from the request:

 @api.route('/authorisation_url') def authorisation_url(): client_id = current_app.config.get('CLIENT_ID') url = auth.get_authorisation_url() return str(url) 
+3
Aug 13 '13 at 18:48
source share

The current_app approach is good, but you should have some query context. If you do not have it (for example, for preliminary work, for example, for testing, it is better to place

with app.test_request_context('/'):

before this call to current_app .

Instead, you will have RuntimeError: working outside of application context .

+2
Feb 20 '14 at 19:02
source share

You can also wrap the project in a function and pass app as an argument:

Blueprint:

 def get_blueprint(app): bp = Blueprint() return bp 

Main:

 from . import my_blueprint app.register_blueprint(my_blueprint.get_blueprint(app)) 
+1
Jun 29 '16 at 12:03
source share



All Articles