Skip to content

prefect_jinja.tasks

Tasks for rendering Jinja Templates.

jinja_render_from_string async

Task that performs the rendering of a string.

Note

The context of a task will be available in the template via context keyword.

Parameters:

Name Type Description Default
template_string str

A string representing a template.

required
**kwargs dict

Keywords that will be available as variables in the template.

{}

Raises:

Type Description
TemplateSyntaxError

If there is a problem with the template.

Returns:

Type Description
str

A string containing the rendered template.

Examples:

Render a hello with username:

@flow
def send_hello_flow(username: str):
    return jinja_render_from_string("Hello, {{name}}!", username=username)
print(send_hello_flow(username="Robinho"))

Source code in prefect_jinja/tasks.py
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
@task
async def jinja_render_from_string(template_string: str, **kwargs) -> str:
    """
    Task that performs the rendering of a string.

    !!! note Context
        The context of a task will be available in the template via `context` keyword.

    Args:
        template_string: A string representing a template.
        **kwargs (dict): Keywords that will be available as variables in the template.

    Raises:
        TemplateSyntaxError: If there is a problem with the template.

    Returns:
        A string containing the rendered template.

    Examples:
        Render a hello with username:
        ```python
        @flow
        def send_hello_flow(username: str):
            return jinja_render_from_string("Hello, {{name}}!", username=username)
        print(send_hello_flow(username="Robinho"))
        ```
    """
    context = get_run_context()

    template = Template(template_string, enable_async=True)
    template.globals = _get_template_context(context)

    return await template.render_async(**kwargs)

jinja_render_from_template async

Task that performs the rendering of a template file based on settings of a Jinja Environment block.

Note

The context of a task will be available in the template via context keyword.

Parameters:

Name Type Description Default
name str

Name of template file to render.

required
jinja_environment JinjaEnvironmentBlock

A Jinja Environment block.

required
**kwargs dict

Keywords that will be available as variables in the template.

{}

Raises:

Type Description
TemplateNotFound

If the template file does not exist.

TemplateSyntaxError

If there is a problem with the template.

Returns:

Type Description
str

A string containing the rendered template.

Examples:

Render a welcome template file inside templates folder with company_name as block variable and username as keyword:

@flow
def send_welcome_flow(username: str):
    jinja_environment = JinjaEnvironmentBlock(
        search_path="templates",
        namespace={
            "company_name": "Acme",
        }
    )
    return jinja_render_from_template(
        "welcome.html",
        jinja_environment,
        username=username
    )
print(send_welcome_flow(username="Neymar"))

Source code in prefect_jinja/tasks.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
@task
async def jinja_render_from_template(name: str, jinja_environment: JinjaEnvironmentBlock, **kwargs) -> str:
    """
    Task that performs the rendering of a template file based on settings of a `Jinja Environment` block.

    !!! note Context
        The context of a task will be available in the template via `context` keyword.

    Args:
        name: Name of template file to render.
        jinja_environment: A Jinja Environment block.
        **kwargs (dict): Keywords that will be available as variables in the template.

    Raises:
        TemplateNotFound: If the template file does not exist.
        TemplateSyntaxError: If there is a problem with the template.

    Returns:
        A string containing the rendered template.

    Examples:
        Render a welcome template file inside `templates` folder with `company_name` as block variable and `username`
        as keyword:
        ```python
        @flow
        def send_welcome_flow(username: str):
            jinja_environment = JinjaEnvironmentBlock(
                search_path="templates",
                namespace={
                    "company_name": "Acme",
                }
            )
            return jinja_render_from_template(
                "welcome.html",
                jinja_environment,
                username=username
            )
        print(send_welcome_flow(username="Neymar"))
        ```
    """
    context = get_run_context()
    jinja_env = jinja_environment.get_env()

    template = jinja_env.get_template(name, globals=_get_template_context(context))

    return await template.render_async(**kwargs)