Package: induction
A simple web framework based on asyncio.Induction is the phenomenon that drives asynchronous motors. Pictured above isTesla’s induction motor.Installationpip install inductionUsage examplesIf you knowexpressand/orFlask, you’ll feel right at home.Synchronous routefrominductionimportInductionapp=Induction(__name__)@app.route('/')defindex(request):returnapp.render_template('index.html')Async routefromasyncioimportcoroutinefrominductionimportInductionapp=Induction(__name__)@app.route('/slow'):@coroutinedefslow(request,response):yield fromasyncio.sleep(10)response.write('Hello, world!')HandlersHandlers are decorated with@app.route(url_pattern). Routes are managed by
theRouteslibrary.Handlers have several way to send data back to the client:returning: synchronous routes can return data directly. The return values
are passed to the response object. Supported return values are:A string or bytes object, which becomes the body of the response. A
default status of200 OKand mimetype oftext/htmlare added.A tuple of(response, headers, status), in any order and with at least
one item provided.headerscan be a list or a dictionnary.writing: handlers can be defined to accept two arguments,requestandresponse. They can then directly write data to the response.InductionobjectsTheInductionconstructor accepts the following arguments:name: the name for your app.And the following keyword arguments:template_folder: path to the folder from which to load templates.
Defaults to'templates'relatively to the current working directory.The following methods are available onInductioninstances:route(path, **conditions): registers a route. Meant to be used as a
decorator:@app.route('/')
def foo(request):
    return jsonify({})before_request(func): registers a function to be called before all
request handlers. E.g.:@app.before_request
def set_some_header(request, response):
    request.uuid = str(uuid.uuid4())
    response.add_header('X-Request-ID', request.uuid)before_requestfunctions are called in the order they’ve been declared.When abefore_requestfunction returns something else thanNone, all
request processing is stopped and the returned data is passed to the
response.after_request(func)registers a function to be called after all request
handlers. Works likebefore_request.handle_404(request, [response]): error handler for HTTP 404 errors.error_handler(exc_type): registers a function to be called when a
request handler raises an exception of typeexc_type. Exception handlers
take the request, the response and the exception object as argument:@app.error_handler(ValueError):
def handle_value_error(request, response, exception):
    response.add_header("X-Exception", str(exception))Note that the response may have been partially sent to the client already.
Depending on what your application does, it might not be safe to set headers
or even send data to the response.Settingexc_typetoNonelets you register a catch-all error handler
that will process all unhandled exceptions:@app.error_handler(None):
def handle_exception(request, response, exception):
    # Send exception to Sentry
    client = raven.Client()
    client.captureException()render_template(template_name_or_list, **context): loads the first
matching template fromtemplate_name_or_listand renders it using the
given context.Response objectsThe following attributes and methods are available onResponseobjects:status,status_line: the HTTP status code and line for this
response.write(chunk, close=False, unchunked=False): writes a chunk of data to
the reponse.Ifchunkis a string, it’ll be encoded to bytes.IfcloseisTrue,write_eof()is called on the response.IfunchunkedisTrueaContent-Lengthheader is added and the
response will be closed once the chunk is written.redirect(location, status=302): redirects tolocationusing the
given status code.Releases0.2(2014-09-25)404 error returns HTML by default.Ability to set a catch-all error handler, e.g. for Sentry handling.0.1(2014-09-19)Initial release.