Skip to content

Instantly share code, notes, and snippets.



Created Oct 23, 2015
What would you like to do?
very simple flask-like web framework, taken from
from flask import Flask
app = Flask()
def index(request):
return 'Hello World'
if __name__ == '__main__':
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)
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