Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
129 views
in Technique[技术] by (71.8m points)

python - Access a Flask extension that is defined in the app factory

I am using the app factory pattern to set up my Flask application. My app uses the Flask-Babel extension, and that is set up in the factory as well. However, I want to access the extension in a blueprint in order to use it,

The factory is in __init__.py.

def create_app(object_name):
    app = Flask(__name__)
    app.config.from_object(object_name)

    babel = Babel(app)

    app.register_blueprint(main_blueprint)
    app.register_blueprint(category_blueprint)
    app.register_blueprint(item_blueprint)

    db.init_app(app)
    return app

I want to add the following to main.py:

@babel.localeselector
def get_locale():
    if 'locale' in session:
        return session['locale']
    return request.accept_languages.best_match(LANGUAGES.keys())

@application.route('/locale/<locale>/', methods=['GET'])
def set_locale(locale):
    session['locale'] = locale
    redirect_to = request.args.get('redirect_to', '/')
    return redirect(redirect_to)     # Change this to previous url

Unfortunately, main.py doesn't have access to the babel variable from the application factory. How should I go about solving this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Flask extensions are designed to be instantiated without an app instance for exactly this case. Outside the factory, define your extensions. Inside the factory, call init_app to associate the app with the extension.

babel = Babel()

def create_app():
    ...
    babel.init_app(app)
    ...

Now the babel name is importable at any time, not just after the app has been created.


You already appear to be doing this correctly with the db (Flask-SQLAlchemy) extension.


In the case of your specific babel.localeselector example, it might make more sense to put that next to babel since it's being defined there.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...