Skip to content

xnano.server.native

xnano.server.native

xnano.server.native


Render applications offscreen and expose their cell frames to browsers.

Classes:

  • NativeWebServer

    Serve one application from an owned offscreen runtime.

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()

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()