LoslegenKostenlos starten

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.

Diese Übung ist Teil des Kurses

<Kurs>Einführung in Apache Airflow mit Python</Kurs>
Kurs ansehen

Übungsanweisungen

  • Set up the Dag to send you an email when it completes without error.

  • Set the subject attribute 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

Interaktive praktische Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# 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()
Code bearbeiten und ausführen