public
Last active

[Flask] Routing with class

  • Download Gist
.gitignore
1 2 3 4 5 6
bin
include
lib
local
*.swp
*.pyc
app.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13
from klask import Klask
 
app = Klask(__name__)
 
class Handler:
 
@app.route("/hello/<name>")
def hello(self, name):
return "Hello {name}!".format(name=name)
 
 
if __name__ == "__main__":
app.run(debug=True)
klask.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
from flask import Flask
from functools import partial, update_wrapper
 
def compose(g, f):
return lambda *args, **kwargs: g(f(*args, **kwargs))
 
 
class Klask(Flask):
 
def __init__(self, *args, **kwargs):
Flask.__init__(self, *args, **kwargs)
 
 
def route(self, rule, **options):
apply_self = lambda f: update_wrapper(partial(f, self=None), f)
decorator = Flask.route(self, rule, **options)
return compose(decorator, apply_self)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.