如何访问蓝图中的app.config?

我试图访问一个蓝图authorisation.py里面的访问应用程序configuration,在一个包api中。 我正在初始化在authorisation.py使用的__init__.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) 

我得到RuntimeError:在应用程序上下文之外工作

我明白这是为什么,但那么访问这些configuration设置的正确方法是什么?

—-更新—-暂时,我已经完成了。

 @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) 

您可以使用flask.current_app代替蓝图中的应用程序。

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

注意: current_app代理只能在请求的上下文中使用。

重载record方法似乎很容易:

 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()]) 

build立在tbicr的答案,这里是一个例子覆盖register方法的例子:

 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__) 

还有一个使用record装饰器的例子:

 from flask import Blueprint from api import api_blueprint as api auth = None # Note there's 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) 

蓝图有register蓝图时调用的register方法 。 所以你可以重写这个方法或使用record装饰器来描述依赖于app逻辑。

您可能需要导入由Flask()返回的主appvariables(或者您称之为的任何variablesFlask()

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

或者从一个请求中做到这一点:

 @api.route('/authorisation_url') def authorisation_url(): client_id = current_app.config.get('CLIENT_ID') url = auth.get_authorisation_url() return str(url) 

current_app方法很好,但是你必须有一些请求上下文。 如果你没有(如testing前的一些工作),你最好放置

with app.test_request_context('/'):

在这个current_app调用之前。

您将有RuntimeError: working outside of application context而不是RuntimeError: working outside of application context

您也可以将蓝图封装在一个函数中,并将该app作为parameter passing:

蓝图:

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

主要:

 from . import my_blueprint app.register_blueprint(my_blueprint.get_blueprint(app))