Skip to content

Commit 5a85d05

Browse files
RNHTTRferruzzi
authored andcommitted
Doc: Add concrete examples for accessing context variables from TaskFlow tasks (apache#33296)
1 parent c4f9482 commit 5a85d05

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed

docs/apache-airflow/core-concepts/taskflow.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,16 @@ If you want to learn more about using TaskFlow, you should consult :doc:`the Tas
6666
Context
6767
-------
6868

69-
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 the :ref:`context variables<templates:variables>` you can use in your Jinja templates.
69+
You can access Airflow :ref:`context variables <templates:variables>` by adding them as keyword arguments as shown in the following example:
70+
71+
.. include:: ../../shared/template-examples/taskflow.rst
72+
73+
Alternatively, you may add ``**kwargs`` to the signature of your task and all Airflow context variables will be accessible in the ``kwargs`` dict:
74+
75+
.. include:: ../../shared/template-examples/taskflow-kwargs.rst
76+
77+
For a full list of context variables, see :ref:`context variables <templates:variables>`.
7078

71-
For this to work, you need to define ``**kwargs`` in your function header, or you can add directly the keyword arguments you would like to get such as ``ti=None`` to have the task instance passed.
7279

7380
Logging
7481
-------

docs/apache-airflow/templates-ref.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ Variable Type Description
8181
The DAG run's logical date, and values derived from it, such as ``ds`` and
8282
``ts``, **should not** be considered unique in a DAG. Use ``run_id`` instead.
8383

84+
Accessing Airflow context variables from TaskFlow tasks
85+
-------------------------------------------------------
86+
87+
While ``@task`` decorated tasks don't support rendering jinja templates passed as arguments,
88+
all of the variables listed above can be accessed directly from tasks. The following code block
89+
is an example of accessing a ``task_instance`` object from its task:
90+
91+
.. include:: ../shared/template-examples/taskflow.rst
92+
93+
Deprecated variables
94+
-------------------------------------------------------
95+
8496
The following variables are deprecated. They are kept for backward compatibility, but you should convert
8597
existing code to use other variables instead.
8698

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
.. Licensed to the Apache Software Foundation (ASF) under one
2+
or more contributor license agreements. See the NOTICE file
3+
distributed with this work for additional information
4+
regarding copyright ownership. The ASF licenses this file
5+
to you under the Apache License, Version 2.0 (the
6+
"License"); you may not use this file except in compliance
7+
with the License. You may obtain a copy of the License at
8+
9+
.. http://www.apache.org/licenses/LICENSE-2.0
10+
11+
.. Unless required by applicable law or agreed to in writing,
12+
software distributed under the License is distributed on an
13+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
KIND, either express or implied. See the License for the
15+
specific language governing permissions and limitations
16+
under the License.
17+
18+
.. code-block:: python
19+
20+
@task
21+
def print_ti_info(**kwargs):
22+
ti = kwargs["task_instance"]
23+
print(f"Run ID: {ti.run_id}") # Run ID: scheduled__2023-08-09T00:00:00+00:00
24+
print(f"Duration: {ti.duration}") # Duration: 0.972019
25+
26+
dr = kwargs["dag_run"]
27+
print(f"DAG Run queued at: {dr.queued_at}") # 2023-08-10 00:00:01+02:20
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
.. Licensed to the Apache Software Foundation (ASF) under one
2+
or more contributor license agreements. See the NOTICE file
3+
distributed with this work for additional information
4+
regarding copyright ownership. The ASF licenses this file
5+
to you under the Apache License, Version 2.0 (the
6+
"License"); you may not use this file except in compliance
7+
with the License. You may obtain a copy of the License at
8+
9+
.. http://www.apache.org/licenses/LICENSE-2.0
10+
11+
.. Unless required by applicable law or agreed to in writing,
12+
software distributed under the License is distributed on an
13+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
KIND, either express or implied. See the License for the
15+
specific language governing permissions and limitations
16+
under the License.
17+
18+
.. code-block:: python
19+
20+
@task
21+
def print_ti_info(task_instance=None, dag_run=None):
22+
print(f"Run ID: {task_instance.run_id}") # Run ID: scheduled__2023-08-09T00:00:00+00:00
23+
print(f"Duration: {task_instance.duration}") # Duration: 0.972019
24+
print(f"DAG Run queued at: {dag_run.queued_at}") # 2023-08-10 00:00:01+02:20

0 commit comments

Comments
 (0)