hayeon
hayeon

Reputation: 51

how to use airflow jinja template in python function?

Is there a way to apply a jinja template in a python function(print_hello) without using an airflow variable(located in airflow web UI) or a global variable?

def print_hello():
    print('{{ds}}')

t_test= PythonOperator(
    task_id='t_test',
    python_callable=print_hello,
    dag=dag
)

Upvotes: 1

Views: 3065

Answers (1)

Elad Kalif
Elad Kalif

Reputation: 15931

For Airflow < 2.0 you need to use provide_context=True

def print_hello(**kwargs):
    ds = (str(kwargs['ds'])
    print(ds)
    

t1 = MyPythonOperator(
    task_id='temp_task',
    python_callable=print_hello,
    provide_context=True,
    dag=dag)

You can also do:

def print_hello(ds, **kwargs):
    print(ds)

Upvotes: 1

Related Questions