Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 950
Tinker Avatar asked Jul 18 '16 18:07

Tinker


People also ask

Which extension is used to store a Flask app?

The name of the actual extension (the human readable name) however would be something like “Flask-SimpleXML”. Make sure to include the name “Flask” somewhere in that name and that you check the capitalization. This is how users can then register dependencies to your extension in their setup.py files.

How do you install a Flask extension?

Flask extensions are listed on the Flask Extension Registry and can be downloaded with easy_install or pip. If you add a Flask extension as dependency to your requirements. txt or setup.py file they are usually installed with a simple command or when your application installs.

Does Flask support extension?

There are a large number of Flask extensions available. A Flask extension is a Python module, which adds specific type of support to the Flask application. Flask Extension Registry is a directory of extensions available. The required extension can be downloaded by pip utility.

What is __ init __ In Flask?

Files named __init__.py are used to mark directories on disk as Python package directories. So it really depends on your structure of projects. Ideally if you have a project with multiple folders, you might have to make them as python package directories.


1 Answers

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.

like image 105
davidism Avatar answered Oct 10 '22 18:10

davidism