Writing with Jinja
As you've started working with Airflow more, you've added various notification callbacks but realize you're receiving the same messages in your inbox each time. It's hard to find a message when the subject is always the same. After reading about Airflow's Jinja support, you realize you can now add some identifying information to make sure your Dags send a report for each day's run. You've decided to start with updating the sales_update Dag to send you an email with a templated subject when it finishes successfully.
All necessary Airflow imports are already available, and the tasks pull_sales_data and generate_sales_report are already defined.
Bu egzersiz, kursun bir parçasıdır
Python ile Apache Airflow'a Giriş
Egzersiz talimatları
Set up the Dag to send you an email when it completes without error.
Set the
subjectattribute for the notification email.Use a Jinja template to include the run date in the subject line:
Sales update successfully processed for YYYY-MM-DD
Uygulamalı etkileşimli egzersiz
Bu egzersizi bu örnek kodu tamamlayarak deneyin.
# Add a notification when successful, including date in the subject
@dag(start_date=datetime(2026,4,30),
____=SmtpNotifier(
from_email="[email protected]",
to="[email protected]",
____="Sales update successfully processed for {{ ____ }}"
))
def sales_update():
pull_sales_data() >> generate_sales_report()
sales_update()