Jinja templating in Airflow along with formatted text

Viewed 2281

I'm trying to run a SQL statement to be rendered by Airflow, but I'm also trying to include a variable inside the statement which is passed in from Python. The SQL statement is just a where clause, and after the WHERE, I'm trying to add a datetime, minus a few seconds:

f" ' {{ ts - macros.timedelta(seconds={lower_delay} + 1) }} ' "

so I want what's in the double curly braces to be computed and rendered in Airflow, but I want to pass in this variable called lower_delay before it does that. I've tried varying combinations with zero, one, or two additional curly braces around lower_delay as well as the entire string, but I seem to get a different error each time.

What is the proper way to pass in this lower_delay variable (it's just a number) so that it ends up rendering correctly?

1 Answers

Jinja templating requires two curly braces, when you use f-strings or str.format it will replace two braces with one while rendering:

Format strings contain “replacement fields” surrounded by curly braces {}. Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. If you need to include a brace character in the literal text, it can be escaped by doubling: {{ and }}.

So the following should work:

f" ' {{{{ ts - macros.timedelta(seconds={lower_delay} + 1) }}}} ' "
Related