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/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), 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..3fd1a9abc 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,126 @@ 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. Currently works only with a local ADR installation, and + not a docker image. + + 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 + ------- + bool + Success status of the PDF export: True if it worked, False otherwise + + 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 # pragma: no cover + 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(f"Can not export pdf report: {str(e)}") + 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 + ------- + bool + Success status of the HTML export: True if it worked, False otherwise + + 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(f"Can not export static HTML report: {str(e)}") + return success diff --git a/test_cleanup.py b/test_cleanup.py index 7155276ff..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/"] + [ + "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 +38,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/conftest.py b/tests/conftest.py index 37a4a4690..b6ba19dc8 100755 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -74,6 +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" + ) if use_local: ansys_installation = pytestconfig.getoption("install_path") else: @@ -83,6 +86,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 fcad71d38..1471a3e65 100755 --- a/tests/test_report.py +++ b/tests/test_report.py @@ -96,3 +96,32 @@ 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, request, get_exec) -> bool: + exec_basis = get_exec + if exec_basis: + success = False + 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 + + +@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_html(directory_name="htmltest_again") + except Exception: + success = False + adr_service_query.stop() + assert success is True