Skip to content

htmy.renderer.typing

RendererType

Bases: Protocol

Protocol definition for renderers.

Source code in htmy/renderer/typing.py
class RendererType(Protocol):
    """Protocol definition for renderers."""

    async def render(self, component: Component, context: Context | None = None) -> str:
        """
        Renders the given component.

        Arguments:
            component: The component to render.
            context: An optional rendering context.

        Returns:
            The rendered string.
        """
        ...

render(component, context=None) async

Renders the given component.

Parameters:

Name Type Description Default
component Component

The component to render.

required
context Context | None

An optional rendering context.

None

Returns:

Type Description
str

The rendered string.

Source code in htmy/renderer/typing.py
async def render(self, component: Component, context: Context | None = None) -> str:
    """
    Renders the given component.

    Arguments:
        component: The component to render.
        context: An optional rendering context.

    Returns:
        The rendered string.
    """
    ...

StreamingRendererType

Bases: RendererType, Protocol

Protocol definition for streaming renderers.

Source code in htmy/renderer/typing.py
class StreamingRendererType(RendererType, Protocol):
    """Protocol definition for streaming renderers."""

    def stream(self, component: Component, context: Context | None = None) -> AsyncIterator[str]:
        """
        Async iterator that renders the given component.

        Arguments:
            component: The component to render.
            context: An optional rendering context.

        Yields:
            The rendered strings.
        """
        ...

stream(component, context=None)

Async iterator that renders the given component.

Parameters:

Name Type Description Default
component Component

The component to render.

required
context Context | None

An optional rendering context.

None

Yields:

Type Description
AsyncIterator[str]

The rendered strings.

Source code in htmy/renderer/typing.py
def stream(self, component: Component, context: Context | None = None) -> AsyncIterator[str]:
    """
    Async iterator that renders the given component.

    Arguments:
        component: The component to render.
        context: An optional rendering context.

    Yields:
        The rendered strings.
    """
    ...

is_renderer(obj)

Type guard that checks if the given object is a renderer.

Source code in htmy/renderer/typing.py
def is_renderer(obj: Any | None) -> TypeGuard[RendererType]:
    """Type guard that checks if the given object is a renderer."""
    # Just a basic check, don't waste time here.
    render: Any = getattr(obj, "render", None)
    return render is not None

is_streaming_renderer(obj)

Type guard that checks if the given object is a streaming renderer.

Source code in htmy/renderer/typing.py
def is_streaming_renderer(obj: Any | None) -> TypeGuard[StreamingRendererType]:
    """Type guard that checks if the given object is a streaming renderer."""
    if not is_renderer(obj):
        return False

    # Just a basic check, don't waste time here.
    stream: Any = getattr(obj, "stream", None)
    return stream is not None