J. C. Rocamonde J. C. Rocamonde - 11 months ago 54
Python Question

Extending Flask class as main App

I'm learning Flask and am a bit confused about how to structure my code. So I tried to extend Flask main class as follows:

from flask import Flask, ...

class App(Flask):
def __init__(self, import_name, *args, **kwargs):
super(App, self).__init__(import_name, *args, **kwargs)

Note that I am aware of that this may be a completely wrong approach.

So that when I want to start the app I do:

app = App(__name__)

if __name__ == '__main__':

This way I can order my methods and routes in the class, but the problem is when using self-decorators:

def home(self, context=None):
context = context or dict()
return render_template('home.html', **context)

Which raises an error as
unresolved reference 'route'
. So I tried with
, but it doesn't still work. Since the method makes a mapping inside each App class instance, I can't try to replace the method with a
. So I tried:

def route(self, rule, **options):
def decorator(f):
endpoint = options.pop('endpoint', None)
self.add_url_rule(rule, endpoint, f, **options)
return f
return decorator

But this requires me to pass
in the decorator.

How can I fix it? Or how do I structure my code instead?

Answer Source

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.


from flask import Blueprint

bp = Blueprint('users', __name__, url_prefix='/users')


from flask import render_template
from my_app.users import bp

def index():
    return render_template('users/index.html')


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


from my_app import create_app

app = create_app()

Run the dev server with:

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

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('/', 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.