Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions airflow-core/docs/tutorial/taskflow.rst
Original file line number Diff line number Diff line change
Expand Up @@ -346,11 +346,56 @@ incrementally to TaskFlow.

You can chain Taskflow and traditional tasks using ``>>`` or pass data using the ``.output`` attribute.

.. _taskflow/accessing_context_variables:

Templating in TaskFlow
''''''''''''''''''''''

Like traditional tasks, decorated TaskFlow functions support templated arguments — including loading content from files
or using runtime parameters.

When running your callable, Airflow will pass a set of keyword arguments that
can be used in your function. This set of kwargs correspond exactly to what you
can use in your Jinja templates. For this to work, you can add context keys you
would like to receive in the function as keyword arguments.

For example, the callable in the code block below will get values of the ``ti``
and ``next_ds`` context variables:

.. code-block:: python

@task
def my_python_callable(*, ti, next_ds):
pass


You can also choose to receive the entire context with ``**kwargs``. Note that
this can incur a slight performance penalty since Airflow will need to
expand the entire context that likely contains many things you don't actually
need. It is therefore more recommended for you to use explicit arguments, as
demonstrated in the previous paragraph.

.. code-block:: python

@task
def my_python_callable(**kwargs):
ti = kwargs["ti"]
next_ds = kwargs["next_ds"]

Also, sometimes you might want to access the context somewhere deep in the stack, but you do not want to pass
the context variables from the task callable. You can still access execution context via the ``get_current_context``
method.

.. code-block:: python

from airflow.providers.standard.operators.python import get_current_context


def some_function_in_your_library():
context = get_current_context()
ti = context["ti"]


Arguments passed to decorated functions are automatically templated. You can also template file using
``templates_exts``:

Expand All @@ -359,6 +404,7 @@ Arguments passed to decorated functions are automatically templated. You can als
@task(templates_exts=[".sql"])
def read_sql(sql): ...


Conditional Execution
'''''''''''''''''''''

Expand Down