Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,16 @@
"redhat.vscode-yaml",
"ryanluker.vscode-coverage-gutters",
"charliermarsh.ruff",
"ms-azuretools.vscode-docker"
"ms-azuretools.vscode-docker",
"ms-python.debugpy",
"ms-python.vscode-python-envs",
"ms-python.vscode-pylance",
"ms-toolsai.vscode-jupyter-slideshow",
"ms-toolsai.vscode-jupyter-cell-tags",
"ms-toolsai.jupyter-renderers",
"ms-toolsai.jupyter",
"ms-azuretools.vscode-containers",
"ms-toolsai.jupyter-keymap"
]
}
},
Expand Down
20 changes: 20 additions & 0 deletions scripts/makecopiercorrect.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ cp ../templates/*example*.txt "../../copier_template/src/{{ project_name }}/temp

for file in "../../copier_template/src/{{ project_name }}/workflow_definitions"/*
do
[[ -d "$file" ]] && continue
[[ $file == *.jinja ]] && continue

sed -i 's/python-interface-to-workflows/{{repo_name}}/g' "$file"
Expand All @@ -21,9 +22,28 @@ do

mv "$file" "$file.jinja"
done
for file in "../../copier_template/src/{{ project_name }}/workflow_definitions/notebooks"/*
do
[[ -d "$file" ]] && continue
[[ $file == *.jinja ]] && continue

sed -i 's/python-interface-to-workflows/{{repo_name}}/g' "$file"
sed -i 's/DiamondLightSource/{{github_org}}/g' "$file"

sed -i '1i{% raw %}' "$file"
echo '{% endraw %}' >> "$file"

sed -i \
-e 's/{{repo_name}}/{% endraw %}{{repo_name}}{% raw %}/g' \
-e 's/{{github_org}}/{% endraw %}{{github_org}}{% raw %}/g' \
"$file"


mv "$file" "$file.jinja"
done
for file in "../../copier_template/src/{{ project_name }}/templates"/*
do
[[ -d "$file" ]] && continue
[[ $file == *.jinja ]] && continue

sed -i 's/python-interface-to-workflows/{{repo_name}}/g' "$file"
Expand Down
2 changes: 1 addition & 1 deletion scripts/runthefiles.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
cd src/python_interface_to_workflows/workflow_definitions
for file in *
do
[[ -d "$file" ]] && continue
uv run "$file"
done
mv *.yaml ../templates/
mv *.txt ../templates/
git add -u ../templates/
11 changes: 10 additions & 1 deletion src/copier_template/.devcontainer/devcontainer.json
Comment thread
TBThomas56 marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,16 @@
"redhat.vscode-yaml",
"ryanluker.vscode-coverage-gutters",
"charliermarsh.ruff",
"ms-azuretools.vscode-docker"
"ms-azuretools.vscode-docker",
"ms-python.debugpy",
"ms-python.vscode-python-envs",
"ms-python.vscode-pylance",
"ms-toolsai.vscode-jupyter-slideshow",
"ms-toolsai.vscode-jupyter-cell-tags",
"ms-toolsai.jupyter-renderers",
"ms-toolsai.jupyter",
"ms-azuretools.vscode-containers",
"ms-toolsai.jupyter-keymap"
]
}
},
Expand Down
6 changes: 3 additions & 3 deletions src/copier_template/scripts/runthefiles.sh.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
cd src/{{project_name}}/workflow_definitions
for file in *
do
[[ -d "$file" ]] && continue
uv run "$file"
done
mv *.yaml ../templates/
mv *.txt ../templates/
git add -u ../templates/
mv *.txt src/{{project_name}}/templates/
git add -u src/{{project_name}}/templates/
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ from {% endraw %}{{project_name}}{% raw %}.auth.keycloak_checker import set_toke
def submit_workflow(w: Workflow):
yamlstr = w.to_yaml() # pyright:ignore
dotenv.load_dotenv(dotenv_path="src/.env", override=True)
token: str = set_token_env_variable(True)
token: str = set_token_env_variable()
host: str = os.environ.get("HOST") # pyright:ignore
visit: str = os.environ.get("VISIT") # pyright:ignore

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
{% raw %}{
"cells": [
{
"cell_type": "markdown",
"id": "1e3146f1",
"metadata": {},
"source": [
"# Running in VSCode:\n",
"\n",
"1. Set kernal to {% endraw %}{{repo_name}}{% raw %} 3.11.x\n",
"2. Hit F1, run Jupyter: Import Notebook to Script\n",
"3. Click notebook_example.ipynb\n",
"4. Run the cells sequentially"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "494174ef",
"metadata": {},
"outputs": [],
"source": [
"from hera.workflows import (\n",
" DAG,\n",
" Artifact,\n",
" Parameter,\n",
" Volume,\n",
" Workflow,\n",
" script, # pyright: ignore[reportUnknownVariableType]\n",
")\n",
"from hera.workflows import models as m\n",
"from hera.workflows.archive import NoneArchiveStrategy\n",
"\n",
"\n",
"@script(\n",
" command=[\"python\"],\n",
" volume_mounts=[m.VolumeMount(name=\"tmpdir\", mount_path=\"/tmp\")],\n",
")\n",
"def install_dependencies():\n",
" import subprocess\n",
"\n",
" print(\"creating venv\")\n",
"\n",
" subprocess.check_call([\"python\", \"-m\", \"venv\", \"/tmp/venv\"])\n",
" subprocess.check_call(\n",
" [\"/tmp/venv/bin/pip\", \"install\", \"pillow\", \"h5py\", \"numpy\", \"hera\"]\n",
" )\n",
"\n",
"\n",
"@script(\n",
" command=[\"python\"],\n",
" outputs=Parameter(\n",
" name=\"out-parameters\", value_from=m.ValueFrom(path=\"/tmp/parameters.json\")\n",
" ),\n",
" volume_mounts=[m.VolumeMount(name=\"tmpdir\", mount_path=\"/tmp\")],\n",
")\n",
"def generate_parameters(\n",
" png: str,\n",
" jpg: str,\n",
" jpeg: str,\n",
" tif: str,\n",
" tiff: str,\n",
"):\n",
" import json\n",
"\n",
" params: list[dict[str, int | list[int] | str] | None] = [\n",
" {\"width\": 500, \"height\": 500, \"weights\": [255, 1, 100], \"extension\": \"png\"}\n",
" if png.lower() == \"true\"\n",
" else None,\n",
" {\"width\": 600, \"height\": 200, \"weights\": [100, 150, 100], \"extension\": \"jpg\"}\n",
" if jpg.lower() == \"true\"\n",
" else None,\n",
" {\"width\": 300, \"height\": 400, \"weights\": [100, 150, 100], \"extension\": \"jpeg\"}\n",
" if jpeg.lower() == \"true\"\n",
" else None,\n",
" {\"width\": 300, \"height\": 200, \"weights\": [230, 100, 1], \"extension\": \"tif\"}\n",
" if tif.lower() == \"true\"\n",
" else None,\n",
" {\"width\": 200, \"height\": 300, \"weights\": [230, 100, 1], \"extension\": \"tiff\"}\n",
" if tiff.lower() == \"true\"\n",
" else None,\n",
" ]\n",
" params_to_write: list[dict[str, int | list[int] | str]] = [\n",
" image_params for image_params in params if image_params is not None\n",
" ]\n",
" with open(\"/tmp/parameters.json\", \"w\") as f:\n",
" json.dump(params_to_write, f)\n",
"\n",
"\n",
"@script(\n",
" command=[\"/tmp/venv/bin/python\"],\n",
" volume_mounts=[m.VolumeMount(name=\"tmpdir\", mount_path=\"/tmp\")],\n",
" outputs=[\n",
" Parameter(\n",
" name=\"out-paths\",\n",
" value_from=m.ValueFrom(\n",
" path=\"/tmp/{{inputs.parameters.extension}}-path.json\"\n",
" ),\n",
" ),\n",
" Artifact(\n",
" name=\"{{inputs.parameters.extension}}-image\",\n",
" path=\"/tmp/{{inputs.parameters.extension}}-image.{{inputs.parameters.extension}}\",\n",
" archive=NoneArchiveStrategy(),\n",
" ),\n",
" ],\n",
")\n",
"def create_image(\n",
" width: int, height: int, weights: tuple[int, int, int], extension: str\n",
"):\n",
" import json\n",
"\n",
" from PIL import Image\n",
"\n",
" def create_pattern(\n",
" width: int,\n",
" height: int,\n",
" weights: tuple[int, int, int],\n",
" ) -> Image.Image:\n",
" print(f\"width: {width}\")\n",
" print(f\"height: {height}\")\n",
" print(f\"RBG weights: {weights}\")\n",
" image = Image.new(\"RGB\", (width, height))\n",
" pixels = image.load()\n",
" for i in range(width):\n",
" for j in range(height):\n",
" pixels[i, j] = ( # pyright: ignore[reportOptionalSubscript]\n",
" (i + j * 50) % weights[0],\n",
" weights[1],\n",
" (i * 300 + j) % weights[2],\n",
" )\n",
" return image\n",
"\n",
" image = create_pattern(width, height, weights)\n",
" path = f\"/tmp/{extension}-image.{extension}\"\n",
" image.save(path)\n",
" with open(f\"/tmp/{extension}-path.json\", \"w\") as f:\n",
" json.dump(path, f)\n",
"\n",
"\n",
"@script(\n",
" command=[\"/tmp/venv/bin/python\"],\n",
" volume_mounts=[m.VolumeMount(name=\"tmpdir\", mount_path=\"/tmp\")],\n",
" outputs=Artifact(\n",
" name=\"hdf5output\",\n",
" path=\"/tmp/images.hdf5\",\n",
" archive=NoneArchiveStrategy(),\n",
" ),\n",
")\n",
"def to_hdf5(paths: str):\n",
"\n",
" import h5py # pyright: ignore[reportMissingTypeStubs]\n",
" import numpy as np\n",
" from PIL import Image\n",
"\n",
" print(\"creating hdf5 file\")\n",
" with h5py.File(\"/tmp/images.hdf5\", \"w\") as f:\n",
" for i, path in enumerate(paths):\n",
" path = path.strip('\"')\n",
" print(f\"Got {path}\")\n",
" with Image.open(path) as image:\n",
" arr = np.array(image)\n",
" f.create_dataset( # pyright: ignore[reportUnknownMemberType]\n",
" f\"image_{i}\", data=arr, dtype=arr.dtype\n",
" )\n",
" print(\"done\")\n",
"\n",
"\n",
"with Workflow(\n",
" generate_name=\"hera-example-\", # when running on graphql this should be name\n",
" entrypoint=\"workflowentry\",\n",
" api_version=\"argoproj.io/v1alpha1\",\n",
" kind=\"Workflow\", # ClusterWorkflowTemplate\", when on graphql\n",
" labels={\"workflows.diamond.ac.uk/science-group-examples\": \"true\"},\n",
" annotations={\n",
" \"workflows.argoproj.io/title\": \"example remade via hera\",\n",
" \"workflows.argoproj.io/description\": \"\"\"Replicates the functionality of\n",
"example.yaml\"\"\",\n",
" \"workflows.diamond.ac.uk/repository\": \"https://github.com/{% endraw %}{{github_org}}{% raw %}/{% endraw %}{{repo_name}}{% raw %}\",\n",
" },\n",
" volumes=Volume(name=\"tmpdir\", mount_path=\"/tmp/\", size=\"1Gi\"),\n",
") as w:\n",
" with DAG(name=\"workflowentry\"):\n",
" install = install_dependencies(name=\"install\")\n",
" params = generate_parameters(\n",
" name=\"params\",\n",
" arguments={\n",
" \"png\": \"True\",\n",
" \"jpg\": \"True\",\n",
" \"jpeg\": \"True\",\n",
" \"tif\": \"True\",\n",
" \"tiff\": \"True\",\n",
" },\n",
" )\n",
" makeimages = create_image(with_param=params.get_parameter(\"out-parameters\"))\n",
" makehdf5 = to_hdf5(\n",
" arguments={\n",
" \"paths\": makeimages.get_parameter(\"out-paths\"),\n",
" }\n",
" )\n",
" [install, params] >> makeimages >> makehdf5 # pyright: ignore\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "04708f46",
"metadata": {},
"outputs": [],
"source": [
"\n",
"with open(\"example.txt\", \"w\") as div:\n",
" div.write(w.to_yaml()) # pyright: ignore[reportUnknownMemberType]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7e9f88cb",
"metadata": {},
"source": [
"from {% endraw %}{{project_name}}{% raw %}.submit_workflow import submit_workflow\n",
"\n",
"submit_workflow(w)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "{% endraw %}{{repo_name}}{% raw %} (3.11.x)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.15"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
{% endraw %}
2 changes: 1 addition & 1 deletion src/copier_template/tests/test_keycloak_checker.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_set_token_env_variable(
keycloak.token.return_value = token
keycloak.refresh_token.return_value = token
keycloak.decode_token.return_value = {"exp": 123456789}
assert set_token_env_variable(staging) == "fake_token"
assert set_token_env_variable() == "fake_token"

mock_token_expired.assert_called_once_with("https://mock.site", port)
if return_present:
Expand Down
2 changes: 1 addition & 1 deletion src/copier_template/tests/test_submit_to_graphql.py.jinja
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from unittest.mock import MagicMock, patch
from unittest.mock import MagicMock, call, patch

from {{project_name}}.submit_workflow import submit_workflow

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ def do_division(a: int, b: int):
do_division(name="first", arguments={"a": 2, "b": 5})


with open("divisionyaml.yaml", "w") as div:
with open("divisionyaml.txt", "w") as div:
div.write(w.to_yaml()) # pyright: ignore[reportUnknownMemberType]
Loading
Loading