You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "set92 (via GitHub)" <gi...@apache.org> on 2025/02/04 12:20:48 UTC

Re: [I] Allow usage of Jinja templating inside task `param` [airflow]

set92 commented on issue #31801:
URL: https://github.com/apache/airflow/issues/31801#issuecomment-2633744266

   > since airlfow 2.10.0
   > 
   > you can do
   > 
   > from pathlib import Path
   > 
   > from airflow.models import DAG
   > from airflow.utils.dates import days_ago
   > from airflow.providers.common.sql.operators.sql import SQLExecuteQueryOperator
   > 
   > with DAG(dag_id="toto",
   >          start_date=days_ago(1),
   >          schedule="@daily"):
   > 
   > 
   >     def generate_sql_query(context, jinja_env) -> list[str]:
   >         with Path(__file__).with_name('toto.sql').open('r') as f:
   >             query = f.read().format(something="TOTO", logical_date="{{ ds }}")
   >             query = context["task"].render_template(query, context, jinja_env)
   >             return query.split(";")
   > 
   > 
   >     SQLExecuteQueryOperator(
   >         task_id="something",
   >         sql=generate_sql_query,
   >         conn_id='toto',
   >     )
   > 
   > sql file toto.sql in same path than the dag file
   > 
   > select
   >     *,
   >     date('{logical_date}')
   > from toto_{something}
   
   Great! Didn't know about it, but is weird we need to use {logical_date}, and not {{logical_date}}. Why is that? I thought Jinja worked all time with double curly brackets


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org