App class

The App class is the core of a MicroPie application. It implements the ASGI call interface and dispatches HTTP, WebSocket and lifespan events to your handlers. Subclass App and add public methods to create route handlers. Instantiate your subclass and pass the resulting object to an ASGI server such as Uvicorn.

Constructor

class App(session_backend=None)

Create a new application. If session_backend is provided it must be an instance of SessionBackend. When omitted, MicroPie uses an in‑memory back‑end.

Attributes

middlewares

A list of HttpMiddleware instances. Middlewares run before and after every HTTP request. Append a middleware instance to enable it. See Writing middleware for examples.

ws_middlewares

A list of WebSocketMiddleware instances used for WebSocket connections.

session_backend

The active SessionBackend instance used to load and save session dictionaries for both HTTP and WebSocket flows.

startup_handlers

A list of asynchronous callables that run during the ASGI lifespan.startup event. Use this to set up resources such as database connections. See Quick start for an example.

shutdown_handlers

A list of asynchronous callables that run during the ASGI lifespan.shutdown event. Use this to clean up resources.

Methods

__call__(scope, receive, send)

The ASGI entry point. Dispatches to HTTP, WebSocket or lifespan handlers based on scope['type']. You normally do not call this directly; the ASGI server calls it for you.

request

The current Request (or WebSocketRequest) stored in a context variable for the active request lifecycle.

_redirect(location, extra_headers=None)

Return a tuple (302, '', headers) representing an HTTP redirect to location. Use this helper in your handlers to redirect the client.

_render_template(name, **kwargs)

Render a Jinja2 template asynchronously. Returns a string containing the rendered output. Requires Jinja2 to be installed. See Rendering templates for details.

Additionally, MicroPie defines several private helper methods such as _parse_cookies and _send_response. These are considered internal and not part of the public API. They may change without notice.