Skip to content

Commit 054792b

Browse files
committed
feat(ipynb): adds notebook w/ instructions on how to run
1 parent 4ee6c6e commit 054792b

1 file changed

Lines changed: 138 additions & 0 deletions

File tree

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "1e3146f1",
6+
"metadata": {},
7+
"source": [
8+
"# Running in VSCode:\n",
9+
"\n",
10+
"1. Set kernal to python-interface-to-workflows 3.11.x\n",
11+
"2. Hit F1, run Jupyter: Import Notebook to Script\n",
12+
"3. Click notebook_division.ipynb\n",
13+
"4. Run the cells sequentially"
14+
]
15+
},
16+
{
17+
"cell_type": "code",
18+
"execution_count": null,
19+
"id": "494174ef",
20+
"metadata": {},
21+
"outputs": [],
22+
"source": [
23+
"import json\n",
24+
"\n",
25+
"from hera.workflows import (\n",
26+
" Artifact,\n",
27+
" EmptyDirVolume,\n",
28+
" Steps,\n",
29+
" Workflow,\n",
30+
" script, # pyright: ignore[reportUnknownVariableType]\n",
31+
")\n",
32+
"from hera.workflows import models as m\n",
33+
"\n",
34+
"\n",
35+
"@script(\n",
36+
" volume_mounts=[m.VolumeMount(name=\"output-dir\", mount_path=\"/output-dir/\")],\n",
37+
" outputs=Artifact(name=\"json-output\", path=\"/output-dir/output.json\"),\n",
38+
")\n",
39+
"def do_division(a: int, b: int):\n",
40+
" div = a / b\n",
41+
" intdiv = a // b\n",
42+
" remain = a % b\n",
43+
" dictionary_of_results = {\n",
44+
" \"divide\": div,\n",
45+
" \"quotient\": intdiv,\n",
46+
" \"remainder\": remain,\n",
47+
" }\n",
48+
" with open(\"/output-dir/output.json\", \"w\") as otpt:\n",
49+
" json.dump(dictionary_of_results, otpt)\n",
50+
"\n",
51+
"\n",
52+
"with Workflow(\n",
53+
" generate_name=\"hera-division-\", # when running on graphql this should be name\n",
54+
" entrypoint=\"divide\",\n",
55+
" api_version=\"argoproj.io/v1alpha1\",\n",
56+
" kind=\"Workflow\", # ClusterWorkflowTemplate\", when on graphql\n",
57+
" labels={\"workflows.diamond.ac.uk/science-group-examples\": \"true\"},\n",
58+
" annotations={\n",
59+
" \"workflows.argoproj.io/title\": \"Division via hera test\",\n",
60+
" \"workflows.argoproj.io/description\": \"\"\"Takes a numerical input and returns\n",
61+
" the remainder, output float, and output string to a json file\"\"\",\n",
62+
" \"workflows.diamond.ac.uk/repository\": \"https://github.com/DiamondLightSource/python-interface-to-workflows\",\n",
63+
" },\n",
64+
" volumes=EmptyDirVolume(name=\"output-dir\", mount_path=\"/output-dir\"),\n",
65+
") as w:\n",
66+
" with Steps(name=\"divide\"):\n",
67+
" do_division(name=\"first\", arguments={\"a\": 2, \"b\": 5})\n",
68+
"\n"
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": null,
74+
"id": "04708f46",
75+
"metadata": {},
76+
"outputs": [],
77+
"source": [
78+
"with open(\"division_from_jupyter.yaml\", \"w\") as div:\n",
79+
" div.write(w.to_yaml()) # pyright: ignore[reportUnknownMemberType]"
80+
]
81+
},
82+
{
83+
"cell_type": "code",
84+
"execution_count": null,
85+
"id": "7e9f88cb",
86+
"metadata": {},
87+
"outputs": [
88+
{
89+
"name": "stdout",
90+
"output_type": "stream",
91+
"text": [
92+
"expiry_str: '1784802224' 1784801151.7235122\n"
93+
]
94+
},
95+
{
96+
"ename": "ValueError",
97+
"evalue": "invalid literal for int() with base 10: \"'1784802224'\"",
98+
"output_type": "error",
99+
"traceback": [
100+
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
101+
"\u001b[31mValueError\u001b[39m Traceback (most recent call last)",
102+
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[3]\u001b[39m\u001b[32m, line 3\u001b[39m\n\u001b[32m 1\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m python_interface_to_workflows.submit_to_argo \u001b[38;5;28;01mimport\u001b[39;00m submit_workflow_to_argo\n\u001b[32m 2\u001b[39m \n\u001b[32m----> \u001b[39m\u001b[32m3\u001b[39m submit_workflow_to_argo(w)\n",
103+
"\u001b[36mFile \u001b[39m\u001b[32m/workspaces/python-interface-to-workflows/src/python_interface_to_workflows/submit_to_argo.py:13\u001b[39m, in \u001b[36msubmit_workflow_to_argo\u001b[39m\u001b[34m(w)\u001b[39m\n\u001b[32m 12\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34msubmit_workflow_to_argo\u001b[39m(w: Workflow):\n\u001b[32m---> \u001b[39m\u001b[32m13\u001b[39m \u001b[30;43mset_token_env_variable\u001b[39;49m\u001b[30;43m(\u001b[39;49m\u001b[30;43mstaging\u001b[39;49m\u001b[30;43m=\u001b[39;49m\u001b[30;43;01mTrue\u001b[39;49;00m\u001b[30;43m)\u001b[39;49m\n\u001b[32m 14\u001b[39m dotenv.load_dotenv(dotenv_path=\u001b[33m\"\u001b[39m\u001b[33msrc/.env\u001b[39m\u001b[33m\"\u001b[39m, override=\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[32m 15\u001b[39m w.namespace = os.environ.get(\u001b[33m\"\u001b[39m\u001b[33mNAMESPACE\u001b[39m\u001b[33m\"\u001b[39m)\n",
104+
"\u001b[36mFile \u001b[39m\u001b[32m/workspaces/python-interface-to-workflows/src/python_interface_to_workflows/auth/keycloak_checker.py:39\u001b[39m, in \u001b[36mset_token_env_variable\u001b[39m\u001b[34m(staging)\u001b[39m\n\u001b[32m 31\u001b[39m code_challenge, code_challenge_method = generate_code_challenge(code_verifier)\n\u001b[32m 32\u001b[39m auth_url = keycloak_openid.auth_url(\n\u001b[32m 33\u001b[39m redirect_uri=\u001b[33mf\u001b[39m\u001b[33m\"\u001b[39m\u001b[33mhttp://localhost:\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mport\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m/\u001b[39m\u001b[33m\"\u001b[39m,\n\u001b[32m 34\u001b[39m scope=\u001b[33m\"\u001b[39m\u001b[33mopenid posix-uid profile email fedid\u001b[39m\u001b[33m\"\u001b[39m,\n\u001b[32m (...)\u001b[39m\u001b[32m 37\u001b[39m code_challenge_method=code_challenge_method,\n\u001b[32m 38\u001b[39m )\n\u001b[32m---> \u001b[39m\u001b[32m39\u001b[39m \u001b[38;5;28;01mmatch\u001b[39;00m \u001b[30;43mopen_auth_url\u001b[39;49m\u001b[30;43m(\u001b[39;49m\u001b[30;43mauth_url\u001b[39;49m\u001b[30;43m,\u001b[39;49m\u001b[30;43m \u001b[39;49m\u001b[30;43mport\u001b[39;49m\u001b[30;43m)\u001b[39;49m:\n\u001b[32m 40\u001b[39m \u001b[38;5;28;01mcase\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[32m 41\u001b[39m token: \u001b[38;5;28mdict\u001b[39m[\u001b[38;5;28mstr\u001b[39m, \u001b[38;5;28mstr\u001b[39m] = ( \u001b[38;5;66;03m# pyright: ignore[reportUnknownVariableType]\u001b[39;00m\n\u001b[32m 42\u001b[39m keycloak_openid.token( \u001b[38;5;66;03m# pyright: ignore[reportUnknownMemberType]\u001b[39;00m\n\u001b[32m 43\u001b[39m grant_type=\u001b[33m\"\u001b[39m\u001b[33mauthorization_code\u001b[39m\u001b[33m\"\u001b[39m,\n\u001b[32m (...)\u001b[39m\u001b[32m 47\u001b[39m )\n\u001b[32m 48\u001b[39m )\n",
105+
"\u001b[36mFile \u001b[39m\u001b[32m/workspaces/python-interface-to-workflows/src/python_interface_to_workflows/auth/open_auth_url.py:36\u001b[39m, in \u001b[36mopen_auth_url\u001b[39m\u001b[34m(auth_url, port)\u001b[39m\n\u001b[32m 34\u001b[39m expiry_str: \u001b[38;5;28mstr\u001b[39m = os.environ.get(\u001b[33m\"\u001b[39m\u001b[33mEXPIRY\u001b[39m\u001b[33m\"\u001b[39m) \u001b[38;5;66;03m# pyright: ignore\u001b[39;00m\n\u001b[32m 35\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33mf\u001b[39m\u001b[33m\"\u001b[39m\u001b[33mexpiry_str: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mexpiry_str\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m\"\u001b[39m, \u001b[38;5;28mstr\u001b[39m(time.time()))\n\u001b[32m---> \u001b[39m\u001b[32m36\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m (expiry_str == \u001b[33m\"\u001b[39m\u001b[33m\"\u001b[39m \u001b[38;5;129;01mor\u001b[39;00m \u001b[30;43mint\u001b[39;49m\u001b[30;43m(\u001b[39;49m\u001b[30;43mexpiry_str\u001b[39;49m\u001b[30;43m)\u001b[39;49m) <= \u001b[38;5;28mfloat\u001b[39m(time.time()):\n\u001b[32m 37\u001b[39m httpd = _ReusingHTTPServer((\u001b[33m\"\u001b[39m\u001b[33mlocalhost\u001b[39m\u001b[33m\"\u001b[39m, port), CallbackHandler)\n\u001b[32m 38\u001b[39m webbrowser.open(auth_url)\n",
106+
"\u001b[31mValueError\u001b[39m: invalid literal for int() with base 10: \"'1784802224'\""
107+
]
108+
}
109+
],
110+
"source": [
111+
"from python_interface_to_workflows.submit_to_argo import submit_workflow_to_argo\n",
112+
"\n",
113+
"submit_workflow_to_argo(w)"
114+
]
115+
}
116+
],
117+
"metadata": {
118+
"kernelspec": {
119+
"display_name": "python-interface-to-workflows (3.11.x)",
120+
"language": "python",
121+
"name": "python3"
122+
},
123+
"language_info": {
124+
"codemirror_mode": {
125+
"name": "ipython",
126+
"version": 3
127+
},
128+
"file_extension": ".py",
129+
"mimetype": "text/x-python",
130+
"name": "python",
131+
"nbconvert_exporter": "python",
132+
"pygments_lexer": "ipython3",
133+
"version": "3.11.15"
134+
}
135+
},
136+
"nbformat": 4,
137+
"nbformat_minor": 5
138+
}

0 commit comments

Comments
 (0)