I am trying to access access application configuration inside a blueprint authorisation.py
which in a package api. I am initializing the blueprint in __init__.py
which is used in authorisation.py
.
__init__.py
from flask import Blueprint api_blueprint = Blueprint("xxx.api", __name__, None) from api import authorisation
authorisation.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 am getting RuntimeError: working outside of application context
I understand why that is but then what is the correct way of accessing those configuration settings?
----Update---- Temporarily, I have done this.
@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)
The environment is used to indicate to Flask, extensions, and other programs, like Sentry, what context Flask is running in. It is controlled with the FLASK_ENV environment variable and defaults to production . Setting FLASK_ENV to development will enable debug mode.
Flask uses a concept of blueprints for making application components and supporting common patterns within an application or across applications. Blueprints can greatly simplify how large applications work and provide a central means for Flask extensions to register operations on applications.
Use flask.current_app
in place of app
in the blueprint view.
from flask import current_app @api.route("/info") def get_account_num(): num = current_app.config["INFO"]
The current_app
proxy is only available in the context of a request.
Overloading record
method seems to be quite easy:
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()])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With