Skip to main content
Build Status

A simple web framework based on asyncio.

Tesla's induction motor

Induction is the phenomenon that drives asynchronous motors. Pictured above is Tesla’s induction motor.

Installation

pip install induction

Usage examples

If you know express and/or Flask, you’ll feel right at home.

Synchronous route

from induction import Induction
app = Induction(__name__)

@app.route('/')
def index(request):
    return app.render_template('index.html')

Async route

from asyncio import coroutine
from induction import Induction
app = Induction(__name__)

@app.route('/slow'):
@coroutine
def slow(request, response):
    yield from asyncio.sleep(10)
    response.write('Hello, world!')

Handlers

Handlers are decorated with @app.route(url_pattern). Routes are managed by the Routes library.

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 of 200 OK and mimetype of text/html are added.

    • A tuple of (response, headers, status), in any order and with at least one item provided. headers can be a list or a dictionnary.

  • writing: handlers can be defined to accept two arguments, request and response. They can then directly write data to the response.

Induction objects

The Induction constructor 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 on Induction instances:

  • 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_request functions are called in the order they’ve been declared.

    When a before_request function returns something else than None, 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 like before_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 type exc_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.

    Setting exc_type to None lets 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 from template_name_or_list and renders it using the given context.

Response objects

The following attributes and methods are available on Response objects:

  • 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.

    If chunk is a string, it’ll be encoded to bytes.

    If close is True, write_eof() is called on the response.

    If unchunked is True a Content-Length header is added and the response will be closed once the chunk is written.

  • redirect(location, status=302): redirects to location using the given status code.

Releases

  • 0.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.

Release files for induction 0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for induction 0.2
File Size Uploaded
induction-0.2.tar.gz 6.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for induction 0.2
File Interpreter ABI Platform
induction-0.2-py3-none-any.whl Python 3 none any Details

Total release size: 13.3 kB

Release files / induction-0.2.tar.gz

Download URL induction-0.2.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
046c1180c630fce917634cb7d92ee5a7ffb28bd452825f677e971a48c3d80a10
BLAKE2b-256 checksum
How to use checksums
325b9e1987fc39913e871619f1048af4f5b30464c815e5aa420b4aa0fc802879
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / induction-0.2-py3-none-any.whl

Download URL induction-0.2-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c2fff686cba8eb7cda79e63072e8c414263039b04e9f3188740cbba0e3b03ba9
BLAKE2b-256 checksum
How to use checksums
8a5524c47efdab067b613e6aa17a6e3a5c813896bc9c61b4e8ed80af0804f28e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.2 This release

2 release files

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page