Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Airflow Jinja Rendered Template

Tags:

airflow

I've been able to successfully render Jinja Templates using the function within the BaseOperator, render_template.

My question is does anyone know the requirements to get rendered strings into the UI under the Rendered or Rendered Template tab?

Referring to this tab in the UI: Referring to this tab in the UI

Any help or guidance here would be appreciated.

like image 750
dillon Avatar asked May 05 '18 01:05

dillon


People also ask

How do you use Jinja templating in Airflow?

Templating in Airflow works the same as Jinja templating in Python. You enclose the code you want evaluated between double curly braces, and the expression is evaluated at runtime. For a complete list of the available variables, see the Airflow Templates reference.

How does Jinja template work?

It is a text-based template language and thus can be used to generate any markup as well as source code. The Jinja template engine allows customization of tags, filters, tests, and globals. Also, unlike the Django template engine, Jinja allows the template designer to call functions with arguments on objects.


1 Answers

If you are using templated fields in an Operator, the created strings out of the templated fields will be shown there. E.g. with a BashOperator:

example_task = BashOperator(
    task_id='task_example_task',
    bash_command='mycommand --date {{ task_instance.execution_date }}',
    dag=dag,
)

then the bash command would get parsed through the template engine (since a Jinja field is included) and later on you could see the result of this parsing in the web UI as you mentioned.

The fields must be templated, though. This can be seen in the code in the field templated_fields. For BashOperator (see code here https://github.com/apache/incubator-airflow/blob/master/airflow/operators/bash_operator.py) this is:

template_fields = ('bash_command', 'env')

Other fields in the BashOperator will not be parsed.

You can use macro commands (see here https://airflow.apache.org/code.html#macros) or information from xcom (see here https://airflow.apache.org/concepts.html?highlight=xcom#xcoms) in templated fields.

like image 80
tobi6 Avatar answered Oct 15 '22 23:10

tobi6