0

I am using Airflow and writing my DAG with Task Flow API. This is an example of one: https://airflow.apache.org/docs/apache-airflow/stable/tutorial_taskflow_api.html

In one of my task functions, I want to log the logical date of my dag. Thus, inside my function I have the line logging.info("DAG logical date is: " + '{{ ds }}'). My log then reads: "DAG logical date is '{{ ds }}'". I am aware variables such as 'ds' can only be read inside templates. Is there a way to read these in a Python function which defines my Task?

Diana Vazquez Romo
  • 152
  • 1
  • 1
  • 11

2 Answers2

3

You can to add ds as a parameter in your function See docs https://airflow.apache.org/docs/apache-airflow/stable/tutorial_taskflow_api.html#accessing-context-variables-in-decorated-tasks

0

As per Airflow 2.3.3 documentation, if you'd like to access one of the Airflow context variables (e.g. ds, logical_date, ti), you need to add **kwargs to your function signature and access it as follows:

def extract(**kwargs):
    my_date = kwargs['logical_date']
    logging.info(f"DAG logical date is: {my_date}") 
Diana Vazquez Romo
  • 152
  • 1
  • 1
  • 11