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 | |
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 | |