Skip to content

Commit

Permalink
Feat/filter report exports (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
margalva authored Jan 11, 2025
1 parent 0e452c4 commit f7d7469
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 18 deletions.
40 changes: 26 additions & 14 deletions src/ansys/dynamicreporting/core/adr_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,8 @@ def get_iframe(self, width: int = 1000, height: int = 800, filter: str = ""):
def export_pdf(
self,
file_name: str = "",
query: Optional[dict] = None,
query_params: Optional[dict] = None,
item_filter: Optional[str] = None,
page: Optional[list] = None,
delay: Optional[int] = None,
) -> bool:
Expand All @@ -547,8 +548,11 @@ def export_pdf(
----------
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
query_params : dict, optional
Dictionary for parameters to apply to report template. Default: None
item_filter: str, optional
String corresponding to query to run on the database items before rendering the report.
Default: None
page : list, optional
List of integers that represents the size of the exported pdf. Default: None, which
corresponds to A4 size
Expand All @@ -569,7 +573,8 @@ def export_pdf(
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')
succ = my_report.export_pdf(file_name=r'D:\\tmp\\myreport.pdf', query = {"colormode": "dark"})
succ2 = my_report.export_pdf(filename=r'D:\\tmp\\onlyimages.pdf', item_filter = 'A|i_type|cont|image;')
"""
success = False # pragma: no cover
if self.service is None: # pragma: no cover
Expand All @@ -579,12 +584,13 @@ def export_pdf(
self.service.logger.error("No connection to any server")
return ""
try: # pragma: no cover
if query is None:
query = {}
if query_params is None:
query_params = {}
self.service.serverobj.export_report_as_pdf(
report_guid=self.report.guid,
file_name=file_name,
query=query,
query=query_params,
item_filter=item_filter,
page=page,
parent=None,
delay=delay,
Expand All @@ -599,7 +605,8 @@ def export_pdf(
def export_html(
self,
directory_name: str = "",
query: Optional[dict] = None,
query_params: Optional[dict] = None,
item_filter: Optional[str] = None,
filename: Optional[str] = "index.html",
no_inline_files: Optional[bool] = False,
) -> bool:
Expand All @@ -610,8 +617,11 @@ def export_html(
----------
directory_name : str
....
query : dict, optional
Dictionary for query parameters to apply to report template before export. Default: None
query_params : dict, optional
Dictionary for parameters to apply to report template. Default: None
item_filter: str, optional
String corresponding to query to run on the database items before rendering the report.
Default: None
filename : str, optional
Filename for the exported static HTML file. Default: index.html
no_inline_files : bool, optional
Expand All @@ -631,7 +641,8 @@ def export_html(
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')
succ = my_report.export_html(directory_name = r'D:\\tmp', query={"colormode": "dark"})
succ2 = my_report.export_html(filename=r'D:\\tmp\\onlyimages.pdf', item_filter = 'A|i_type|cont|image;')
"""
success = False
if self.service is None: # pragma: no cover
Expand All @@ -641,12 +652,13 @@ def export_html(
self.service.logger.error("No connection to any server")
return ""
try:
if query is None:
query = {}
if query_params is None:
query_params = {}
self.service.serverobj.export_report_as_html(
report_guid=self.report.guid,
directory_name=directory_name,
query=query,
query=query_params,
item_filter=item_filter,
filename=filename,
no_inline_files=no_inline_files,
ansys_version=self.service._ansys_version,
Expand Down
12 changes: 9 additions & 3 deletions src/ansys/dynamicreporting/core/utils/report_remote_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from urllib3.util.retry import Retry

from . import exceptions, filelock, report_objects, report_utils
from ..adr_utils import build_query_url
from .encoders import BaseEncoder


Expand Down Expand Up @@ -852,7 +853,7 @@ def _download_report(self, url, file_name, directory_name=None):
with open(file_path, "wb") as report:
report.write(resp.content)

def build_url_with_query(self, report_guid, query, rest_api=False):
def build_url_with_query(self, report_guid, query, item_filter=None, rest_api=False):
url = self.get_URL()
if rest_api:
url += f"/api/generate-report/?view={str(report_guid)}"
Expand All @@ -864,13 +865,17 @@ def build_url_with_query(self, report_guid, query, rest_api=False):
url += f"&{key}"
else:
url += f"&{key}={value}"
if item_filter:
query_str = build_query_url(filter=item_filter)
url += query_str
return url

def export_report_as_html(
self,
report_guid,
directory_name,
query=None,
item_filter=None,
filename="index.html",
no_inline_files=False,
ansys_version=None,
Expand All @@ -881,7 +886,7 @@ def export_report_as_html(
directory_path = os.path.abspath(directory_name)
from ansys.dynamicreporting.core.utils.report_download_html import ReportDownloadHTML

url = self.build_url_with_query(report_guid, query)
url = self.build_url_with_query(report_guid, query, item_filter)
# ask the server for the Ansys version number. It will generally know it.
_ansys_version = self.get_api_version().get("ansys_version", self._ansys_version)
if ansys_version:
Expand All @@ -901,6 +906,7 @@ def export_report_as_pdf(
report_guid,
file_name,
query=None,
item_filter=None,
page=None,
parent=None,
delay=None,
Expand All @@ -910,7 +916,7 @@ def export_report_as_pdf(
if query is None:
query = {}
query["print"] = "pdf"
url = self.build_url_with_query(report_guid, query)
url = self.build_url_with_query(report_guid, query, item_filter)
file_path = os.path.abspath(file_name)
if has_qt and (parent is not None):
from .report_download_pdf import NexusPDFSave
Expand Down
1 change: 1 addition & 0 deletions test_cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
file_list.append("mypresentation")
file_list.append("mytest.pdf")
file_list.append("again_mytest")
file_list.append("again_mytest_filter")
for i_file in file_list:
try:
os.remove(i_file)
Expand Down
Binary file modified tests/test_data/query_db/db.sqlite3
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/test_data/query_db/media/csf_conversion_version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
23.20R1
25.20R1
16 changes: 16 additions & 0 deletions tests/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,22 @@ def test_save_as_pdf(adr_service_query, request, get_exec) -> None:
assert success is True


@pytest.mark.ado_test
def test_save_as_pdf_with_filter(adr_service_query, request, get_exec) -> None:
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_filter")
success = my_report.export_pdf(file_name=pdf_file, item_filter="A|i_type|cont|image;")
except Exception:
success = False
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) -> None:
success = False
Expand Down
37 changes: 37 additions & 0 deletions tests/test_report_remote_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,43 @@ def test_export_pdf(adr_service_query, get_exec) -> None:
assert success is True


@pytest.mark.ado_test
def test_export_pdf_with_filter(adr_service_query, get_exec) -> None:
exec_basis = get_exec
item_filter = "A|i_type|cont|image;"
success = False
try:
my_report = adr_service_query.get_report(report_name="My Top Report")
success = True
except SyntaxError:
success = False
s = adr_service_query.serverobj
if exec_basis:
if environ.get("ANSYS_REL_INT_I"):
ansys_version = int(environ.get("ANSYS_REL_INT_I"))
else:
import re

matches = re.search(r".*v([0-9]{3}).*", exec_basis)
ansys_version = int(matches.group(1))
s.export_report_as_pdf(
report_guid=my_report.report.guid,
file_name="mytest",
exec_basis=exec_basis,
ansys_version=ansys_version,
item_filter=item_filter,
)
else:
# If no local installation, then you can not run the routine for pdf conversion. OSError expected.
try:
s.export_report_as_pdf(
report_guid=my_report.report.guid, file_name="mytest", item_filter=item_filter
)
except OSError:
success = True
assert success is True


@pytest.mark.ado_test
def test_export_pptx_error(adr_service_query) -> None:
my_report = adr_service_query.get_report(report_name="My Top Report")
Expand Down

0 comments on commit f7d7469

Please sign in to comment.