Created
April 6, 2012 22:39
-
-
Save rahulkmr/2323631 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python | |
from flask import Flask, request | |
app = Flask(__name__) | |
idx = 0 | |
fnids = {} | |
def new_fnid(fn): | |
""" | |
Generate id for closures. | |
""" | |
global idx | |
idx += 1 | |
fnids[idx] = fn | |
return idx | |
def w_link(): | |
name = request.values['name'] | |
fnid = new_fnid(lambda: "Hello %s" % name) | |
return '<a href="?fnid=%s">Click here</a>' % (fnid) | |
def aform(): | |
fnid = new_fnid(w_link) | |
return ''' | |
<form method="post"> | |
<input type="hidden" name="fnid" value="%s" /> | |
<input name="name" /> | |
<input type="submit" /> | |
</form> | |
''' % (fnid) | |
@app.route('/', methods=['GET', 'POST']) | |
def enter(): | |
if request.values.get('fnid'): | |
return fnids[int(request.values['fnid'])]() | |
return aform() | |
if __name__ == '__main__': | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment