0

I'm trying to pass a dynamic date value into a macro function ds_format.

DEFAULT_DATE= '{{ ds }}'
__init__(self, exec_date=DEFAULT_DATE): self.exec_date = exec_date

Now, I've some macros within the functions like

{{macros.ds_format(ds, "%Y-%m-%d", "%Y%m%d")}} 

But, I want to pass the exec_date into the macro like

"""{{ macros.ds_format(""" + self.exec_date + """, "%Y-%m-%d", "%d%m%Y") }}""" 

Is this the right way to pass a value to to a micro in Airflow (jinja2) ? or. Is there a different way to pass the dynamic value (exec_date) ?.

Kris
  • 1,618
  • 1
  • 13
  • 13

2 Answers2

1

You need to just pass as follow:

"""{{ macros.ds_format(ds, "%Y-%m-%d", "%d%m%Y") }}""" 

An example is https://stackoverflow.com/a/52137676/5691525

Edited:

Why don't you use the following code if you just want to use exectiuon date:

EXEC_DATE = "{{ execution_date.strftime('"%d%m%Y"') }}"

If you want to just use variable why even use jinja at all? Just use normal python datetime that can be seen in this post: How do I turn a python datetime into a string, with readable format date?

kaxil
  • 17,706
  • 2
  • 59
  • 78
  • Thanks for the update. My question was how to pass the a dynamic variable self.exec_date into the ds_format function ? – Kris Sep 26 '18 at 17:46
  • Updated the comment – kaxil Sep 26 '18 at 21:05
  • I may redefine the original question. Here, my requirement is to pass the {{ execution_date }} or {{ ds }} to a different class (without using PythonOperator) as an argument within the constructor and then use it. So, In simple, I want to pass the Jinja template to a class or Need to find a way to retrieve the execution date without using Jinja templates now. Thanks. – Kris Oct 06 '18 at 08:47
1

Isn't it simpler to just use the {{ ds_nodash }} instead?

https://airflow.apache.org/code.html#macros

Damon Cool
  • 121
  • 1
  • 3