No tienes acceso a esta clase

¡Continúa aprendiendo! Únete y comienza a potenciar tu carrera

Python Operator

13/29
Recursos

Aportes 27

Preguntas 4

Ordenar por:

¿Quieres ver más aportes, preguntas y respuestas de la comunidad?

This is wonderful!

from airflow import DAG
from airflow.operators.python import PythonOperator

from datetime import datetime


def print_hello():
    print('Hello people from python funtion')

with DAG(
    dag_id='python_operator',
    description='Fisrt dag using python operator',
    schedule_interval='@once',
    start_date=datetime(2022,8,1)
    ) as dag:
    
    t1 = PythonOperator(
        task_id = 'hello_with_python',
        python_callable = print_hello
    )
    ```

Me gusta la parte de que se se sigan podiendo reutilizar funciones, dejo ejemplo

from airflow import DAG
from airflow.operators.python import PythonOperator
from datetime import datetime

def print_hello(country, **kwargs):
    print(f'I am processing this {country}')


with DAG(dag_id='pythonoperator'
        , description='Utilizando python operator'
        , start_date=datetime(2023, 6, 7)
        , schedule_interval='@once') as dag:


    t1 = PythonOperator(task_id='process_ar'
                        , python_callable=print_hello
                        , op_kwargs={'country':'AR'})

    t2 = PythonOperator(task_id='process_uy'
                        , python_callable=print_hello
                        , op_kwargs={'country':'UY'})

    t1 >> t2 
from airflow import DAG
from datetime import datetime
from airflow.operators.python import PythonOperator

def print_hello():
    print("HELLO PAPUS")

with DAG(dag_id="pythonoperator",
         description="Nuestro primer DAG utilizando python ope",
         schedule_interval="@once",
         start_date =datetime(2022,12,6)) as dag:


    t1 = PythonOperator(task_id="hello_with_python",
                        python_callable=print_hello)

    t1                        ```
![](https://static.platzi.com/media/user_upload/image-731119f0-347d-4d91-9937-8eeff5dba554.jpg)
from airflow import DAGfrom airflow.operators.python import PythonOperatorfrom datetime import datetime def print\_hello():    print("Hola de pruebas.") with DAG(dag\_id="pythonoperator",         description="Nuestro primer DAG utilizando python operator",         schedule\_interval="@once",         start\_date=datetime(2022, 8, 1)) as dag:        t1 = PythonOperator(        task\_id="hello\_with\_python",        python\_callable=print\_hello    ) t1
![](https://static.platzi.com/media/user_upload/image-fea1f5a4-84d9-40f6-90b8-f96dabcf813f.jpg)

😃

from airflow import DAG
from airflow.operators.python import PythonOperator
from datetime import datetime


def print_hello():
    return print('hello world with python in airflow')


with DAG(dag_id='primer_dag_python',
         schedule_interval='@once',
         start_date=datetime(2022, 7, 1)) as dag:

    t1 = PythonOperator(task_id='primer_task_python',
                        python_callable=print_hello)

![](https://static.platzi.com/media/user_upload/image-81162b99-13ce-4eba-a4c2-7bdd4301331b.jpg)
from airflow import DAGfrom datetime import datetimefrom airflow.operators.python import PythonOperator \#If you need access to Airflow context variables (e.g., ds, run\_id), you must include \*\*kwargs.def hello(country): #\*\*kwaargs):    print(f"Hello {country}") with DAG (dag\_id="pythonOperator", description="Python Operator DAG", start\_date=datetime(2025, 3, 27), schedule="@once") as dag:     t1 = PythonOperator(task\_id='taskPython', python\_callable=hello,op\_args=\["Mexico"])     t2 = PythonOperator(task\_id='taskPython2', python\_callable=hello,op\_kwargs={'countrty':"France"}) \# op\_args = you can pass a list of arguments to the function. if you have more than one argument you must pass the arguments in order in a list# op\_kwargs = you can pass a dictionary where the keys are the name of the argumnet ant the values are the values you want to pass. if you have more than one argument you can pass them in any order.
![](https://static.platzi.com/media/user_upload/image-690dc156-eb53-4f5b-ac3a-f0b464984b44.jpg)
![](https://static.platzi.com/media/user_upload/image-3e2eb02d-cfd9-4269-93a8-e68f6fe734be.jpg)![](https://static.platzi.com/media/user_upload/image-994a3f15-c7f5-44fa-b2c8-a62a451b4dda.jpg)Python Operator successfull!!
![](https://static.platzi.com/media/user_upload/image-a55f6f32-6323-43cf-9d97-b4d4f3409b5b.jpg)
![](https://static.platzi.com/media/user_upload/image-c4fdcafa-df25-4f16-abab-5895b72ff6ba.jpg)
![](https://static.platzi.com/media/user_upload/Captura%20de%20pantalla%202024-11-28%20194217-3c700ef5-e201-48e7-a72f-84bfc2918731.jpg)
¿Cómo ejecuto SQLAlchemy 2 en los DAGs usando PythonOperatos? dado que Airflow usa la versión 1.4. pasa que tengo toda una estructura de código que está en un árbol de directorios por orden para hacer un ETL, y si usara el PythonEnvOperator no me toda los demás archivos.
![](https://static.platzi.com/media/user_upload/image-16a6a8a4-d095-4e6f-8edb-e1f77c424315.jpg)
![](https://static.platzi.com/media/user_upload/image-bfb8e948-9816-4ecf-8957-53b5292dc300.jpg)

Ahora con Python Operator:

Si quisiera usar por ejemplo la librería pandas, donde se instala para usarla en el DAG?

Holis from python operator!!