Reputation: 43
I am trying to pull values using xcom_pull in airflow dynamically
The below mentioned formatting doesn't work for me when I piece together jinja templating with f-strings in op_kwargs. Appreciate if anyone can help me here.
op_kwargs={'names':"{{ ti.xcom_pull(key = '" + f'name{i+1}' + ", task_ids='places' ) }}"}
Upvotes: 2
Views: 3341
Reputation: 15931
Using fstring require to set proper number of brackets for Jinja. You can do:
op_kwargs={'names': f"{{{{ ti.xcom_pull(key='name{i+1}', task_ids='places') }}}}"}
Example (This is just a minimal example for your parameters to clarify how this works):
from datetime import datetime
from airflow import DAG
from airflow.operators.python import PythonOperator
default_args = {
'owner': 'airflow',
'start_date': datetime(2017, 2, 1)
}
def func1(ti):
ti.xcom_push(key="name2", value="helloworld")
def func2(names):
print(names)
with DAG('fstring_dag', default_args=default_args, catchup=False, schedule=None):
a = PythonOperator(
task_id='places',
python_callable=func1,
)
i = 1
b = PythonOperator(
task_id='places2',
python_callable=func2,
op_kwargs={'names': f"{{{{ ti.xcom_pull(key='name{i+1}', task_ids='places') }}}}"}
)
a >> b
Render tab:
Log:
Upvotes: 3