Another way for reusing DAGs not stated in docs
- Dominant language
- Python
- Stars
- 46.9k
- Forks
- 17.8k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 484
Description
### What do you see as an issue?
[In official docs](https://airflow.apache.org/docs/apache-airflow/stable/howto/dynamic-dag-generation.html) there have been stated several ways for dynamic DAGs generation. I found out another way which looks very convenient, so I'd like to get your opinion of it.
### Solving the problem
```
# reusable_dag.py
import datetime
from airflow.decorators import task
from airflow import DAG
DEFAULT_DAG_CONFIG = {
'schedule_interval': None,
'catchup': False,
'start_date': datetime.datetime.fromisoformat('2025-04-29'),
'default_args': {
'depends_on_past': False
},
'max_active_tasks': 1,
'max_active_runs': 1
}
def reusable_dag_generator(
dag_obj: DAG,
printable_msg: str
):
"""Reusable dag generator used as a template for DAG generation"""
with dag_obj:
@task()
def start_dag():
pass
@task()
def print_msg():
print(printable_msg)
@task()
def finish_dag():
pass
start_dag() >> print_msg() >> finish_dag()
return dag_obj
# dag0.py
from airflow import DAG
from reusable_dag import reusable_dag_generator, DEFAULT_DAG_CONFIG
dag = reusable_dag_generator(
dag_obj=DAG(
dag_id='test0',
**DEFAULT_DAG_CONFIG
),
printable_msg='Hello DAG0!'
)
# dag1.py
from airflow import DAG
from reusable_dag import reusable_dag_generator, DEFAULT_DAG_CONFIG
dag = reusable_dag_generator(
dag_obj=DAG(
dag_id='test1',
**DEFAULT_DAG_CONFIG
),
printable_msg='Hello DAG1!'
)
```
### Anything else
If provided way looks convenient, I would be glad to make a PR for docs enhancements.
### Are you willing to submit PR?
- [x] Yes I am willing to submit a PR!
### Code of Conduct
- [x] I agree to follow this project's [Code of Conduct](https://github.com/apache/airflow/blob/main/CODE_OF_CONDUCT.md)
Contributor guide
Research direction
Start by reading the linked Dynamic DAG Generation documentation and compare the proposed reusable_dag.py pattern with the approaches already described there. Done means deciding whether this pattern belongs in the official docs and, if so, documenting it with a clear example and explanation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100