Skip to content

Instantly share code, notes, and snippets.

@wong2 wong2/example.py
Created Oct 23, 2015

Embed
What would you like to do?
very simple flask-like web framework, taken from http://lucumr.pocoo.org/2010/6/14/opening-the-flask/
from flask import Flask
app = Flask()
@app.route('/')
def index(request):
return 'Hello World'
if __name__ == '__main__':
app.run()
from werkzeug import Request, Response, run_simple
from werkzeug.exceptions import HTTPException
from werkzeug.routing import Map, Rule
class Flask(object):
def __init__(self):
self.url_map = Map()
self.views = {}
def route(self, string, **options):
def decorator(f):
options['endpoint'] = f.__name__
self.views[f.__name__] = f
self.url_map.add(Rule(string, **options))
return f
return decorator
def run(self, **options):
return run_simple('localhost', 5000, self, **options)
def make_response(self, rv):
if isinstance(rv, basestring):
return Response(rv, mimetype='text/html')
return rv
def __call__(self, environ, start_response):
request = Request(environ)
adapter = self.url_map.bind_to_environ(environ)
try:
endpoint, values = adapter.match()
response = self.make_response(self.views[endpoint](request, **values))
except HTTPException, e:
response = e
return response(environ, start_response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.