Sitemap
Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.

Python Airflow Project For Your GitHub!

Combine Apache Airflow with Python to automate and manage your workflows efficiently

9 min readAug 8, 2024

--

Press enter or click to view image in full size
Apache Airflow Image logo from Apache

In the rapidly evolving landscape of software development, workflow automation has become a cornerstone of efficient and scalable operations. As projects grow in complexity, managing and automating various tasks can become challenging.

This is where tools like Apache Airflow come into play, streamlining the orchestration of workflows and ensuring that all processes run smoothly. Have in mind that I have made another article explaining the key terms and fundamentals of Airflow and how it is used in Data Engineering, if you are interested can find more in the link below:

Python, a high level and powerful programming language, has gained immense popularity due to its simplicity and wide range of applications. When combined with Apache Airflow, Python offers a robust solution for automating workflows, handling complex data pipelines, and ensuring seamless execution of tasks.

Hi, my name is CyCoderX and in this article, we will delve into the basics of Python and Apache Airflow, exploring how these tools can be harnessed to create efficient and automated workflows. Whether you are a novice or a tech enthusiast, this guide aims to provide a comprehensive introduction to these powerful technologies and their applications in the real world.

Let’s dive in!

Key Concepts of Apache Airflow

Apache Airflow is an open-source platform designed to programmatically author, schedule, and monitor workflows. Initially developed by Airbnb to address the complexity of managing data workflows, Airflow has since become a widely adopted tool in the data engineering and data science communities.

What is Apache Airflow?

Apache Airflow is a workflow orchestration tool that allows users to define and manage workflows as code. It uses Directed Acyclic Graphs (DAGs) to represent workflows, where each node represents a task and the edges define dependencies between tasks. This approach enables the clear definition, scheduling, and monitoring of workflows in a scalable and maintainable manner.

Key Components and Architecture

  1. DAGs (Directed Acyclic Graphs): The core concept in Airflow, representing a collection of tasks with defined dependencies. DAGs are defined using Python code.
  2. Tasks: The individual units of work within a DAG. Tasks can be anything from data extraction and transformation to loading data into a database.
  3. Scheduler: A component that handles the execution of tasks in a DAG based on their schedule and dependencies.
  4. Executor: Determines how tasks are executed, whether locally, on a cluster, or using external systems like Kubernetes.
  5. Web Server: Provides a user interface for monitoring and managing workflows, allowing users to view the status of DAGs and tasks, check logs, and trigger or pause workflows.
  6. Metadata Database: Stores information about DAGs, task instances, users, and other metadata. Typically, a relational database like PostgreSQL or MySQL is used.

Advantages of Using Airflow

  • Scalability: Airflow can handle complex workflows with numerous tasks and dependencies, scaling horizontally to meet growing demands.
  • Flexibility: Workflows are defined in Python, providing the flexibility to incorporate custom logic and leverage Python’s extensive libraries.
  • Extensibility: Airflow supports plugins and custom operators, enabling users to extend its functionality to meet specific needs.
  • Monitoring and Logging: Airflow provides detailed logs and a rich user interface for monitoring workflows, making it easier to identify and troubleshoot issues.
  • Community and Ecosystem: Being an open-source project, Airflow has a large community and ecosystem, offering numerous plugins, integrations, and support.

Apache Airflow simplifies the creation, scheduling, and management of workflows, making it an invaluable tool for data engineers and anyone involved in complex data processes.

Interested in more Python content? Click here to check out my list on Medium.

Installing and Setting Up Airflow with Python

Getting started with Apache Airflow involves setting up your environment, installing the necessary packages, and configuring Airflow to run your workflows. Here’s a step-by-step guide to help you through the process.

Prerequisites

Before installing Airflow, ensure you have the following prerequisites:

  • Python 3.6+: Airflow requires Python 3.6 or later. Check your Python version by running python --version in your terminal.
  • Virtual Environment: It’s recommended to create a virtual environment to manage dependencies and avoid conflicts. You can use venv or virtualenv.

Step-by-Step Installation Guide

Create a Virtual Environment:

python -m venv airflow-env
source airflow-env/bin/activate # On Windows, use `airflow-env\Scripts\activate`

Install Apache Airflow: Airflow uses several extra packages, and it’s essential to specify the correct version. The following command installs Airflow with common dependencies:

export AIRFLOW_HOME=~/airflow  # Optional: Define the Airflow home directory
pip install apache-airflow

Initialize the Metadata Database: Airflow uses a metadata database to store information about DAGs, tasks, and their states. Initialize the database with:

airflow db init

Create a User: For accessing the Airflow web interface, create an admin user:

airflow users create \
--username admin \
--firstname YOUR_FIRST_NAME \
--lastname YOUR_LAST_NAME \
--role Admin \
--email YOUR_EMAIL

Start the Airflow Web Server and Scheduler: Run the following commands in separate terminal windows:

airflow webserver --port 8080  # Starts the web server
airflow scheduler # Starts the scheduler

Access the Airflow Web Interface: Open your web browser and go to http://localhost:8080 to access the Airflow UI. Log in with the admin credentials you created.

Basic Configuration and Setup

  1. Airflow Configuration File: Airflow’s behavior is controlled by the airflow.cfg file located in the Airflow home directory. You can adjust various settings such as executor type, database connection, and logging configuration.
  2. Setting Up Connections: If your workflows involve connecting to external systems (e.g., databases, cloud services), configure these connections in the Airflow UI under the “Admin” tab -> “Connections”.
  3. Creating a DAG Directory: Airflow looks for DAG files in the directory specified by the dags_folder setting in airflow.cfg. By default, this is set to the dags directory under the Airflow home. Create this directory to store your DAG files:
mkdir -p $AIRFLOW_HOME/dags

With Airflow installed and configured, you are now ready to start creating and running your workflows.

Your engagement, whether through claps, comments, or following me, fuels my passion for creating and sharing more informative content.
If you’re interested in more
SQL or Python content, please consider following me. Alternatively, you can click here to check out my Python list on Medium.

Creating Your First Airflow DAG (Directed Acyclic Graph)

Creating a Directed Acyclic Graph (DAG) in Airflow involves defining a series of tasks and their dependencies. DAGs are written in Python, making it easy to leverage Python’s flexibility and libraries. Let’s walk through the process of creating a simple DAG.

Explanation of DAGs

A DAG in Airflow is a collection of all the tasks you want to run, organized in a way that reflects their relationships and dependencies. Each DAG is defined in a Python script that specifies the tasks and the order in which they should run.

Writing a Simple DAG in Python

  • Create a New DAG File: Create a new Python file in your DAGs directory ($AIRFLOW_HOME/dags). For example, create a file named simple_dag.py.
  • Import Necessary Modules: In your DAG file, import the required modules:
from datetime import datetime, timedelta
from airflow import DAG
from airflow.operators.dummy_operator import DummyOperator
from airflow.operators.python_operator import PythonOperator
  • Define Default Arguments: Set up default arguments for the DAG, such as the start date, schedule interval, and retry settings:
default_args = {
'owner': 'airflow',
'depends_on_past': False,
'email_on_failure': False,
'email_on_retry': False,
'retries': 1,
'retry_delay': timedelta(minutes=5),
}
  • Instantiate the DAG: Create an instance of the DAG with a unique ID and assign the default arguments:
dag = DAG(
'simple_dag',
default_args=default_args,
description='A simple tutorial DAG',
schedule_interval=timedelta(days=1),
start_date=datetime(2023, 1, 1),
catchup=False,
)
  • Define Tasks: Define the tasks you want to include in the DAG. Here, we’ll create a simple Python function and a DummyOperator task:
def print_hello():
print("Hello, World!")

start = DummyOperator(
task_id='start',
dag=dag,
)

hello_task = PythonOperator(
task_id='hello_task',
python_callable=print_hello,
dag=dag,
)

end = DummyOperator(
task_id='end',
dag=dag,
)
  • Set Task Dependencies: Define the order in which tasks should run by setting their dependencies:
start >> hello_task >> end

Here is the complete simple_dag.py file:

from datetime import datetime, timedelta
from airflow import DAG
from airflow.operators.dummy_operator import DummyOperator
from airflow.operators.python_operator import PythonOperator

default_args = {
'owner': 'airflow',
'depends_on_past': False,
'email_on_failure': False,
'email_on_retry': False,
'retries': 1,
'retry_delay': timedelta(minutes=5),
}

dag = DAG(
'simple_dag',
default_args=default_args,
description='A simple tutorial DAG',
schedule_interval=timedelta(days=1),
start_date=datetime(2023, 1, 1),
catchup=False,
)

def print_hello():
print("Hello, World!")

start = DummyOperator(
task_id='start',
dag=dag,
)

hello_task = PythonOperator(
task_id='hello_task',
python_callable=print_hello,
dag=dag,
)

end = DummyOperator(
task_id='end',
dag=dag,
)

start >> hello_task >> end

Running and Monitoring the DAG

  1. Start Airflow Components: Ensure the Airflow web server and scheduler are running:
airflow webserver --port 8080
airflow scheduler

2. Access the Airflow Web UI: Open your web browser and go to http://localhost:8080. You should see your DAG (simple_dag) listed on the main page.

Get CyCoderX’s stories in your inbox

Join Medium for free to get updates from this writer.

3. Trigger the DAG: Click on the toggle next to your DAG to activate it, then click on the DAG name to view its details. You can manually trigger the DAG by clicking on the “Trigger DAG” button.

4. Monitor the DAG: Use the web UI to monitor the progress of your DAG, view logs, and check the status of individual tasks.

By following these steps, you’ve created and run a simple DAG in Airflow, illustrating the basics of workflow automation with Python and Airflow.

Conclusion

In the ever-evolving field of software development, workflow automation is not just a convenience but a necessity for handling complex data processes efficiently. Apache Airflow, with its powerful orchestration capabilities, paired with Python’s versatility, provides a robust solution for automating and managing workflows.

This article aimed to introduce you to the foundational concepts of Apache Airflow and its seamless integration with Python. From understanding Directed Acyclic Graphs (DAGs) to setting up your Airflow environment and creating your first DAG, we covered essential steps to get you started on your journey toward efficient workflow automation.

Whether you’re a beginner looking to explore the basics or a tech enthusiast aiming to deepen your understanding, mastering these tools can significantly enhance your ability to manage and scale your projects. By incorporating Airflow into your GitHub projects, you can ensure smoother, more reliable operations and focus on what truly matters — building innovative solutions. You can also build on top of this foundation to create more complex and customized workflows, tailoring Airflow to fit your specific project needs.

Happy coding!

Press enter or click to view image in full size
Photo by Call Me Fred on Unsplash

Final Words:

Thank you for taking the time to read my article.

This article was first published on medium by CyCoderX.

Hey There! I’m CyCoderX, a data engineer who loves crafting end-to-end solutions. I write articles about Python, SQL, AI, Data Engineering, lifestyle and more!

Join me as we explore the exciting world of tech, data and beyond!

For similar articles and updates, feel free to explore my Medium profile:

If you enjoyed this article, consider following for future updates.

Interested in Python content and tips? Click here to check out my list on Medium.

Interested in more SQL, Databases and Data Engineering content? Click here to find out more!

What did you think about this article? Let me know in the comments below … or above, depending on your device! 🙃

In Plain English 🚀

Thank you for being a part of the In Plain English community! Before you go:

--

--

CyCoderX
CyCoderX

Written by CyCoderX

Machine Learning & Data Engineer | Data Science | Python & SQL | AI | Software Developer | Azure & AWS Cloud Specialist | Blogger simplifying Big Data & Trends

Python in Plain English
Python in Plain English

Published in Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.