Extending Flask class as main App
<p>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:</p>

<pre><code>from flask import Flask, ...

class App(Flask):
    def __init__(self, import_name, *args, **kwargs):
        super(App, self).__init__(import_name, *args, **kwargs)
</code></pre>

<p>Note that I am aware of that this may be a completely wrong approach. 
<br>So that when I want to start the app I do:</p>

<pre><code>app = App(__name__)

if __name__ == '__main__':
    app.run()
</code></pre>

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

<pre><code>@route('/')
def home(self, context=None):
    context = context or dict()
    return render_template('home.html', **context)
</code></pre>

<p>Which raises an error as <code>unresolved reference 'route'</code>. I guess this is not the way I should be structuring the app. How should I do it instead or how do I get the error fixed?</p>

<p>Doing this doesn't make sense.  You would subclass <code>Flask</code> to change its internal behavior, not to define your routes as class methods.</p>

<p>Instead, you're looking for <a href="http://flask.pocoo.org/docs/0.11/blueprints/" rel="nofollow">blueprints</a> and the <a href="http://flask.pocoo.org/docs/0.11/patterns/appfactories/" rel="nofollow">app factory pattern</a>.  Blueprints divide your views into groups without requiring an app, and the factory creates and sets up the app only when called.</p>

<p><code>my_app/users/__init__.py</code></p>

<pre><code>from flask import Blueprint

bp = Blueprint('users', __name__, url_prefix='/users')
</code></pre>

<p><code>my_app/users/views.py</code></p>

<pre><code>from flask import render_template
from my_app.users import bp

@bp.route('/')
def index():
    return render_template('users/index.html')
</code></pre>

<p><code>my_app/__init__.py</code></p>

<pre><code>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
</code></pre>

<p><code>run.py</code></p>

<pre><code>from my_app import create_app

app = create_app()
</code></pre>

<p>Run the dev server with:</p>

<pre><code>FLASK_APP=run.py
FLASK_DEBUG=True
flask run
</code></pre>

<p>If you need access to the app in a view, use <code>current_app</code>, just like <code>request</code> gives access to the request in the view.</p>

<pre><code>from flask import current_app
from itsdangerous import URLSafeSerializer

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

<hr>

<p>If you <em>really</em> want to define routes as methods of a Flask subclass, you'll need to use <a href="http://flask.pocoo.org/docs/0.11/api/#flask.Flask.add_url_rule" rel="nofollow"><code>self.add_url_rule</code></a> in <code>__init__</code> rather than decorating each route locally.</p>

<pre><code>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')
</code></pre>

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

python flask