'Flask Babel RuntimeError: Working outside of request context

I tried to set up multiple Dash Apps inside a Flask App and use Flask Babel.

from flask import Flask, request
from flask_babel import Babel, gettext
from werkzeug.middleware.dispatcher import DispatcherMiddleware

def init_app():
    """Construct core Flask application."""
    app = Flask(__name__, instance_relative_config=False)
    app.config.from_object("config.Config")

    babel = Babel(app)

    @babel.localeselector
    def get_locale():
        return request.accept_languages.best_match(app.config["LANGUAGES"])

    with app.app_context():
        # Import parts of our core Flask app
        from . import routes
        from .plotly.sec import init_dashboard_sec
        from .plotly.bafin import init_dashboard_bafin

        # app = init_dashboard(app)
        app = DispatcherMiddleware(app, {
            "/s": init_dashboard_sec(app),
            "/b": init_dashboard_bafin(app)
        })

        return app

However, since I added the babel decorated function I get the following error:

RuntimeError: Working outside of request context.

This typically means that you attempted to use functionality that needed
an active HTTP request.  Consult the documentation on testing for
information about how to avoid this problem.

I tried to move the function to a different position but the error stayed the same.



Solution 1:[1]

I don't know if it is the correct solution but the following is working:

from flask import Flask, request
from flask_babel import Babel, gettext
from werkzeug.middleware.dispatcher import DispatcherMiddleware

def init_app():
    """Construct core Flask application."""
    app = Flask(__name__, instance_relative_config=False)
    app.config.from_object("config.Config")
    babel = Babel(app)

    with app.app_context():
        # Import parts of our core Flask app
        from . import routes
        from .plotly.sec import init_dashboard_sec
        from .plotly.bafin import init_dashboard_bafin

        # app = init_dashboard(app)
        app = DispatcherMiddleware(app, {
            "/s": init_dashboard_sec(app),
            "/b": init_dashboard_bafin(app)
        })

        @babel.localeselector
        def get_locale():
            return request.accept_languages.best_match(["de", "en"])

        return app

So babel gets initialized outside of the app context but the localselector is inside the context. For me it does not make much sense since the localeselector is still out of a request context but its working.

Solution 2:[2]

if you are probably working on the the terminal testing stuff out and you get the error just do this

from your_app import create_app
app = create_app()
with app.test_request_context(
        '/make_report/2017', data={'format': 'short'}):
    ##do something here

for more information you can vist this part of the docs to learn more https://flask.palletsprojects.com/en/2.1.x/reqcontext/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ukena
Solution 2 goodnews john