# String Templating Tasks


# StringFormatter

class

prefect.tasks.templates.strings.StringFormatter

(template=None, **kwargs)[source]

This task contains a template which is formatted with the results of any upstream tasks and returned.

Variables from prefect.context are also available for formatting.

Args:

  • template (str, optional): the optional default template string to format at runtime; can also be provided as a keyword to run, which takes precedence over this default.
  • **kwargs (optional): additional keyword arguments to pass to the standard Task constructor
Example:

from prefect import Flow
from prefect.tasks.templates import StringFormatter


message = '''
Hi {name}!  Welcome to Prefect.  Today is {today}.
'''

msg_task = StringFormatter(name="message body", template=message)

with Flow("string-template") as flow:
    output = msg_task(name="Marvin")

flow_state = flow.run()
print(flow_state.result[output].result)
# Hi Marvin!  Welcome to Prefect.  Today is 2019-08-28.

methods:                                                                                                                                                       

prefect.tasks.templates.strings.StringFormatter.run

(template=None, **format_kwargs)[source]

Formats the template with the provided kwargs.

Args:

  • template (str, optional): the template string to format; if not provided, self.template will be used
  • **format_kwargs (optional): keyword arguments to use for formatting
Returns:
  • str: the formatted string



# JinjaTemplate

class

prefect.tasks.templates.jinja2.JinjaTemplate

(template=None, **kwargs)[source]

This task contains a Jinja template which is formatted with the results of any upstream tasks and returned.

Variables from prefect.context will also be used for rendering.

Args:

  • template (str, optional): the optional default template string to render at runtime; can also be provided as a keyword to run, which takes precedence over this default.
  • **kwargs (optional): additional keyword arguments to pass to the standard Task constructor
Example:

from prefect import Flow
from prefect.tasks.templates import JinjaTemplate


message = '''
Hi {{name}}!  Welcome to Prefect.  Today is {{today}}.
'''

msg_task = JinjaTemplate(name="message body", template=message)

with Flow("string-template") as flow:
        output = msg_task(name="Marvin")

flow_state = flow.run()

print(flow_state.result[output].result)
# Hi Marvin!  Welcome to Prefect.  Today is 2019-08-28.

methods:                                                                                                                                                       

prefect.tasks.templates.jinja2.JinjaTemplate.run

(template=None, **format_kwargs)[source]

Formats the Jinja Template with the provided kwargs.

Args:

  • template (str, optional): the template string to render; if not provided, self.template will be used
  • **format_kwargs (optional): keyword arguments to use for rendering; note that variables from prefect.context will also be used
Returns:
  • str: the rendered string



This documentation was auto-generated from commit bd9182e
on July 31, 2024 at 18:02 UTC