Programing

Blueprint에서 app.config에 액세스하는 방법은 무엇입니까?

lottogame 2020. 9. 5. 10:29
반응형

Blueprint에서 app.config에 액세스하는 방법은 무엇입니까?


authorisation.py패키지 API에있는 청사진 내에서 애플리케이션 구성에 액세스하려고합니다 . .NET에서 __init__.py사용되는 청사진을 초기화하고 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)

RuntimeError : working outside of application context

왜 그런지 이해하지만 해당 구성 설정에 액세스하는 올바른 방법은 무엇입니까?

---- 업데이트 ---- 일시적으로 이렇게했습니다.

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

To build on tbicr's answer, here's an example 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 the 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)

Blueprints have register method which called when you register blueprint. So you can override this method or use record decorator to describe logic which depends from app.


The current_app approach is fine but you must have some request context. If you don't have one (some pre-work like testing, e.g.) you'd better place

with app.test_request_context('/'):

before this current_app call.

You will have RuntimeError: working outside of application context , instead.


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

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

Or do that from within a request:

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

You could also wrap the blueprint in a function and pass the 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))

참고URL : https://stackoverflow.com/questions/18214612/how-to-access-app-config-in-a-blueprint

반응형