API Reference

Routing Definition Syntax

Route definition strings may contain two kinds of parameter delimiters, path segments and catchalls.

Path segment parameters are delimited by a colon (:), and capture from their appearance in the route definition until the following forward slash (/) or the end of the URL. Only a single path segment parameter may exist in a given URL path segment.

Path segment delimiters may optionally be followed by a name, in which case the captured text from the URL will be associated with that name in the returned parameter dictionary when a lookup is performed.

Examples:

'/blog/post/:post_name' # Captures final segment as "post_name"

'/user_:id/profile'     # Captures URL between "user_" and "/" as "id"

'/article/:/:id'        # Captures final segment as "id", the first ":"
                        # matches as a wildcard but does not report the
                        # captured text

Catchalls are delimited by an asterisk (*) and work the same as path segment parameters, except they always match to the end of the URL including any forward slashes.

Examples:

'/article/:id/*slug' # Captures the second path segment as "id" and
                     # all following segments as "slug"

'/random*'           # Matches any URL following "random", but does
                     # not report the captured text

Methods

class nanoroute.router

Core class which represents a collection of HTTP verbs, routes, and associated Python object handles.

delete(route, /)

Convenience routing decorator, equivalent to route('DELETE', ...)

Parameters:

route (str) – The route definition

Returns:

A registration decorator method

Return type:

Callable[[handle], handle]

get(route, /)

Convenience routing decorator, equivalent to route('GET', ...)

Parameters:

route (str) – The route definition

Returns:

A registration decorator method

Return type:

Callable[[handle], handle]

lookup(method, path, /)

Base lookup function, finds a handle object given a method and a request path

Parameters:
  • method (str) – The HTTP verb from the associated request

  • path (str) – The URL path from the associated request

Raises:

LookupError – If no matching handler is found

Returns:

The associated handle object and a {key: value} dictionary of captured parameters

Return type:

tuple[Any, dict[str, str]]

Usage:

handler, params = app.lookup('GET', '/about')
post(route, /)

Convenience routing decorator, equivalent to route('POST', ...)

Parameters:

route (str) – The route definition

Returns:

A registration decorator method

Return type:

Callable[[handle], handle]

put(route, /)

Convenience routing decorator, equivalent to route('PUT', ...)

Parameters:

route (str) – The route definition

Returns:

A registration decorator method

Return type:

Callable[[handle], handle]

route(methods, route, /)

Base routing decorator

Parameters:
  • methods (str | Iterable[str]) – Either a string or iterable of strings representing the HTTP verbs to associate the route with

  • route (str) – The route definition

Returns:

A registration decorator method

Return type:

Callable[[handle], handle]

Usage:

import nanoroute
app = nanoroute.router()

@app.route('GET', '/about')
def handle_about():
  ...
wsgi_app(environ, start_response, /)

Convenience lookup function for compatibility with PEP 3333 WSGI servers, stores the captured parameter dict in the nanoroute.params environ key.

Parameters:
  • environ (dict[str, str]) – WSGI environ dict

  • start_response (Callable[[str, dict[str, str]], Callable[[str], None]]) – WSGI start_response() method

Returns:

WSGI-compatible iterable

Return type:

bytes | Iterable[bytes]

Equivalent to the following:

def wsgi_app(environ, start_response):
  handler, params = app.lookup(
      environ['REQUEST_METHOD'],
      environ['PATH_INFO']
  )
  environ['nanoroute.params'] = params
  return handler(environ, start_response)

app.wsgi_app = wsgi_app