Extending Flask class as main App

Doing this doesn’t make sense. You would subclass Flask to change its internal behavior, not to define your routes as class methods.

Instead, you’re looking for blueprints and the app factory pattern. Blueprints divide your views into groups without requiring an app, and the factory creates and sets up the app only when called.

my_app/users/__init__.py

from flask import Blueprint

bp = Blueprint('users', __name__, url_prefix="https://stackoverflow.com/users")

my_app/users/views.py

from flask import render_template
from my_app.users import bp

@bp.route("https://stackoverflow.com/")
def index():
    return render_template('users/index.html')

my_app/__init__.py

def create_app():
    app = Flask(__name__)
    # set up the app here
    # for example, register a blueprint
    from my_app.users import bp
    app.register_blueprint(bp)
    return app

run.py

from my_app import create_app

app = create_app()

Run the dev server with:

FLASK_APP=run.py
FLASK_DEBUG=True
flask run

If you need access to the app in a view, use current_app, just like request gives access to the request in the view.

from flask import current_app
from itsdangerous import URLSafeSerializer

@bp.route('/token')
def token():
    s = URLSafeSerializer(current_app.secret_key)
    return s.dumps('secret')

If you really want to define routes as methods of a Flask subclass, you’ll need to use self.add_url_rule in __init__ rather than decorating each route locally.

class MyFlask(Flask):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.add_url_rule("https://stackoverflow.com/", view_func=self.index)

    def index(self):
        return render_template('index.html')

The reason route (and self) won’t work is because it’s an instance method, but you don’t have an instance when you’re defining the class.

Leave a Comment