mmirage.core.writer — Writer

Template renderer

Template renderer for generating output from variable environments.

class mmirage.core.writer.renderer.TemplateRenderer(output_schema)[source]

Bases: object

Renderer for generating output from variable environments using Jinja2 templates.

Supports nested templates (dicts and lists), optimized handling of simple variable references, and proper handling of non-string values like PIL Images.

Parameters:

output_schema (Dict[str, Any])

output_schema

Dictionary defining the structure of output samples.

__init__(output_schema)[source]

Initialize the template renderer.

Parameters:

output_schema (Dict[str, Any]) – Dictionary defining the structure of output samples. Values can be strings (Jinja2 templates), lists, or dicts.

Return type:

None

batch_render(batch)[source]

Render a batch of variable environments.

Parameters:

batch (List[VariableEnvironment]) – List of variable environments to render.

Returns:

Dictionary mapping output keys to lists of rendered values.

Return type:

Dict[str, List[Any]]

is_single_variable_template(s)[source]

Check if a string is a simple single-variable template.

Parameters:

s (str) – String to check.

Returns:

The variable name if s is exactly ‘{{ var }}’, otherwise None.

Return type:

str | None