From 3aa5d0359f44cdb8cebe083197d6f3e98d8e1fda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Bregu=C5=82a?= Date: Fri, 25 Oct 2019 15:43:42 +0200 Subject: [PATCH] [AIRFLOW-5770] Add example for PythonVirtualenvOperator --- .../example_dags/example_python_operator.py | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/airflow/example_dags/example_python_operator.py b/airflow/example_dags/example_python_operator.py index 86403ceb25b63..ecfba76cba1a4 100644 --- a/airflow/example_dags/example_python_operator.py +++ b/airflow/example_dags/example_python_operator.py @@ -24,7 +24,7 @@ import airflow from airflow.models import DAG -from airflow.operators.python_operator import PythonOperator +from airflow.operators.python_operator import PythonOperator, PythonVirtualenvOperator args = { 'owner': 'Airflow', @@ -71,3 +71,33 @@ def my_sleeping_function(random_base): run_this >> task # [END howto_operator_python_kwargs] + + +def callable_virtualenv(): + """ + Example function that will be performed in a virtual environment. + + Importing at the module level ensures that it will not attempt to import the + library before it is installed. + """ + from colorama import Fore, Back, Style + from time import sleep + print(Fore.RED + 'some red text') + print(Back.GREEN + 'and with a green background') + print(Style.DIM + 'and in dim text') + print(Style.RESET_ALL) + for _ in range(10): + print(Style.DIM + 'Please wait...', flush=True) + sleep(10) + print('Finished') + + +virtualenv_task = PythonVirtualenvOperator( + task_id="virtualenv_python", + python_callable=callable_virtualenv, + requirements=[ + "colorama==0.4.0" + ], + system_site_packages=False, + dag=dag, +)