airflow-guides icon indicating copy to clipboard operation
airflow-guides copied to clipboard

Update /guides/templating/

Open jwitz opened this issue 4 years ago • 0 comments

@paolaperaza commented on Mon Sep 16 2019

We have a current Airflow Guide for Templating + Macros, but a few customers have brought up similar use cases that we respond to with related material that is not in that doc.

That would include:

How and when to use user defined macros & user defined filters

Based on Slack conversation here and here.

Forum post here: https://forum.astronomer.io/t/how-can-i-pass-sql-as-a-file-w-airflows-postgres-operator/355

Content from @kaxil -

Example of using a user-defined macros and user-defined filters.

from datetime import timedelta

import airflow
from airflow.models import DAG
from airflow.operators.bash_operator import BashOperator
from airflow.operators.dummy_operator import DummyOperator

args = {
    'owner': 'Airflow',
    'start_date': airflow.utils.dates.days_ago(2),
}

def compute_next_execution_date(dag, execution_date):
    return dag.following_schedule(execution_date)

dag = DAG(
    dag_id='zz_bash_filter.macros',
    default_args=args,
    schedule_interval=None,
    dagrun_timeout=timedelta(minutes=60),
    user_defined_macros={
        'next_execution_date': compute_next_execution_date,
    },
    user_defined_filters={
        'hello': lambda name: 'Hello %s' % name
    },
)


echo = BashOperator(
        task_id='echo',
        bash_command='echo "{{ next_execution_date(dag, execution_date) }}"',
        dag=dag,
    )

echo1 = BashOperator(
        task_id='echo',
        bash_command='echo "{{ 'kaxil' | hello}}"',
        dag=dag,
    )

echo >> echo1

Btw Jinja already has a pprint filter An example usage would be {{ ti.xcom_pull("aa") | pprint }} https://jinja.palletsprojects.com/en/2.10.x/templates/#pprint List of available filters: https://jinja.palletsprojects.com/en/2.10.x/templates/#list-of-builtin-filters

jwitz avatar Dec 16 '20 16:12 jwitz