Parent: Apache Airflow
You can acces airflow parameters either by Jinja Templating or kwargs.
Either way works but there are important considerations.
Remember that without render_template_as_native_obj=True jinja templating returns your params as strings, even if they’re set as integers.
This is the comparison
Kwargs:
@task()
def function(**context):
params = context['params']
return business_logic_function(params['district'])
function()At first glance this looks fine and even the Airflow documentation mentions this as the first method of passing DAG params to a function. But at a glance there are a few weaknesses here:
**contextpulls all the dag params in every function, and it happens every time the dag gets parsed by Airflow- Its not visible at a glance which param gets passed to this function.
- Less readable than the next alternative
Jinja Templating:
@task()
def function(district):
params = context['params']
return business_logic_function(district)
function('{{params.district}}')
Now this is significantly better because:
- Only the params used in this function is accessed
- Its explicitly visible which params are required, both in the function definition and implementation
- More readable
Specific to jinja
You need to pass
render_template_as_native_obj=Truein the @dag decorator, otherwise all params will be rendered as string
Related:
Writing clean code Refactoring