Skip to content

prefect_jinja.blocks

A module to interact with Jinja Environment.

JinjaEnvironmentBlock

Block to create a template environment.

Parameters:

Name Type Description Default
namespace dict

A dict of variables that are available in every template loaded by the environment.

required
search_path str

A path to the directory that contains the templates. Can be relative or absolute. Relative paths are relative to the running flow directory.

required
Example

Load a environment block:

from prefect_jinja import JinjaEnvironmentBlock
block = JinjaEnvironmentBlock.load("BLOCK_NAME")

Source code in prefect_jinja/blocks.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
class JinjaEnvironmentBlock(Block):
    """
    Block to create a template environment.

    Args:
        namespace (dict): A dict of variables that are available in every template loaded by the environment.
        search_path (str): A path to the directory that contains the templates. Can be relative or absolute.
            Relative paths are relative to the running `flow` directory.

    Example:
        Load a environment block:
        ```python
        from prefect_jinja import JinjaEnvironmentBlock
        block = JinjaEnvironmentBlock.load("BLOCK_NAME")
        ```
    """

    _block_type_name = "Jinja Environment"
    # _logo_url = ""

    namespace: Dict[str, Optional[str]] = Field(
        default_factory=dict,
        description="A dict of variables that are available in every template loaded by the environment.",
    )
    search_path: Optional[str] = Field(
        description="A path to the directory that contains the templates. Can be relative or absolute. Relative paths are relative to the running `flow` directory.",
    )

    def get_env(self) -> Environment:
        """
        Creates a Jinja Environment with a loader that searches for template files in the path provided by the
        `search_path` attribute and sets the global variables provided by the `namespace` attribute.

        Returns:
            A Jinja environment.

        Example:
            ```python
            @flow
            def example_get_jinja_environment_flow():
                env_block = JinjaEnvironmentBlock(
                    search_path="templates", namespace={"sender_mail": "sender@test.com"}
                )
                return env_block.get_env()
            jinja_env = example_get_jinja_environment_flow()
            ```
        """
        loader = FileSystemLoader(self.search_path)
        env = Environment(
            loader=loader, autoescape=select_autoescape(), enable_async=True
        )
        if self.namespace is not None:
            env.globals = self.namespace.copy()

        return env

get_env

Creates a Jinja Environment with a loader that searches for template files in the path provided by the search_path attribute and sets the global variables provided by the namespace attribute.

Returns:

Type Description
Environment

A Jinja environment.

Example
@flow
def example_get_jinja_environment_flow():
    env_block = JinjaEnvironmentBlock(
        search_path="templates", namespace={"sender_mail": "sender@test.com"}
    )
    return env_block.get_env()
jinja_env = example_get_jinja_environment_flow()
Source code in prefect_jinja/blocks.py
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
def get_env(self) -> Environment:
    """
    Creates a Jinja Environment with a loader that searches for template files in the path provided by the
    `search_path` attribute and sets the global variables provided by the `namespace` attribute.

    Returns:
        A Jinja environment.

    Example:
        ```python
        @flow
        def example_get_jinja_environment_flow():
            env_block = JinjaEnvironmentBlock(
                search_path="templates", namespace={"sender_mail": "sender@test.com"}
            )
            return env_block.get_env()
        jinja_env = example_get_jinja_environment_flow()
        ```
    """
    loader = FileSystemLoader(self.search_path)
    env = Environment(
        loader=loader, autoescape=select_autoescape(), enable_async=True
    )
    if self.namespace is not None:
        env.globals = self.namespace.copy()

    return env