Update dag-run.rst to ref dag_run.conf#24452
Conversation
Docs made it sound like there was no way to access dag_run.conf outside of a templated field.
docs/apache-airflow/dag-run.rst
Outdated
| import pendulum | ||
|
|
||
| from airflow import DAG | ||
| from airflow.operators.python import PythonOperator | ||
|
|
||
| dag = DAG( | ||
| "example_parameterized_dag", | ||
| schedule_interval=None, | ||
| start_date=pendulum.datetime(2021, 1, 1, tz="UTC"), | ||
| catchup=False, | ||
| ) | ||
|
|
||
| def print_conf(**context): | ||
| conf1 = context['dag_run'].conf['conf1'] | ||
| print(conf1) | ||
|
|
||
| parameterized_task = PythonOperator( | ||
| task_id="parameterized_task", | ||
| python_callable=print_conf, | ||
| dag=dag, | ||
| ) |
There was a problem hiding this comment.
| import pendulum | |
| from airflow import DAG | |
| from airflow.operators.python import PythonOperator | |
| dag = DAG( | |
| "example_parameterized_dag", | |
| schedule_interval=None, | |
| start_date=pendulum.datetime(2021, 1, 1, tz="UTC"), | |
| catchup=False, | |
| ) | |
| def print_conf(**context): | |
| conf1 = context['dag_run'].conf['conf1'] | |
| print(conf1) | |
| parameterized_task = PythonOperator( | |
| task_id="parameterized_task", | |
| python_callable=print_conf, | |
| dag=dag, | |
| ) | |
| import pendulum | |
| from airflow import DAG | |
| dag = DAG("example_parameterized_dag", | |
| schedule_interval=None, | |
| start_date=pendulum.datetime(2021, 1, 1, tz="UTC"), | |
| catchup=False, | |
| ) | |
| @dag.task(task_id="parameterized_task") | |
| def print_conf(dag_run=None): | |
| conf1 = dag_run.conf["conf1"] | |
| print(conf1) | |
| print_conf() | |
We've moved away from using PythonOperator with preference for the TaskFlow API. I don't think the docs overall reflect this (...yet) but the example DAGs were all updated with this in mind. Also with the TaskFlow API you can access context variables directly without needing kwargs.
There was a problem hiding this comment.
I wrote it up slightly differently. Are you OK with my use of context = get_current_context()?
There was a problem hiding this comment.
get_current_context() was really purpose-built to access context variables deep in the stack without having to pass them around from the task callable to other callables.
But, maybe another example could be added to access dag_run.conf in a callable that isn't a task using get_current_context()? I don't think more examples could hurt 🙂
docs/apache-airflow/dag-run.rst
Outdated
| import pendulum | ||
|
|
||
| from airflow import DAG | ||
| from airflow.operators.python import PythonOperator | ||
|
|
||
| dag = DAG( | ||
| "example_parameterized_dag", | ||
| schedule_interval=None, | ||
| start_date=pendulum.datetime(2021, 1, 1, tz="UTC"), | ||
| catchup=False, | ||
| ) | ||
|
|
||
| def print_conf(**context): | ||
| conf1 = context['dag_run'].conf['conf1'] | ||
| print(conf1) | ||
|
|
||
| parameterized_task = PythonOperator( | ||
| task_id="parameterized_task", | ||
| python_callable=print_conf, | ||
| dag=dag, | ||
| ) |
There was a problem hiding this comment.
| import pendulum | |
| from airflow import DAG | |
| from airflow.operators.python import PythonOperator | |
| dag = DAG( | |
| "example_parameterized_dag", | |
| schedule_interval=None, | |
| start_date=pendulum.datetime(2021, 1, 1, tz="UTC"), | |
| catchup=False, | |
| ) | |
| def print_conf(**context): | |
| conf1 = context['dag_run'].conf['conf1'] | |
| print(conf1) | |
| parameterized_task = PythonOperator( | |
| task_id="parameterized_task", | |
| python_callable=print_conf, | |
| dag=dag, | |
| ) | |
| import pendulum | |
| from airflow import DAG | |
| dag = DAG("example_parameterized_dag", | |
| schedule_interval=None, | |
| start_date=pendulum.datetime(2021, 1, 1, tz="UTC"), | |
| catchup=False, | |
| ) | |
| @dag.task(task_id="parameterized_task") | |
| def print_conf(dag_run=None): | |
| conf1 = dag_run.conf["conf1"] | |
| print(conf1) | |
| print_conf() | |
We've moved away from using PythonOperator with preference for the TaskFlow API. I don't think the docs overall reflect this (...yet) but the example DAGs were all updated with this in mind. Also with the TaskFlow API you can access context variables directly without needing kwargs. I suppose this is could be argued as a style preference but helpful to have another example in the docs IMO.
|
Static checks failing. Please fix them @patricker and rebase. Installing pre-commits helps as a lot of problems are fixed for you automatically when you commit. |
| dag=dag, | ||
| ) | ||
|
|
||
| **Note**: The parameters from ``dag_run.conf`` can only be used in a template field of an operator. |
There was a problem hiding this comment.
Let’s make this a normal paragraph; I feel it reads better combined with the newly added content. Something like this:
Note that the parameters from […]. Example of a parametrized DAG […]:
|
I'll try and wrap this up in the next couple days, thanks for the feedback! |
|
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 5 days if no further activity occurs. Thank you for your contributions. |
Docs made it sound like there was no way to access dag_run.conf outside of a templated field.