Created
February 16, 2012 19:53
-
-
Save jbpotonnier/1847371 to your computer and use it in GitHub Desktop.
[Flask] Routing with class
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
bin | |
include | |
lib | |
local | |
*.swp | |
*.pyc | |
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 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) | |
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 | |
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) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment