|
| 1 | +Jobs are isolated unit of work which can be python functions, jupyter notebooks or shell scripts. |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +Considering a simple function: |
| 6 | + |
| 7 | +```python |
| 8 | +def add_numbers(x: int, y: int): |
| 9 | + # save some data in data.csv |
| 10 | + return x + y |
| 11 | +``` |
| 12 | + |
| 13 | +The runnable representation of it is: |
| 14 | + |
| 15 | +```python |
| 16 | +from functions import add_numbers |
| 17 | +from runnable import PythonJob, Catalog |
| 18 | + |
| 19 | +write_catalog = Catalog(put=["data.csv"]) |
| 20 | +job = PythonJob(function=add_numbers, |
| 21 | + returns["sum_of_numbers"], |
| 22 | + catalog=write_catalog, |
| 23 | + ) |
| 24 | + |
| 25 | +``` |
| 26 | + |
| 27 | +```PythonJob``` requires a function to call. The input parameters are passed in |
| 28 | + from the parameters provided at the time of execution. |
| 29 | + |
| 30 | +The return parameters are stored for future reference. Any data object generated in the |
| 31 | +process can be saved to the catalog. |
| 32 | + |
| 33 | +<hr style="border:2px dotted orange"> |
| 34 | + |
| 35 | + |
| 36 | +## Python functions |
| 37 | + |
| 38 | +You can use Python functions as jobs in a pipeline, enabling flexible encapsulation of logic, parameter passing, result capturing, and cataloging of outputs. |
| 39 | + |
| 40 | +=== "Basic Python Function as a Job" |
| 41 | + ```python |
| 42 | + --8<-- "examples/11-jobs/python_tasks.py" |
| 43 | + ``` |
| 44 | + |
| 45 | + The stdout (e.g., "Hello World!") and logs are captured and stored in the catalog for traceability. |
| 46 | + |
| 47 | +=== "Writing Data to the Catalog" |
| 48 | + ```python |
| 49 | + --8<-- "examples/11-jobs/catalog.py" |
| 50 | + ``` |
| 51 | + |
| 52 | + The `Catalog` object specifies which files or data should be saved after job execution. |
| 53 | + |
| 54 | +=== "Passing and Returning Parameters" |
| 55 | + |
| 56 | + ```python |
| 57 | + --8<-- "examples/11-jobs/passing_parameters_python.py" |
| 58 | + ``` |
| 59 | + |
| 60 | + Parameters can be passed at execution time, and returned values can be automatically handled, serialized, and tracked as metrics. |
| 61 | + |
| 62 | +--- |
| 63 | + |
| 64 | +## Notebooks |
| 65 | + |
| 66 | +You can also use Jupyter notebooks as jobs in your pipeline. This allows you to encapsulate notebook logic, capture outputs, and integrate notebooks seamlessly into your workflow. |
| 67 | + |
| 68 | +=== "Notebook as a Job" |
| 69 | + ```python |
| 70 | + --8<-- "examples/11-jobs/notebooks.py" |
| 71 | + ``` |
| 72 | + The output of the notebook will be captured as execution log |
| 73 | + along with the actual notebook and stored in the catalog for traceability. |
| 74 | + |
| 75 | +--- |
| 76 | + |
| 77 | +## Shell script |
| 78 | + |
| 79 | +You can also use shell scripts or commands as jobs in your pipeline. This allows you to execute any shell command, capture its output, and integrate it into your workflow. |
| 80 | + |
| 81 | +=== "Shell Script" |
| 82 | + ```python |
| 83 | + --8<-- "examples/11-jobs/scripts.py" |
| 84 | + ``` |
| 85 | + The stdout and stderr of the shell command are captured as execution log and stored in the catalog for traceability. |
| 86 | + |
| 87 | +For more advanced examples, see the files in `examples/11-jobs/`. |
0 commit comments