flowbber.plugins.sinks.template

Template

This sink will render specified Jinja2 template using the collected data as payload.

Important

This class inherits several inclusion and exclusion configuration options for filtering data before using it. See FilterSink Options for more information.

For the following collected data:

OrderedDict([
    ('timestamp', {
        'epoch': 1503280511,
        'epochf': 1503280511.560432,
        'iso8601': '2017-08-20T19:55:11',
        'strftime': '2017-08-20 19:55:11',
    }),
    ('user', {'uid': 1000, 'user': 'kuralabs'}),
])

The following template could be used:

<h1>My Rendered Template</h1>

<h2>Timestamp:</h2>

<ul>
    <li>Epoch: {{ data.timestamp.epoch }}</li>
    <li>ISO8601: {{ data.timestamp.iso8601 }}</li>
</ul>

<h2>User:</h2>

<ul>
    <li>UID: {{ data.user.uid }}</li>
    <li>User: {{ data.user.user }}</li>
</ul>

And rendering it with that data will result in:

<h1>My Rendered Template</h1>

<h2>Timestamp:</h2>

<ul>
    <li>Epoch: 1503280511</li>
    <li>ISO8601: 2017-08-20T19:55:11</li>
</ul>

<h2>User:</h2>

<ul>
    <li>UID: 1000</li>
    <li>User: kuralabs</li>
</ul>

Dependencies:

pip3 install flowbber[template]

Usage:

[[sinks]]
type = "template"
id = "..."

    [sinks.config]
    template = "template1.tpl"
    output = "render1.html"
    override = true
    create_parents = true
{
    "sinks": [
        {
            "type": "template",
            "id": "...",
            "config": {
                "template": "template1.tpl",
                "output": "render1.html",
                "override": true,
                "create_parents": true
            }
        }
    ]
}

template

URI to the Jinja2 template. If no schema is specified, file:// will be used.

Supported schemas:

  • file:// (the default): File system path to the template.

    file://path/to/template.tpl
    

    Or if using Substitutions:

    file://{pipeline.dir}/template.tpl
    

    When using this option, the selected template is able to load sibling templates (in the same directory) using Jinja2 import or extend directives:

    {% import "common.html" as common %}
    
    {% extends "base.html" %}
    
  • python://: A Python package and function name to load the content of the template.

    python://package.subpackage.function:template
    

    This is particularly useful if your templates are included as package_data in a package with your custom plugins, or you want to load the template using a custom logic from your flowconf.py.

    Specify the package and function using a dotted notation and the template name separated by a : (colon). The function receives the template name as argument and must return the content of the template.

    For example, in your flowconf.py:

    from jinja2 import TemplateNotFound
    
    def my_loading_function(template_name):
       if template_name == 'greeting_template':
           return '<h1>Hello { data.user.name }!</h1>'
       raise TemplateNotFound(template_name)
    

    This can be used in your pipeline as:

    When using this option, the selected template is able to load other templates that the function is able to resolve using Jinja2 import or extend directives:

    {% import "common" as common %}
    
    {% extends "base" %}
    
  • Default: N/A

  • Optional: False

  • Schema:

    {
        'type': 'string',
        'empty': False,
    }
    
  • Secret: False

output

Output file.

This option is nullable, if None is provided (the default), the output file name will be auto-determined using the name of the template and appending an out file extension.

For example:

template output
file://templates/the_template.tpl the_template.out
python://mypackage.load_template:my_template my_template.out
  • Default: None

  • Optional: True

  • Schema:

    {
        'type': 'string',
        'empty': False,
        'nullable': True
    }
    
  • Secret: False

override

Override output file if already exists.

  • Default: False

  • Optional: True

  • Schema:

    {
        'type': 'boolean',
    }
    
  • Secret: False

create_parents

Create output file parent directories if don’t exist.

  • Default: True

  • Optional: True

  • Schema:

    {
        'type': 'boolean',
    }
    
  • Secret: False

payload

Extra data to pass to the template. Data provided using this configuration option will be available to the template under the payload variable.

Usage:

[[sinks]]
type = "template"
id = "mytemplate"

    [sinks.config.payload]
    project_name = "Flowbber"
    project_url = "https://docs.kuralabs.io/flowbber/"

And then in your template:

<p>Visit our project page at
    <a href="{{ payload.project_url }}">{{ payload.project_name }}</a>
</p>
  • Default: None

  • Optional: True

  • Schema:

    {
        'type': 'dict',
        'nullable': True,
    }
    
  • Secret: False

filters

Custom filters to pass to the template.

This options must map the name of the filter with the path to the function that implements it. Any path to a Python function is valid, including using the local flowconf.py file.

Usage:

[[sinks]]
type = "template"
id = "mytemplate"

    [sinks.config.filters]
    coverage_class = "flowconf.filter_coverage_class"

And then in your flowconf.py (or package with custom components):

def filter_coverage_class(value, threshold=(0.5, 0.8)):
    lower, higher = threshold
    if value < lower:
        return 'low'
    if value < higher:
        return 'mid'
    return 'high'

The above filter can then be used as:

{% for filename, filedata in data.coverage.files.items() %}
<ul>
    <li class="{{ filedata.line_rate|coverage_class }}">
        {{ filename }}
    </li>
</ul>
{% endfor %}
  • Default: None

  • Optional: True

  • Schema:

    {
        'type': 'dict',
        'keysrules': {
            'type': 'string',
            'empty': False,
        },
        'valuesrules': {
            'type': 'string',
            'empty': False,
        },
        'nullable': True,
    }
    
  • Secret: False

Classes

  • TemplateSink: Common sink base class that adds several inclusion and exclusion
class flowbber.plugins.sinks.template.TemplateSink(index, type_, id_, optional=False, timeout=None, config=None)

Inheritance

Inheritance diagram of TemplateSink