Skip to content

xnano.web

xnano.web

xnano.web


Serve grids and components through an offscreen native cell runtime.

Classes:

  • Web

    Serve an application in a browser from an offscreen runtime.

Functions:

  • grid_factory

    Normalize a grid instance, class, or callable into a factory.

Web

Web(
    *,
    state: Any = None,
    title: str | None = None,
    width: int = 80,
    height: int = 24
)

Serve an application in a browser from an offscreen runtime.

Attributes:

  • state

    Application state shared with event and request hooks.

  • title

    Browser document title.

  • width

    Offscreen viewport width in cells.

  • height

    Offscreen viewport height in cells.

  • surface

    Presentation surface name.

Example

from xnano.components import Text web = Web(title="Status")

web.run(Text("Ready"), host="127.0.0.1", port=8000)

Methods:

  • run

    Serve an application until interrupted.

  • close

    Stop a managed server when one has been attached.

Source code in xnano/web.py
def __init__(
    self,
    *,
    state: Any = None,
    title: str | None = None,
    width: int = 80,
    height: int = 24,
) -> None:
    self.state = state
    """Application state passed to the offscreen runtime."""
    self.title = title or "xnano"
    """Browser document title."""
    self.width = width
    """Offscreen viewport width in cells."""
    self.height = height
    """Offscreen viewport height in cells."""
    self.surface = "web"
    """Presentation surface name."""
    self._server: Any | None = None

state instance-attribute

state = state

Application state passed to the offscreen runtime.

title instance-attribute

title = title or 'xnano'

Browser document title.

width instance-attribute

width = width

Offscreen viewport width in cells.

height instance-attribute

height = height

Offscreen viewport height in cells.

surface instance-attribute

surface = 'web'

Presentation surface name.

run

run(
    source: Any,
    *,
    host: str = "127.0.0.1",
    port: int = 8000
) -> None

Serve an application until interrupted.

Parameters:

  • source (Any) –

    Grid/component instance, class, or factory.

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

    Bind address.

  • port (int, default: 8000 ) –

    Bind port.

Source code in xnano/web.py
def run(
    self,
    source: Any,
    *,
    host: str = "127.0.0.1",
    port: int = 8000,
) -> None:
    """Serve an application until interrupted.

    Args:
        source: Grid/component instance, class, or factory.
        host: Bind address.
        port: Bind port.
    """
    from xnano.server.native import serve_native

    factory, _, _ = grid_factory(source)
    serve_native(
        factory,
        state=self.state,
        title=self.title,
        host=host,
        port=port,
        width=self.width,
        height=self.height,
    )

close

close() -> None

Stop a managed server when one has been attached.

Source code in xnano/web.py
def close(self) -> None:
    """Stop a managed server when one has been attached."""
    if self._server is not None:
        self._server.shutdown()
        self._server.server_close()
        self._server = None

grid_factory

grid_factory(
    source: Any,
) -> tuple[Callable[[], Any], bool, type | None]

Normalize a grid instance, class, or callable into a factory.

Parameters:

  • source (Any) –

    Root grid instance, grid class, or factory.

Returns:

Raises:

  • TypeError

    If source cannot produce a grid or component.

Source code in xnano/web.py
def grid_factory(source: Any) -> tuple[Callable[[], Any], bool, type | None]:
    """Normalize a grid instance, class, or callable into a factory.

    Args:
        source: Root grid instance, grid class, or factory.

    Returns:
        Factory, whether the source is shared, and its grid class.

    Raises:
        TypeError: If ``source`` cannot produce a grid or component.
    """
    from xnano.grids import BaseGrid

    if isinstance(source, type) and issubclass(source, BaseGrid):
        return source, False, source
    if isinstance(source, BaseGrid):
        return (lambda: source), True, type(source)
    if callable(source):
        return source, False, None
    if getattr(type(source), "_xnano_component_base", False):
        return (lambda: source), True, None
    raise TypeError(
        "Web.run() expects a grid/component instance, class, or factory"
    )