You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2020/10/16 17:58:05 UTC

[GitHub] [airflow] turbaszek commented on a change in pull request #10587: @dag decorator

turbaszek commented on a change in pull request #10587:
URL: https://github.com/apache/airflow/pull/10587#discussion_r506637517



##########
File path: docs/concepts.rst
##########
@@ -162,6 +162,52 @@ Example DAG with decorated style
         html_content=email_info['body']
     )
 
+DAG decorator
+-------------
+
+.. versionadded:: 2.0.0
+
+In addition to creating DAGs using context managed, in Airflow 2.0 you can also create DAGs from a function.
+DAG decorator creates a DAG generator function. This function when called returns a DAG.
+
+DAG decorator also sets up the parameters you have in the function as DAG params. This allows you to parametrize
+your DAGs and set the parameters when triggering the DAG manually. See
+:ref:`Passing Parameters when triggering dags <dagrun:parameters>` to learn how to pass parameters when triggering DAGs.
+
+Example DAG with decorator:
+
+.. code-block:: python
+
+  from airflow.decorators import dag, task
+
+  @dag(default_args=default_args, schedule_interval=None)
+  def send_server_ip(email: 'example@example.com')
+
+    # Using default connection as it's set to httpbin.org by default
+    get_ip = SimpleHttpOperator(
+        task_id='get_ip', endpoint='get', method='GET', xcom_push=True
+    )
+
+    @task(multiple_outputs=True)
+    def prepare_email(raw_json: str) -> Dict[str, str]:
+      external_ip = json.loads(raw_json)['origin']
+      return {
+        'subject':f'Server connected from {external_ip}',
+        'body': f'Seems like today your server executing Airflow is connected from the external IP {external_ip}<br>'
+      }
+
+    email_info = prepare_email(get_ip.output)
+
+    send_email = EmailOperator(
+        task_id='send_email',
+        to=email,
+        subject=email_info['subject'],
+        html_content=email_info['body']
+    )
+
+  my_dag = send_server_ip()

Review comment:
       I would be in favor of putting this in example dag and using:
   ```
   .. exampleinclude:: /../airflow/providers/google/cloud/example_dags/example_bigquery_operations.py
       :language: python
       :dedent: 4
       :start-after: [START howto_operator_bigquery_create_dataset]
       :end-before: [END howto_operator_bigquery_create_dataset]
   ```
   
   In this way we will have a DAG that can be used for that can be used for tests




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org