2

I want to add a custom global macro to Airflow, I found this answer that outlines how this can be done. But I'm wondering if I can get the context implicitly (just like the built-in macros) e.g:

def ds_ny(**context):
  """
  Custom macro that returns ds in NY timezone
  """
  return context.get("execution_date").in_timezone("America/New_York").format("%Y-%m-%d")  

Usage:

select * from table where some_date = '{{ macros.ds_ny }}'

Currently I have to pass a parameter to the macro for it to render {{ macros.ds_ny(execution_date) }}

Yoni M
  • 103
  • 6
  • I have the same concern. Have you found any breakthroughs? – astrohsy Aug 27 '21 at 17:47
  • I've been trying to do the same thing. I don't think it's currently possible - if you look at the source code for some of the built-in macros, they all either must have the context passed in (e.g. `macros.ds_add`) or the context is resolved explicitly inside of the [`TaskInstance`](https://github.com/apache/airflow/blob/9a6baab5a271b28b6b3cbf96ffa151ac7dc79013/airflow/models/taskinstance.py#L2102) (e.g. `next_ds`). – 0x5453 May 09 '22 at 21:29

0 Answers0