Last active
September 21, 2016 13:44
-
-
Save jpthompson23/11008578 to your computer and use it in GitHub Desktop.
Create a metaclass to automatically register new views with the app in Flask-Classy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from flask import Flask, render_template | |
| from flask.ext.classy import FlaskView, route | |
| # Instantiate the app: | |
| app = Flask(__name__) | |
| # create a metaclass to automatically register views with the app | |
| # and create the class AutoregView to use it | |
| # ------------------------------------------------------------------------------ | |
| def Autoregistrant(app=app): | |
| class _Autoregistrant(type): | |
| def __init__(cls, name, bases, clsdict): | |
| cls.register(app) | |
| return _Autoregistrant | |
| class AutoregView(FlaskView): | |
| __metaclass__ = Autoregistrant(app) | |
| # Then subclass AutoregView to create your views: | |
| class Index(AutoregView): | |
| route_base = '/' | |
| @route('') | |
| def index(self): | |
| return render_template('index.html') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment