0
0
Apache Airflowdevops~10 mins

PythonOperator for custom logic in Apache Airflow - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - PythonOperator for custom logic
Define Python function
Create PythonOperator with function
Add operator to DAG
DAG runs -> PythonOperator triggers
Function executes custom logic
Task completes with result
This flow shows how a Python function is wrapped by PythonOperator, added to a DAG, and executed when the DAG runs.
Execution Sample
Apache Airflow
def greet():
    print('Hello from Airflow!')

from airflow.operators.python import PythonOperator

from airflow import DAG
from datetime import datetime

default_args = {
    'start_date': datetime(2023, 1, 1)
}

dag = DAG('greet_dag', default_args=default_args, schedule_interval='@daily')

greet_task = PythonOperator(
    task_id='greet',
    python_callable=greet,
    dag=dag
)
Defines a simple function and creates a PythonOperator that runs it as a task.
Process Table
StepActionEvaluationResult
1Define function greetFunction greet() createdFunction ready to use
2Create PythonOperator greet_taskAssign greet as python_callableOperator ready with task_id 'greet'
3Add greet_task to DAGDAG includes greet_taskTask scheduled in DAG
4DAG run startsPythonOperator triggers greetTask execution begins
5Execute greet()Print statement runsOutput: 'Hello from Airflow!'
6Task completesNo errorsTask marked success
💡 Task completes successfully after running the custom Python function.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
greetundefinedfunction greet()function greet()function greet()function greet()function greet()function greet()
greet_taskundefinedundefinedPythonOperator with greetPythonOperator with greetPythonOperator with greetPythonOperator with greetPythonOperator with greet
DAGundefinedundefinedundefinedDAG with greet_taskDAG running greet_taskDAG running greet_taskDAG completed greet_task
Key Moments - 3 Insights
Why do we pass the function name without parentheses to python_callable?
Because python_callable expects a reference to the function itself, not the result of calling it. See execution_table step 2 where greet is assigned without () to keep it callable by Airflow.
What happens if the function prints output? Does Airflow capture it?
Yes, Airflow captures standard output in task logs. In execution_table step 5, the print runs and its output is stored in logs accessible via Airflow UI.
How does Airflow know when the task is done?
Airflow marks the task success after the function completes without error, as shown in execution_table step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output at step 5?
AError message
BNo output
C'Hello from Airflow!'
D'Task completed'
💡 Hint
Check the 'Result' column at step 5 in the execution_table.
At which step does Airflow start running the Python function?
AStep 2
BStep 4
CStep 1
DStep 6
💡 Hint
Look for when the DAG run triggers the PythonOperator in the execution_table.
If you mistakenly write python_callable=greet() instead of python_callable=greet, what happens?
AFunction runs immediately during DAG definition
BTask runs normally when triggered
CAirflow throws syntax error
DTask is skipped
💡 Hint
Think about when the function is called if parentheses are used, referencing key_moments about function references.
Concept Snapshot
PythonOperator runs custom Python code in Airflow.
Define a Python function.
Pass it to PythonOperator via python_callable (no parentheses).
Add operator to DAG.
When DAG runs, operator calls your function.
Output is logged; task success depends on function completion.
Full Transcript
This visual execution shows how to use PythonOperator in Airflow to run custom Python logic. First, you define a Python function with the code you want to run. Then, you create a PythonOperator and assign your function to its python_callable parameter without parentheses, so Airflow can call it later. Next, you add this operator as a task in your DAG. When the DAG runs, Airflow triggers the PythonOperator, which calls your function. The function executes its code, such as printing a message, and Airflow captures this output in logs. After the function finishes without errors, Airflow marks the task as successful. This process allows you to run any Python code as part of your Airflow workflows.