简体   繁体   中英

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
)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM