Jinja is well explained when using with operators which has support for a template field. It could be a bash_command parameters in a BashOperator as following
Airflow 使用之Operators. 搭建 airflow 的目的还是为了使用,使用离不开各种 Operators,本文主要介绍以下几点.
- For airflow context variables make sure that you either have access to Airflow through setting systemsitepackages to True or add apache-airflow to the requirements argument. Otherwise you won't have access to the most context variables of Airflow in opkwargs.
- The following are 30 code examples for showing how to use airflow.DAG.These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
- When you set the providecontext argument to True, Airflow passes in an additional set of keyword arguments: one for each of the Jinja template variables and a templatesdict argument. The templatesdict argument is templated, so each value in the dictionary is evaluated as a Jinja template.
Whistle dog tracking. Using jinja template in a op_args or op_kwargs in a python operator
def my_sleeping_function(**context):
# here context.random_base will be the actual execution_date, e.g. 2020-02-20
def my_sleeping_function(**context):
# here context.random_base will be the actual execution_date, e.g. 2020-02-20
![Airflow dag op_kwargs Airflow dag op_kwargs](/uploads/1/3/7/1/137182172/968788535.png)
Airflow Dag Op_kwargs
however, if you have a python code where you want to render your own variables, you can using following method from helpers module.
There is an helper method which is built on top of jinja in airflow, you can import it in your dag file
There is an helper method which is built on top of jinja in airflow, you can import it in your dag file
![Op_kwargs Airflow Op_kwargs Airflow](/uploads/1/3/7/1/137182172/659409254.png)
Op_kwargs Airflow Example
Suppose you have a template string in your dag definition, however, you only know the context when the dag task is running.
For example, the execution_date which is provided in context.ds
Then you can use parse_template_string method to get a template and use the render with context to get your filename as following
For example, the execution_date which is provided in context.ds
Then you can use parse_template_string method to get a template and use the render with context to get your filename as following