我正在尝试在包api中的蓝图authorisation.py
内访问访问应用程序配置。我正在初始化__init__.py
中使用的authorisation.py
中的蓝图。
__初始化__。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:在应用程序上下文之外工作
我理解为什么会这样,但是访问这些配置设置的正确方法是什么?
---- ----更新 暂时,我已经做到了。
@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)
答案 0 :(得分:84)
您可以使用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
代理仅在请求的上下文中可用。
答案 1 :(得分:18)
重载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()])
答案 2 :(得分:10)
要构建tbicr's答案,以下是覆盖register
method示例的示例:
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
decorator的示例:
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)
答案 3 :(得分:6)
蓝图有register
method,当您register blueprint时会调用。因此,您可以覆盖此方法或使用record
decorator来描述依赖于app
的逻辑。
答案 4 :(得分:4)
current_app
方法很好,但您必须有一些请求上下文。如果你没有(像测试一样的前期工作,例如),你最好放置
with app.test_request_context('/'):
current_app
来电之前。
您将拥有RuntimeError: working outside of application context
。
答案 5 :(得分:2)
您需要导入由app
返回的主Flask()
变量(或您称之为的任何变量):
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)
答案 6 :(得分:1)
您还可以将蓝图包装在函数中并将app
作为参数传递:
蓝图:
def get_blueprint(app):
bp = Blueprint()
return bp
主:
from . import my_blueprint
app.register_blueprint(my_blueprint.get_blueprint(app))