Skip to content

Commit e8d85ea

Browse files
committed
feat(yaml): adds initial script to generate yaml division file
1 parent f2bbe7f commit e8d85ea

4 files changed

Lines changed: 272 additions & 0 deletions

File tree

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ description = " Python alternative to creating and running argo workflows in the
1616
dependencies = [
1717
"gql",
1818
"aiohttp",
19+
"hera",
20+
"requests",
1921
] # Add project dependencies here, e.g. ["click", "numpy"]
2022
dynamic = ["version"]
2123
license.file = "LICENSE"

src/divisionyaml.yaml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
apiVersion: argoproj.io/v1alpha1
2+
kind: ClusterWorkflowTemplate
3+
metadata:
4+
generateName: hera-division
5+
namespace: https://staging.workflows.diamond.ac.uk/workflows/
6+
annotations:
7+
workflows.argoproj.io/description: |-
8+
Takes a numerical input and returns the
9+
remainder, output float, and output string to a json file
10+
workflows.argoproj.io/title: Division via hera test
11+
workflows.diamond.ac.uk/repository: https://github.com/DiamondLightSource/python-interface-to-workflows
12+
labels:
13+
workflows.diamond.ac.uk/science-group-examples: 'true'
14+
spec:
15+
entrypoint: divide
16+
templates:
17+
- name: divide
18+
steps:
19+
- - name: first
20+
template: do-devision
21+
arguments:
22+
parameters:
23+
- name: x
24+
value: '2'
25+
- name: y
26+
value: '5'
27+
- name: do-devision
28+
inputs:
29+
parameters:
30+
- name: x
31+
- name: y
32+
outputs:
33+
artifacts:
34+
- name: json-output
35+
path: /output-dir/output.json
36+
script:
37+
image: python:3.10
38+
source: |-
39+
import os
40+
import sys
41+
sys.path.append(os.getcwd())
42+
import json
43+
try: x = json.loads(r'''{{inputs.parameters.x}}''')
44+
except: x = r'''{{inputs.parameters.x}}'''
45+
try: y = json.loads(r'''{{inputs.parameters.y}}''')
46+
except: y = r'''{{inputs.parameters.y}}'''
47+
48+
div = x / y
49+
intdiv = x // y
50+
remain = x % y
51+
dictionary_of_results = {'divide': div, 'integer divisor': intdiv, 'remainder': remain}
52+
with open('/output-dir/output.json', 'w') as otpt:
53+
json.dump(dictionary_of_results, otpt)
54+
command:
55+
- python
56+
volumeMounts:
57+
- name: output-dir
58+
mountPath: /output-dir/
59+
volumeClaimTemplates:
60+
- metadata:
61+
name: output-dir
62+
spec:
63+
accessModes:
64+
- ReadWriteOnce
65+
resources:
66+
requests:
67+
storage: 1Mi
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import json
2+
3+
from hera.workflows import (
4+
Artifact,
5+
Steps,
6+
Volume,
7+
Workflow,
8+
WorkflowsService,
9+
script, # pyright: ignore[reportUnknownVariableType]
10+
)
11+
from hera.workflows import models as m
12+
13+
14+
@script(
15+
volume_mounts=[m.VolumeMount(name="output-dir", mount_path="/output-dir/")],
16+
outputs=Artifact(name="json-output", path="/output-dir/output.json"),
17+
)
18+
def do_devision(x: int, y: int):
19+
div = x / y
20+
intdiv = x // y
21+
remain = x % y
22+
dictionary_of_results = {
23+
"divide": div,
24+
"integer divisor": intdiv,
25+
"remainder": remain,
26+
}
27+
with open("/output-dir/output.json", "w") as otpt:
28+
json.dump(dictionary_of_results, otpt)
29+
30+
31+
with Workflow(
32+
generate_name="hera-division",
33+
entrypoint="divide",
34+
namespace="https://staging.workflows.diamond.ac.uk/workflows/",
35+
api_version="argoproj.io/v1alpha1",
36+
kind="ClusterWorkflowTemplate",
37+
labels={"workflows.diamond.ac.uk/science-group-examples": "true"},
38+
annotations={
39+
"workflows.argoproj.io/title": "Division via hera test",
40+
"workflows.argoproj.io/description": """Takes a numerical input and returns the
41+
remainder, output float, and output string to a json file""",
42+
"workflows.diamond.ac.uk/repository": "https://github.com/DiamondLightSource/python-interface-to-workflows",
43+
},
44+
volumes=Volume(name="output-dir", mount_path="/output-dir", size="1Mi"),
45+
) as w:
46+
with Steps(name="divide"):
47+
do_devision(name="first", arguments={"x": 2, "y": 5})
48+
49+
50+
with open("src/divisionyaml.yaml", "w") as div:
51+
div.write(w.to_yaml()) # pyright: ignore[reportUnknownMemberType]
52+
53+
sendthis = m.WorkflowSubmitRequest(
54+
namespace="https://staging.workflows.diamond.ac.uk/workflows/",
55+
resource_kind="ClusterWorkflowTemplate",
56+
resource_name="hera-division",
57+
)
58+
a = WorkflowsService(
59+
host="https://staging.workflows.diamond.ac.uk/graphql",
60+
namespace="https://staging.workflows.diamond.ac.uk/workflows/",
61+
token="a",
62+
)
63+
WorkflowsService.submit_workflow(a, req=sendthis)

0 commit comments

Comments
 (0)