Skip to content

Commit

Permalink
Add decorator for script (#507)
Browse files Browse the repository at this point in the history
Signed-off-by: Flaviu Vadan <[email protected]>
  • Loading branch information
flaviuvadan authored Mar 27, 2023
1 parent 8cf44d1 commit 0a9385c
Show file tree
Hide file tree
Showing 30 changed files with 744 additions and 133 deletions.
38 changes: 31 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,43 @@ Another option for workflow submission without the authentication layer is using

# Examples

### Single step script

```python
from hera.workflows import DAG, Container, Parameter, Workflow
from hera.workflows import Steps, Workflow, script


@script()
def echo(message: str):
print(message)


with Workflow(
generate_name="single-script-",
entrypoint="steps",
) as w:
with Steps(name="steps"):
echo(arguments={"message": "A"})

w.create()

```

### DAG diamond

```python
from hera.workflows import DAG, Workflow, script


@script()
def echo(message: str):
print(message)


with Workflow(
generate_name="dag-diamond-",
entrypoint="diamond",
) as w:
echo = Container(
name="echo",
image="alpine:3.7",
command=["echo", "{{inputs.parameters.message}}"],
inputs=[Parameter(name="message")],
)
with DAG(name="diamond"):
A = echo(name="A", arguments={"message": "A"})
B = echo(name="B", arguments={"message": "B"})
Expand Down
108 changes: 108 additions & 0 deletions docs/examples/workflows/coinflip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Coinflip





## Hera

```python
from hera.workflows import DAG, Workflow, script


@script()
def flip():
import random

result = "heads" if random.randint(0, 1) == 0 else "tails"
print(result)


@script()
def heads():
print("it was heads")


@script()
def tails():
print("it was tails")


with Workflow(generate_name="coinflip-", entrypoint="d") as w:
with DAG(name="d") as s:
f = flip()
heads().on_other_result(f, "heads")
tails().on_other_result(f, "tails")
```

## YAML

```yaml
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: coinflip-
spec:
entrypoint: d
templates:
- dag:
tasks:
- name: flip
template: flip
- depends: flip
name: heads
template: heads
when: '{{tasks.flip.outputs.result}} == heads'
- depends: flip
name: tails
template: tails
when: '{{tasks.flip.outputs.result}} == tails'
name: d
- name: flip
script:
command:
- python
image: python:3.7
source: 'import os
import sys
sys.path.append(os.getcwd())
import random
result = "heads" if random.randint(0, 1) == 0 else "tails"
print(result)
'
- name: heads
script:
command:
- python
image: python:3.7
source: 'import os
import sys
sys.path.append(os.getcwd())
print("it was heads")
'
- name: tails
script:
command:
- python
image: python:3.7
source: 'import os
import sys
sys.path.append(os.getcwd())
print("it was tails")
'
```
101 changes: 101 additions & 0 deletions docs/examples/workflows/dag_conditional_parameters.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Dag Conditional Parameters





## Hera

```python
from hera.expr import g
from hera.workflows import DAG, Parameter, Workflow, script


@script(add_cwd_to_sys_path=False, image="python:alpine3.6")
def heads():
print("heads")


@script(add_cwd_to_sys_path=False, image="python:alpine3.6")
def tails():
print("tails")


@script(add_cwd_to_sys_path=False, image="python:alpine3.6")
def flip_coin():
import random

print("heads" if random.randint(0, 1) == 0 else "tails")


with Workflow(
generate_name="dag-conditional-parameter-",
entrypoint="main",
) as w:
with DAG(name="main") as main_dag:
fc = flip_coin(name="flip-coin")
h = heads(name="heads").on_other_result(fc, "heads")
t = tails(name="tails").on_other_result(fc, "tails")

expression = g.tasks["flip-coin"].outputs.result == "heads"
expression = expression.check(g.tasks.heads.outputs.result, g.tasks.tails.outputs.result) # type: ignore
main_dag.outputs = [Parameter(name="stepresult", value_from={"expression": str(expression)})]
```

## YAML

```yaml
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: dag-conditional-parameter-
spec:
entrypoint: main
templates:
- dag:
tasks:
- name: flip-coin
template: flip-coin
- depends: flip-coin
name: heads
template: heads
when: '{{tasks.flip-coin.outputs.result}} == heads'
- depends: flip-coin
name: tails
template: tails
when: '{{tasks.flip-coin.outputs.result}} == tails'
name: main
outputs:
parameters:
- name: stepresult
valueFrom:
expression: 'tasks[''flip-coin''].outputs.result == ''heads'' ? tasks.heads.outputs.result
: tasks.tails.outputs.result'
- name: flip-coin
script:
command:
- python
image: python:alpine3.6
source: 'import random
print("heads" if random.randint(0, 1) == 0 else "tails")
'
- name: heads
script:
command:
- python
image: python:alpine3.6
source: 'print("heads")
'
- name: tails
script:
command:
- python
image: python:alpine3.6
source: 'print("tails")
'
```
89 changes: 89 additions & 0 deletions docs/examples/workflows/dag_diamond_with_callable_decorators.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Dag Diamond With Callable Decorators





## Hera

```python
from hera.workflows import (
DAG,
Workflow,
script,
)


@script(add_cwd_to_sys_path=False, image="python:alpine3.6")
def echo(message):
print(message)


with Workflow(generate_name="dag-diamond-", entrypoint="diamond") as w:
with DAG(name="diamond"):
A = echo(name="A", arguments={"message": "A"})
B = echo(name="B", arguments={"message": "B"})
C = echo(name="C", arguments={"message": "C"})
D = echo(name="D", arguments={"message": "D"})
A >> [B, C] >> D
```

## YAML

```yaml
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: dag-diamond-
spec:
entrypoint: diamond
templates:
- dag:
tasks:
- arguments:
parameters:
- name: message
value: A
name: A
template: echo
- arguments:
parameters:
- name: message
value: B
depends: A
name: B
template: echo
- arguments:
parameters:
- name: message
value: C
depends: A
name: C
template: echo
- arguments:
parameters:
- name: message
value: D
depends: B && C
name: D
template: echo
name: diamond
- inputs:
parameters:
- name: message
name: echo
script:
command:
- python
image: python:alpine3.6
source: 'import json
try: message = json.loads(r''''''{{inputs.parameters.message}}'''''')
except: message = r''''''{{inputs.parameters.message}}''''''
print(message)
'
```
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Http
# Http



Expand Down
Loading

0 comments on commit 0a9385c

Please sign in to comment.