close

How to pass parameter to PythonOperator in Airflow

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to pass parameter to PythonOperator in Airflow in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to pass parameter to PythonOperator in Airflow?

  1. How to pass parameter to PythonOperator in Airflow?

    PythonOperator have a named parameter op_kwargs and accepts dict object.

  2. pass parameter to PythonOperator in Airflow

    PythonOperator have a named parameter op_kwargs and accepts dict object.

Method 1

  1. Pass a dict object to op_kwargs
  2. Use the keys to access their value from kwargs dict in your python callabledef SendEmail(**kwargs): print(kwargs['key1']) print(kwargs['key2']) msg = MIMEText("The pipeline for client1 is completed, please check.") msg['Subject'] = "xxxx" msg['From'] = "xxxx" ...... s = smtplib.SMTP('localhost') s.send_message(msg) s.quit() t5_send_notification = PythonOperator( task_id='t5_send_notification', provide_context=True, python_callable=SendEmail, op_kwargs={'key1': 'value1', 'key2': 'value2'}, dag=dag, )

Method 2

PythonOperator have a named parameter op_kwargs and accepts dict object.

have

t5_send_notification = PythonOperator(
    task_id='t5_send_notification',
    provide_context=True,
    python_callable=SendEmail,
    op_kwargs={"my_param":'value1'},
    dag=dag,
)

def SendEmail(my_param,**kwargs):
    print(my_param) #'value_1'
    msg = MIMEText("The pipeline for client1 is completed, please check.")
    msg['Subject'] = "xxxx"
    msg['From'] = "xxxx"
    ......
    s = smtplib.SMTP('localhost')
    s.send_me

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read