For now, you can do this with circular imports , for example:
app.py
import flask app = flask.Flask(__name__) import error_handlers
error_handlers.py
from app import app @app.errorhandler(404) def handle404(e): return '404 handled'
Apparently, this can be tricky in more complex scenarios.
Flask has a clean and flexible way to create applications from multiple modules, the blueprints concept. To register error handlers with flask.Blueprint , you can use any of these:
Example:
error_handlers.py
import flask blueprint = flask.Blueprint('error_handlers', __name__) @blueprint.app_errorhandler(404) def handle404(e): return '404 handled'
app.py
import flask import error_handlers app = flask.Flask(__name__) app.register_blueprint(error_handlers.blueprint)
Both methods achieve the same, depending on what suits you.
source share