You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "BasPH (via GitHub)" <gi...@apache.org> on 2023/02/01 11:20:59 UTC

[GitHub] [airflow] BasPH commented on pull request #29143: Demonstrate usage of the PythonSensor

BasPH commented on PR #29143:
URL: https://github.com/apache/airflow/pull/29143#issuecomment-1411897691

   `--load-example-dags` is a flag in Breeze, therefore meaningless to the average Airflow user.
   
   Also, I think example DAGs is the wrong place to store code samples shown in the docs. With the default config, users see example DAGs. There's currently already a large number of example DAGs and your average Airflow user does not search example DAG code when in need of certain functionality. Instead, they google "how do I do XYZ in Airflow?" and end up in the docs. Seeing the first view of Airflow cluttered with example DAGs and then having to google how to get rid of those is a bad user experience.
   
   If we must keep code in separate files for testing/migration purposes, I suggest storing it together with the docs. So for this example, we'd have e.g. `airflow/docs/apache-airflow/howto/operator/pythonsensor.py`:
   
   ```python
   # Licensed to the Apache Software Foundation (ASF) under one
   # or more contributor license agreements.  See the NOTICE file
   # distributed with this work for additional information
   # regarding copyright ownership.  The ASF licenses this file
   # to you under the Apache License, Version 2.0 (the
   # "License"); you may not use this file except in compliance
   # with the License.  You may obtain a copy of the License at
   #
   #   http://www.apache.org/licenses/LICENSE-2.0
   #
   # Unless required by applicable law or agreed to in writing,
   # software distributed under the License is distributed on an
   # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   # KIND, either express or implied.  See the License for the
   # specific language governing permissions and limitations
   # under the License.
   
   # [START example]
   import datetime
   
   from airflow.decorators import dag, task
   from airflow.sensors.python import PythonSensor
   
   
   @dag(start_date=datetime.datetime(2023, 1, 1), schedule=None)
   def example():
   
       # TaskFlow sensor
       @task.sensor
       def wait_for_success_taskflow():
           return datetime.datetime.now().minute % 2 == 0
   
       wait_for_success_taskflow()
   
       # Equivalent functionality with PythonSensor class
       def wait_for_success_pythonsensor():
           return datetime.datetime.now().minute % 2 == 0
   
       PythonSensor(task_id="wait_for_success_pythonsensor", python_callable=wait_for_success_pythonsensor)
   
   
   example()
   # [END example]
   ```
   
   And the code in the .rst file will be:
   
   ```rst
   .. exampleinclude:: pythonsensor.py
       :start-after: [START example]
       :end-before: [END example]
   ```
   
   WDYT?


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

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