From 436fdaaddd31ecc6b6fc8a070fac3f99ade8f4b7 Mon Sep 17 00:00:00 2001 From: Marina Galvagni Date: Mon, 27 Nov 2023 11:08:54 -0500 Subject: [PATCH 1/7] Add wrapper for export in PDF and HTML for reports --- pyproject.toml | 2 +- src/ansys/dynamicreporting/core/adr_report.py | 123 +++++++++++++++++- tests/test_report.py | 24 ++++ 3 files changed, 147 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 44496578b..fec07e76a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,7 +8,7 @@ requires = [ [project] name = "ansys-dynamicreporting-core" -version = "0.6.0.dev0" +version = "0.6.1.dev0" authors = [ {name = "ANSYS, Inc.", email = "pyansys.core@ansys.com"}, ] diff --git a/src/ansys/dynamicreporting/core/adr_report.py b/src/ansys/dynamicreporting/core/adr_report.py index 95fa71218..03c64afdc 100644 --- a/src/ansys/dynamicreporting/core/adr_report.py +++ b/src/ansys/dynamicreporting/core/adr_report.py @@ -18,6 +18,7 @@ my_report.visualize() """ import sys +from typing import Optional import webbrowser from ansys.dynamicreporting.core.adr_utils import in_ipynb @@ -182,7 +183,7 @@ def get_iframe(self, width: int = 1000, height: int = 800): import ansys.dynamicreporting.core as adr adr_service = adr.Service(ansys_installation = r'C:\\Program Files\\ANSYS Inc\\v232') ret = adr_service.connect() - my_report = adr_service.get_report(report_name = "My Top Report" + my_report = adr_service.get_report(report_name = "My Top Report") report_iframe = my_report.get_iframe() """ if "IPython.display" in sys.modules: @@ -191,3 +192,123 @@ def get_iframe(self, width: int = 1000, height: int = 800): else: iframe = None return iframe + + def export_pdf( + self, + file_name: str = "", + query: Optional[dict] = None, + page: Optional[list] = None, + delay: Optional[int] = None, + ) -> bool: + """ + Export report as PDF. + + Parameters + ---------- + file_name : str + Path and filename for the PDF file to export. + query : dict, optional + Dictionary for query parameters to apply to report template before export. Default: None + page: list, optional + List of integers that represents the size of the exported pdf. Default: None, which + corresponds to A4 size + delay: int, optional + Seconds to delay the start of the pdf export operation. Default: None, which + corresponds to no delay + Returns + ------- + success + Pdf file for the report has been exported + + Examples + -------- + :: + + import ansys.dynamicreporting.core as adr + adr_service = adr.Service(ansys_installation = r'C:\\Program Files\\ANSYS Inc\\v232') + ret = adr_service.connect() + my_report = adr_service.get_report(report_name = "My Top Report") + succ = my_report.export_pdf(file_name=r'D:\\tmp\\myreport.pdf') + """ + success = False + if self.service is None: # pragma: no cover + self.service.logger.error("No connection to any report") + return "" + if self.service.serverobj is None: # pragma: no cover + self.service.logger.error("No connection to any server") + return "" + try: + if query is None: + query = {} + self.service.serverobj.export_report_as_pdf( + report_guid=self.report.guid, + file_name=file_name, + query=query, + page=page, + parent=None, + delay=delay, + exec_basis=self.service._ansys_installation, + ansys_version=self.service._ansys_version, + ) + success = True + except Exception as e: # pragma: no cover + self.service.logger.error("Can not export pdf report") + return success + + def export_html( + self, + directory_name: str = "", + query: Optional[dict] = None, + filename: Optional[str] = "index.html", + no_inline_files: Optional[bool] = False, + ) -> bool: + """ + Export report as static HTML. + + Parameters + ---------- + directory_name : str + .... + query : dict, optional + Dictionary for query parameters to apply to report template before export. Default: None + filename : str, optional + Filename for the exported static HTML file. Default: index.html + no_inline_files: bool, optional + If True, the information is exported as stand alone files instead of in line content + in the static HTML. Default: False + Returns + ------- + success + HTML file for the report has been exported + + Examples + -------- + :: + + import ansys.dynamicreporting.core as adr + adr_service = adr.Service(ansys_installation = r'C:\\Program Files\\ANSYS Inc\\v232') + ret = adr_service.connect() + my_report = adr_service.get_report(report_name = "My Top Report") + succ = my_report.export_html(directory_name = r'D:\\tmp') + """ + success = False + if self.service is None: # pragma: no cover + self.service.logger.error("No connection to any report") + return "" + if self.service.serverobj is None: # pragma: no cover + self.service.logger.error("No connection to any server") + return "" + try: + if query is None: + query = {} + self.service.serverobj.export_report_as_html( + report_guid=self.report.guid, + directory_name=directory_name, + query=query, + filename=filename, + no_inline_files=no_inline_files, + ) + success = True + except Exception as e: # pragma: no cover + self.service.logger.error("Can not export static HTML report") + return success diff --git a/tests/test_report.py b/tests/test_report.py index fcad71d38..c37d6e126 100755 --- a/tests/test_report.py +++ b/tests/test_report.py @@ -96,3 +96,27 @@ def test_unit_no_url(request) -> bool: if "No connection to any server" in line: err_msg = True assert err_msg + + +@pytest.mark.ado_test +def test_save_as_pdf(adr_service_query) -> bool: + success = False + try: + my_report = adr_service_query.get_report(report_name="My Top Report") + success = my_report.export_pdf(file_name="again_mytest") + except Exception: + success = False + adr_service_query.stop() + assert success is True + + +@pytest.mark.ado_test +def test_save_as_html(adr_service_query) -> bool: + success = False + try: + my_report = adr_service_query.get_report(report_name="My Top Report") + success = my_report.export_pdf(directory_name="htmltest_again") + except Exception: + success = False + adr_service_query.stop() + assert success is True From a366f5a96cdfd0670b5d481fec630eb45983b984 Mon Sep 17 00:00:00 2001 From: Marina Galvagni Date: Mon, 27 Nov 2023 11:59:51 -0500 Subject: [PATCH 2/7] Fix test --- test_cleanup.py | 3 ++- tests/test_report.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/test_cleanup.py b/test_cleanup.py index 7155276ff..613512f8e 100644 --- a/test_cleanup.py +++ b/test_cleanup.py @@ -10,7 +10,7 @@ "tests/test_data/viewer_test/", ] dir_list.extend( - ["tests/test_data/ansys", "tests/test_data/media", "tests/test_data/webfonts/", "htmltest/"] + ["tests/test_data/ansys", "tests/test_data/media", "tests/test_data/webfonts/", "htmltest/", "htmltest_again/"] ) dir_list.append("tests/test_data/create_delete/") dir_list.append("tests/test_data/create_twice/") @@ -32,6 +32,7 @@ file_list.extend(glob.glob("tests/outfile*.txt")) file_list.append("mypresentation") file_list.append("mytest.pdf") +file_list.append("again_mytest") for i_file in file_list: try: os.remove(i_file) diff --git a/tests/test_report.py b/tests/test_report.py index c37d6e126..09c32951f 100755 --- a/tests/test_report.py +++ b/tests/test_report.py @@ -115,7 +115,7 @@ def test_save_as_html(adr_service_query) -> bool: success = False try: my_report = adr_service_query.get_report(report_name="My Top Report") - success = my_report.export_pdf(directory_name="htmltest_again") + success = my_report.export_html(directory_name="htmltest_again") except Exception: success = False adr_service_query.stop() From e31b4fb26aa50d6bd04204620c06883694acd731 Mon Sep 17 00:00:00 2001 From: Marina Galvagni Date: Mon, 27 Nov 2023 14:19:36 -0500 Subject: [PATCH 3/7] Remove pdf test for docker case --- src/ansys/dynamicreporting/core/adr_report.py | 7 ++++--- tests/conftest.py | 2 ++ tests/test_report.py | 19 ++++++++++++------- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/ansys/dynamicreporting/core/adr_report.py b/src/ansys/dynamicreporting/core/adr_report.py index 03c64afdc..98e3f20b0 100644 --- a/src/ansys/dynamicreporting/core/adr_report.py +++ b/src/ansys/dynamicreporting/core/adr_report.py @@ -201,7 +201,8 @@ def export_pdf( delay: Optional[int] = None, ) -> bool: """ - Export report as PDF. + Export report as PDF. Currently works only with a local ADR installation, and not + a docker image. Parameters ---------- @@ -252,7 +253,7 @@ def export_pdf( ) success = True except Exception as e: # pragma: no cover - self.service.logger.error("Can not export pdf report") + self.service.logger.error(f"Can not export pdf report: {str(e)}") return success def export_html( @@ -310,5 +311,5 @@ def export_html( ) success = True except Exception as e: # pragma: no cover - self.service.logger.error("Can not export static HTML report") + self.service.logger.error(f"Can not export static HTML report: {str(e)}") return success diff --git a/tests/conftest.py b/tests/conftest.py index 37a4a4690..c9fa166da 100755 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -74,6 +74,7 @@ def adr_service_query(request, pytestconfig: pytest.Config) -> Service: use_local = pytestconfig.getoption("use_local_launcher") local_db = os.path.join("test_data", "query_db") db_dir = os.path.join(request.fspath.dirname, local_db) + tmp_docker_dir = os.path.join(os.path.join(request.fspath.dirname, "test_data"), "tmp_docker_query") if use_local: ansys_installation = pytestconfig.getoption("install_path") else: @@ -83,6 +84,7 @@ def adr_service_query(request, pytestconfig: pytest.Config) -> Service: ansys_installation=ansys_installation, docker_image=DOCKER_DEV_REPO_URL, db_directory=db_dir, + data_directory=tmp_docker_dir, port=8000 + int(random() * 4000), ) tmp_service.start(create_db=False, exit_on_close=True, delete_db=False) diff --git a/tests/test_report.py b/tests/test_report.py index 09c32951f..1989271d9 100755 --- a/tests/test_report.py +++ b/tests/test_report.py @@ -99,14 +99,19 @@ def test_unit_no_url(request) -> bool: @pytest.mark.ado_test -def test_save_as_pdf(adr_service_query) -> bool: - success = False - try: - my_report = adr_service_query.get_report(report_name="My Top Report") - success = my_report.export_pdf(file_name="again_mytest") - except Exception: +def test_save_as_pdf(adr_service_query, request, get_exec) -> bool: + exec_basis = get_exec + if exec_basis: success = False - adr_service_query.stop() + try: + my_report = adr_service_query.get_report(report_name="My Top Report") + pdf_file = os.path.join(request.fspath.dirname, "again_mytest") + success = my_report.export_pdf(file_name=pdf_file) + except Exception: + success = False + adr_service_query.stop() + else: # If no local installation, then skip this test + success = True assert success is True From 34c292e30b13975cac32593ceb85f3151bda0e58 Mon Sep 17 00:00:00 2001 From: Marina Galvagni Date: Mon, 27 Nov 2023 15:38:03 -0500 Subject: [PATCH 4/7] Syntax changes --- src/ansys/dynamicreporting/core/adr_report.py | 4 ++-- test_cleanup.py | 8 +++++++- tests/conftest.py | 4 +++- tests/test_report.py | 2 +- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/ansys/dynamicreporting/core/adr_report.py b/src/ansys/dynamicreporting/core/adr_report.py index 98e3f20b0..17dd98f86 100644 --- a/src/ansys/dynamicreporting/core/adr_report.py +++ b/src/ansys/dynamicreporting/core/adr_report.py @@ -201,8 +201,8 @@ def export_pdf( delay: Optional[int] = None, ) -> bool: """ - Export report as PDF. Currently works only with a local ADR installation, and not - a docker image. + Export report as PDF. Currently works only with a local ADR installation, and + not a docker image. Parameters ---------- diff --git a/test_cleanup.py b/test_cleanup.py index 613512f8e..a9c467a2a 100644 --- a/test_cleanup.py +++ b/test_cleanup.py @@ -10,7 +10,13 @@ "tests/test_data/viewer_test/", ] dir_list.extend( - ["tests/test_data/ansys", "tests/test_data/media", "tests/test_data/webfonts/", "htmltest/", "htmltest_again/"] + [ + "tests/test_data/ansys", + "tests/test_data/media", + "tests/test_data/webfonts/", + "htmltest/", + "htmltest_again/", + ] ) dir_list.append("tests/test_data/create_delete/") dir_list.append("tests/test_data/create_twice/") diff --git a/tests/conftest.py b/tests/conftest.py index c9fa166da..b6ba19dc8 100755 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -74,7 +74,9 @@ def adr_service_query(request, pytestconfig: pytest.Config) -> Service: use_local = pytestconfig.getoption("use_local_launcher") local_db = os.path.join("test_data", "query_db") db_dir = os.path.join(request.fspath.dirname, local_db) - tmp_docker_dir = os.path.join(os.path.join(request.fspath.dirname, "test_data"), "tmp_docker_query") + tmp_docker_dir = os.path.join( + os.path.join(request.fspath.dirname, "test_data"), "tmp_docker_query" + ) if use_local: ansys_installation = pytestconfig.getoption("install_path") else: diff --git a/tests/test_report.py b/tests/test_report.py index 1989271d9..1471a3e65 100755 --- a/tests/test_report.py +++ b/tests/test_report.py @@ -110,7 +110,7 @@ def test_save_as_pdf(adr_service_query, request, get_exec) -> bool: except Exception: success = False adr_service_query.stop() - else: # If no local installation, then skip this test + else: # If no local installation, then skip this test success = True assert success is True From ed169892433dafb0e2a06026343ee18bfb602080 Mon Sep 17 00:00:00 2001 From: Marina Galvagni Date: Mon, 27 Nov 2023 17:21:51 -0500 Subject: [PATCH 5/7] Fix documentation build number --- doc/source/conf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/conf.py b/doc/source/conf.py index 96c143ef3..e64507793 100755 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -74,7 +74,7 @@ # Intersphinx mapping intersphinx_mapping = { - "python": ("https://docs.python.org/3", None), + "python": ("https://docs.python.org/3.11", None), # kept here as an example # "scipy": ("https://docs.scipy.org/doc/scipy/reference", None), # "numpy": ("https://numpy.org/devdocs", None), From e8bf5f7f0cc81670c987c30a1053ba72e8faed8d Mon Sep 17 00:00:00 2001 From: Marina Galvagni Date: Tue, 28 Nov 2023 10:19:27 -0500 Subject: [PATCH 6/7] Improve doc strings --- src/ansys/dynamicreporting/core/adr_report.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/ansys/dynamicreporting/core/adr_report.py b/src/ansys/dynamicreporting/core/adr_report.py index 17dd98f86..0a296d1d2 100644 --- a/src/ansys/dynamicreporting/core/adr_report.py +++ b/src/ansys/dynamicreporting/core/adr_report.py @@ -210,16 +210,17 @@ def export_pdf( Path and filename for the PDF file to export. query : dict, optional Dictionary for query parameters to apply to report template before export. Default: None - page: list, optional + page : list, optional List of integers that represents the size of the exported pdf. Default: None, which corresponds to A4 size - delay: int, optional + delay : int, optional Seconds to delay the start of the pdf export operation. Default: None, which corresponds to no delay + Returns ------- - success - Pdf file for the report has been exported + bool + Success status of the PDF export: True if it worked, False otherwise Examples -------- @@ -274,13 +275,14 @@ def export_html( Dictionary for query parameters to apply to report template before export. Default: None filename : str, optional Filename for the exported static HTML file. Default: index.html - no_inline_files: bool, optional + no_inline_files : bool, optional If True, the information is exported as stand alone files instead of in line content in the static HTML. Default: False + Returns ------- - success - HTML file for the report has been exported + bool + Success status of the HTML export: True if it worked, False otherwise Examples -------- From 430aa3a7698b622f6698d60df344e499da22a6f9 Mon Sep 17 00:00:00 2001 From: Marina Galvagni Date: Tue, 28 Nov 2023 10:53:29 -0500 Subject: [PATCH 7/7] Do not raise warnings to errors when building docs --- .github/workflows/ci_cd.yml | 1 + .github/workflows/nightly-docs.yml | 1 + src/ansys/dynamicreporting/core/adr_report.py | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci_cd.yml b/.github/workflows/ci_cd.yml index e31ce7897..e1c2e2d2d 100644 --- a/.github/workflows/ci_cd.yml +++ b/.github/workflows/ci_cd.yml @@ -120,6 +120,7 @@ jobs: with: python-version: ${{ env.MAIN_PYTHON_VERSION }} check-links: false + sphinxopts: '-j auto' package: name: Package library diff --git a/.github/workflows/nightly-docs.yml b/.github/workflows/nightly-docs.yml index da613090f..1be7b3af5 100644 --- a/.github/workflows/nightly-docs.yml +++ b/.github/workflows/nightly-docs.yml @@ -23,6 +23,7 @@ jobs: with: python-version: ${{ env.MAIN_PYTHON_VERSION }} check-links: false + sphinxopts: '-j auto' docs_upload: needs: docs_build diff --git a/src/ansys/dynamicreporting/core/adr_report.py b/src/ansys/dynamicreporting/core/adr_report.py index 0a296d1d2..3fd1a9abc 100644 --- a/src/ansys/dynamicreporting/core/adr_report.py +++ b/src/ansys/dynamicreporting/core/adr_report.py @@ -232,7 +232,7 @@ def export_pdf( my_report = adr_service.get_report(report_name = "My Top Report") succ = my_report.export_pdf(file_name=r'D:\\tmp\\myreport.pdf') """ - success = False + success = False # pragma: no cover if self.service is None: # pragma: no cover self.service.logger.error("No connection to any report") return ""