From 9677c75ef8a1145ecf11bfd6d3f603946ad6a290 Mon Sep 17 00:00:00 2001 From: Hamish Brown Date: Thu, 6 Jun 2024 09:19:46 +1200 Subject: [PATCH] Pair report.py with note book and give environment specific root variables so can test report generation locally --- TestGraphs/report.ipynb | 88 +++++++++++++++++++++++++++++++++++++++++ TestGraphs/report.py | 46 ++++++++++++++++++--- 2 files changed, 128 insertions(+), 6 deletions(-) create mode 100644 TestGraphs/report.ipynb diff --git a/TestGraphs/report.ipynb b/TestGraphs/report.ipynb new file mode 100644 index 0000000..ac21b68 --- /dev/null +++ b/TestGraphs/report.ipynb @@ -0,0 +1,88 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 6, + "id": "c6317285-bf98-489f-a858-44925c83f9c3", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "import os\n", + "import aspose.words as aw\n", + "import os.path as osp\n", + "from glob import glob\n", + "from pathlib import Path\n", + "\n", + "def main():\n", + " try: \n", + " if os.environ[\"GITHUB_WORKSPACE\"] != None:\n", + " root = os.environ[\"GITHUB_WORKSPACE\"]\n", + " inPath = os.path.join(root, \"TestGraphs\", \"Outputs\")\n", + " \n", + " except: \n", + " rootfrags = os.path.abspath('WS2.py').split(\"\\\\\")\n", + " root = \"\"\n", + " for d in rootfrags:\n", + " if d == \"FieldNBalance\":\n", + " break\n", + " else:\n", + " root += d + \"\\\\\"\n", + " inPath = os.path.join(root,\"FieldNBalance\",\"TestGraphs\", \"Outputs\") \n", + " \n", + " if not osp.isdir(inPath):\n", + " raise FileNotFoundError(f\"Directory does not exist: {inPath}\")\n", + " \n", + " imgs = glob(osp.join(inPath, \"*.png\"))\n", + " if len(imgs) == 0:\n", + " raise FileNotFoundError(f\"No images found in directory: {inPath}\")\n", + " \n", + " doc = aw.Document()\n", + " builder = aw.DocumentBuilder(doc)\n", + "\n", + " for img in imgs:\n", + " builder.insert_image(img)\n", + "\n", + " outdir = \"html\"\n", + " Path(outdir).mkdir(parents=True, exist_ok=True) \n", + " doc.save(osp.join(outdir, \"index.html\"))\n", + "\n", + "if __name__ == \"__main__\":\n", + " main()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f3ac9d83-1a3d-4c2f-b329-0e91215fc21f", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "jupytext": { + "formats": "ipynb,py:light" + }, + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.10.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/TestGraphs/report.py b/TestGraphs/report.py index 2e71649..37aa180 100644 --- a/TestGraphs/report.py +++ b/TestGraphs/report.py @@ -1,16 +1,47 @@ +# --- +# jupyter: +# jupytext: +# formats: ipynb,py:light +# text_representation: +# extension: .py +# format_name: light +# format_version: '1.5' +# jupytext_version: 1.15.0 +# kernelspec: +# display_name: Python 3 (ipykernel) +# language: python +# name: python3 +# --- + +# + +import os import aspose.words as aw import os.path as osp from glob import glob from pathlib import Path def main(): - img_path = osp.join("TestGraphs", "Outputs") - if not osp.isdir(img_path): - raise FileNotFoundError(f"Directory does not exist: {img_path}") + try: + if os.environ["GITHUB_WORKSPACE"] != None: + root = os.environ["GITHUB_WORKSPACE"] + inPath = os.path.join(root, "TestGraphs", "Outputs") + + except: + rootfrags = os.path.abspath('WS2.py').split("\\") + root = "" + for d in rootfrags: + if d == "FieldNBalance": + break + else: + root += d + "\\" + inPath = os.path.join(root,"FieldNBalance","TestGraphs", "Outputs") + + if not osp.isdir(inPath): + raise FileNotFoundError(f"Directory does not exist: {inPath}") - imgs = glob(osp.join(img_path, "*.png")) + imgs = glob(osp.join(inPath, "*.png")) if len(imgs) == 0: - raise FileNotFoundError(f"No images found in directory: {img_path}") + raise FileNotFoundError(f"No images found in directory: {inPath}") doc = aw.Document() builder = aw.DocumentBuilder(doc) @@ -23,4 +54,7 @@ def main(): doc.save(osp.join(outdir, "index.html")) if __name__ == "__main__": - main() \ No newline at end of file + main() +# - + +