Skip to content

Instantly share code, notes, and snippets.

@jpthompson23
Last active September 21, 2016 13:44
Show Gist options
  • Save jpthompson23/11008578 to your computer and use it in GitHub Desktop.
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
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