UnionRemote examples#

Registering and running a workflow#

In the following example we register and run a workflow and retrieve its output:

A simple project#
├── remote.py
└── workflow
    ├── __init__.py
    └── example.py

The workflow code that will be registered and run on Union resides in the workflow directory and consists of an empty __init__.py file and the workflow and task code in example.py:

example.py#
import os
from union import task, workflow, FlyteFile


@task()
def create_file(message: str) -> FlyteFile:
    with open("data.txt", "w") as f:
        f.write(message)
    return FlyteFile(path="data.txt")

@workflow
def my_workflow(message: str) -> FlyteFile:
    f = create_file(message)
    return f

The file remote.py contains the UnionRemote logic. It is not part of the workflow code, and is meant to be run on your local machine.

remote.py#
from union import UnionRemote
from workflow.example import my_workflow


def run_workflow():
    remote = UnionRemote()
    remote.fast_register_workflow(entity=my_workflow)
    execution = remote.execute(
        entity=my_workflow,
        inputs={"message": "Hello, world!"},
        wait=True)
    output = execution.outputs["o0"]
    print(output)
    with open(output, "r") as f:
        read_lines = f.readlines()
    print(read_lines)


if __name__ == "__main__":
    run_workflow()

You can run the code with:

$ python remote.py

The my_workflow workflow and the create_file task is registered and run. Once the the workflow completes, the output is passed back to the run_workflow function and printed out.

The output is also be available via the UI, in the Outputs tab of the create_file task details view:

Outputs

The steps above demonstrates the simplest way of registering and running a workflow with UnionRemote. For more options and details see API reference > UnionRemote > Entrypoint.