Skip to content

xnano.server

xnano.server

xnano.server


Serve browser applications and standalone grid request hooks.

Modules:

Classes:

Functions:

NativeWebServer

NativeWebServer(
    address: tuple[str, int],
    factory: Callable[[], Any],
    *,
    state: Any = None,
    title: str = "xnano",
    width: int = 80,
    height: int = 24
)

Bases: HTTPServer

Serve one application from an owned offscreen runtime.

Attributes:

  • factory

    Callable that creates the root grid or component.

  • root

    Root grid or component being served.

  • title

    Browser document title.

  • runtime (Runtime[Any] | None) –

    Offscreen runtime used to render frames and dispatch input.

Example

from xnano.components import Text server = NativeWebServer(("127.0.0.1", 0), lambda: Text("Ready")) server.server_address[1] > 0 True server.server_close()

Methods:

  • serve_forever

    Serve requests and release the runtime on its owner thread.

  • server_close

    Close both the HTTP listener and offscreen runtime.

Source code in xnano/server/native.py
def __init__(
    self,
    address: tuple[str, int],
    factory: Callable[[], Any],
    *,
    state: Any = None,
    title: str = "xnano",
    width: int = 80,
    height: int = 24,
) -> None:
    self.factory = factory
    self.root = factory()
    self.title = title
    self.grid = self.root
    self.runtime = None
    self._state = state
    self._width = width
    self._height = height
    super().__init__(address, _NativeWebHandler)

serve_forever

serve_forever(poll_interval: float = 0.5) -> None

Serve requests and release the runtime on its owner thread.

Source code in xnano/server/native.py
def serve_forever(self, poll_interval: float = 0.5) -> None:
    """Serve requests and release the runtime on its owner thread."""
    try:
        super().serve_forever(poll_interval)
    finally:
        if self.runtime is not None:
            self.runtime.close()
            self.runtime = None

server_close

server_close() -> None

Close both the HTTP listener and offscreen runtime.

Source code in xnano/server/native.py
def server_close(self) -> None:
    """Close both the HTTP listener and offscreen runtime."""
    if self.runtime is not None:
        self.runtime.close()
        self.runtime = None
    super().server_close()

RequestServer

RequestServer(
    address: tuple[str, int],
    grid: Any,
    *,
    runtime: Any | None = None
)

Bases: ThreadingHTTPServer

Serve request hooks declared by one grid.

Attributes:

  • grid

    Grid instance whose request hooks are served.

  • runtime

    Runtime exposed to request hook contexts, if supplied.

Example

server = RequestServer(("127.0.0.1", 0), object()) server.server_address[1] > 0 True server.server_close()

Source code in xnano/server/requests.py
def __init__(
    self,
    address: tuple[str, int],
    grid: Any,
    *,
    runtime: Any | None = None,
) -> None:
    self.grid = grid() if isinstance(grid, type) else grid
    self.runtime = runtime if runtime is not None else self.grid
    super().__init__(address, _RequestHandler)

serve_native

serve_native(
    factory: Callable[[], Any],
    *,
    state: Any = None,
    title: str = "xnano",
    host: str = "127.0.0.1",
    port: int = 8000,
    width: int = 80,
    height: int = 24
) -> None

Serve an application until interrupted.

Parameters:

  • factory (Callable[[], Any]) –

    Callable returning the root grid or component.

  • state (Any, default: None ) –

    Application state shared with hooks.

  • title (str, default: 'xnano' ) –

    Browser document title.

  • host (str, default: '127.0.0.1' ) –

    Bind address.

  • port (int, default: 8000 ) –

    Bind port.

  • width (int, default: 80 ) –

    Offscreen viewport width.

  • height (int, default: 24 ) –

    Offscreen viewport height.

Source code in xnano/server/native.py
def serve_native(
    factory: Callable[[], Any],
    *,
    state: Any = None,
    title: str = "xnano",
    host: str = "127.0.0.1",
    port: int = 8000,
    width: int = 80,
    height: int = 24,
) -> None:
    """Serve an application until interrupted.

    Args:
        factory: Callable returning the root grid or component.
        state: Application state shared with hooks.
        title: Browser document title.
        host: Bind address.
        port: Bind port.
        width: Offscreen viewport width.
        height: Offscreen viewport height.
    """
    server = NativeWebServer(
        (host, port),
        factory,
        state=state,
        title=title,
        width=width,
        height=height,
    )
    try:
        server.serve_forever()
    finally:
        server.server_close()

start_request_server

start_request_server(
    grid: Any,
    *,
    host: str = "127.0.0.1",
    port: int = 8000,
    runtime: Any | None = None
) -> RequestServer

Start request-hook serving on a daemon thread.

Parameters:

  • grid (Any) –

    Grid instance or class declaring request hooks.

  • host (str, default: '127.0.0.1' ) –

    Bind address.

  • port (int, default: 8000 ) –

    Bind port. Use 0 to select a free port.

  • runtime (Any | None, default: None ) –

    Runtime exposed to request-hook contexts.

Returns:

  • RequestServer

    Running server. Call shutdown and server_close to stop it.

Source code in xnano/server/requests.py
def start_request_server(
    grid: Any,
    *,
    host: str = "127.0.0.1",
    port: int = 8000,
    runtime: Any | None = None,
) -> RequestServer:
    """Start request-hook serving on a daemon thread.

    Args:
        grid: Grid instance or class declaring request hooks.
        host: Bind address.
        port: Bind port. Use ``0`` to select a free port.
        runtime: Runtime exposed to request-hook contexts.

    Returns:
        Running server. Call ``shutdown`` and ``server_close`` to stop it.
    """
    server = RequestServer((host, port), grid, runtime=runtime)
    threading.Thread(target=server.serve_forever, daemon=True).start()
    return server