Skip to content

Commit 231525f

Browse files
authored
Feat/export (#78)
1 parent 1398b51 commit 231525f

File tree

8 files changed

+170
-4
lines changed

8 files changed

+170
-4
lines changed

.github/workflows/ci_cd.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ jobs:
120120
with:
121121
python-version: ${{ env.MAIN_PYTHON_VERSION }}
122122
check-links: false
123+
sphinxopts: '-j auto'
123124

124125
package:
125126
name: Package library

.github/workflows/nightly-docs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ jobs:
2323
with:
2424
python-version: ${{ env.MAIN_PYTHON_VERSION }}
2525
check-links: false
26+
sphinxopts: '-j auto'
2627

2728
docs_upload:
2829
needs: docs_build

doc/source/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474

7575
# Intersphinx mapping
7676
intersphinx_mapping = {
77-
"python": ("https://docs.python.org/3", None),
77+
"python": ("https://docs.python.org/3.11", None),
7878
# kept here as an example
7979
# "scipy": ("https://docs.scipy.org/doc/scipy/reference", None),
8080
# "numpy": ("https://numpy.org/devdocs", None),

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ requires = [
88

99
[project]
1010
name = "ansys-dynamicreporting-core"
11-
version = "0.6.0.dev0"
11+
version = "0.6.1.dev0"
1212
authors = [
1313
{name = "ANSYS, Inc.", email = "[email protected]"},
1414
]

src/ansys/dynamicreporting/core/adr_report.py

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
my_report.visualize()
1919
"""
2020
import sys
21+
from typing import Optional
2122
import webbrowser
2223

2324
from ansys.dynamicreporting.core.adr_utils import in_ipynb
@@ -182,7 +183,7 @@ def get_iframe(self, width: int = 1000, height: int = 800):
182183
import ansys.dynamicreporting.core as adr
183184
adr_service = adr.Service(ansys_installation = r'C:\\Program Files\\ANSYS Inc\\v232')
184185
ret = adr_service.connect()
185-
my_report = adr_service.get_report(report_name = "My Top Report"
186+
my_report = adr_service.get_report(report_name = "My Top Report")
186187
report_iframe = my_report.get_iframe()
187188
"""
188189
if "IPython.display" in sys.modules:
@@ -191,3 +192,126 @@ def get_iframe(self, width: int = 1000, height: int = 800):
191192
else:
192193
iframe = None
193194
return iframe
195+
196+
def export_pdf(
197+
self,
198+
file_name: str = "",
199+
query: Optional[dict] = None,
200+
page: Optional[list] = None,
201+
delay: Optional[int] = None,
202+
) -> bool:
203+
"""
204+
Export report as PDF. Currently works only with a local ADR installation, and
205+
not a docker image.
206+
207+
Parameters
208+
----------
209+
file_name : str
210+
Path and filename for the PDF file to export.
211+
query : dict, optional
212+
Dictionary for query parameters to apply to report template before export. Default: None
213+
page : list, optional
214+
List of integers that represents the size of the exported pdf. Default: None, which
215+
corresponds to A4 size
216+
delay : int, optional
217+
Seconds to delay the start of the pdf export operation. Default: None, which
218+
corresponds to no delay
219+
220+
Returns
221+
-------
222+
bool
223+
Success status of the PDF export: True if it worked, False otherwise
224+
225+
Examples
226+
--------
227+
::
228+
229+
import ansys.dynamicreporting.core as adr
230+
adr_service = adr.Service(ansys_installation = r'C:\\Program Files\\ANSYS Inc\\v232')
231+
ret = adr_service.connect()
232+
my_report = adr_service.get_report(report_name = "My Top Report")
233+
succ = my_report.export_pdf(file_name=r'D:\\tmp\\myreport.pdf')
234+
"""
235+
success = False # pragma: no cover
236+
if self.service is None: # pragma: no cover
237+
self.service.logger.error("No connection to any report")
238+
return ""
239+
if self.service.serverobj is None: # pragma: no cover
240+
self.service.logger.error("No connection to any server")
241+
return ""
242+
try:
243+
if query is None:
244+
query = {}
245+
self.service.serverobj.export_report_as_pdf(
246+
report_guid=self.report.guid,
247+
file_name=file_name,
248+
query=query,
249+
page=page,
250+
parent=None,
251+
delay=delay,
252+
exec_basis=self.service._ansys_installation,
253+
ansys_version=self.service._ansys_version,
254+
)
255+
success = True
256+
except Exception as e: # pragma: no cover
257+
self.service.logger.error(f"Can not export pdf report: {str(e)}")
258+
return success
259+
260+
def export_html(
261+
self,
262+
directory_name: str = "",
263+
query: Optional[dict] = None,
264+
filename: Optional[str] = "index.html",
265+
no_inline_files: Optional[bool] = False,
266+
) -> bool:
267+
"""
268+
Export report as static HTML.
269+
270+
Parameters
271+
----------
272+
directory_name : str
273+
....
274+
query : dict, optional
275+
Dictionary for query parameters to apply to report template before export. Default: None
276+
filename : str, optional
277+
Filename for the exported static HTML file. Default: index.html
278+
no_inline_files : bool, optional
279+
If True, the information is exported as stand alone files instead of in line content
280+
in the static HTML. Default: False
281+
282+
Returns
283+
-------
284+
bool
285+
Success status of the HTML export: True if it worked, False otherwise
286+
287+
Examples
288+
--------
289+
::
290+
291+
import ansys.dynamicreporting.core as adr
292+
adr_service = adr.Service(ansys_installation = r'C:\\Program Files\\ANSYS Inc\\v232')
293+
ret = adr_service.connect()
294+
my_report = adr_service.get_report(report_name = "My Top Report")
295+
succ = my_report.export_html(directory_name = r'D:\\tmp')
296+
"""
297+
success = False
298+
if self.service is None: # pragma: no cover
299+
self.service.logger.error("No connection to any report")
300+
return ""
301+
if self.service.serverobj is None: # pragma: no cover
302+
self.service.logger.error("No connection to any server")
303+
return ""
304+
try:
305+
if query is None:
306+
query = {}
307+
self.service.serverobj.export_report_as_html(
308+
report_guid=self.report.guid,
309+
directory_name=directory_name,
310+
query=query,
311+
filename=filename,
312+
no_inline_files=no_inline_files,
313+
)
314+
success = True
315+
except Exception as e: # pragma: no cover
316+
self.service.logger.error(f"Can not export static HTML report: {str(e)}")
317+
return success

test_cleanup.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@
1010
"tests/test_data/viewer_test/",
1111
]
1212
dir_list.extend(
13-
["tests/test_data/ansys", "tests/test_data/media", "tests/test_data/webfonts/", "htmltest/"]
13+
[
14+
"tests/test_data/ansys",
15+
"tests/test_data/media",
16+
"tests/test_data/webfonts/",
17+
"htmltest/",
18+
"htmltest_again/",
19+
]
1420
)
1521
dir_list.append("tests/test_data/create_delete/")
1622
dir_list.append("tests/test_data/create_twice/")
@@ -32,6 +38,7 @@
3238
file_list.extend(glob.glob("tests/outfile*.txt"))
3339
file_list.append("mypresentation")
3440
file_list.append("mytest.pdf")
41+
file_list.append("again_mytest")
3542
for i_file in file_list:
3643
try:
3744
os.remove(i_file)

tests/conftest.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ def adr_service_query(request, pytestconfig: pytest.Config) -> Service:
7474
use_local = pytestconfig.getoption("use_local_launcher")
7575
local_db = os.path.join("test_data", "query_db")
7676
db_dir = os.path.join(request.fspath.dirname, local_db)
77+
tmp_docker_dir = os.path.join(
78+
os.path.join(request.fspath.dirname, "test_data"), "tmp_docker_query"
79+
)
7780
if use_local:
7881
ansys_installation = pytestconfig.getoption("install_path")
7982
else:
@@ -83,6 +86,7 @@ def adr_service_query(request, pytestconfig: pytest.Config) -> Service:
8386
ansys_installation=ansys_installation,
8487
docker_image=DOCKER_DEV_REPO_URL,
8588
db_directory=db_dir,
89+
data_directory=tmp_docker_dir,
8690
port=8000 + int(random() * 4000),
8791
)
8892
tmp_service.start(create_db=False, exit_on_close=True, delete_db=False)

tests/test_report.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,32 @@ def test_unit_no_url(request) -> bool:
9696
if "No connection to any server" in line:
9797
err_msg = True
9898
assert err_msg
99+
100+
101+
@pytest.mark.ado_test
102+
def test_save_as_pdf(adr_service_query, request, get_exec) -> bool:
103+
exec_basis = get_exec
104+
if exec_basis:
105+
success = False
106+
try:
107+
my_report = adr_service_query.get_report(report_name="My Top Report")
108+
pdf_file = os.path.join(request.fspath.dirname, "again_mytest")
109+
success = my_report.export_pdf(file_name=pdf_file)
110+
except Exception:
111+
success = False
112+
adr_service_query.stop()
113+
else: # If no local installation, then skip this test
114+
success = True
115+
assert success is True
116+
117+
118+
@pytest.mark.ado_test
119+
def test_save_as_html(adr_service_query) -> bool:
120+
success = False
121+
try:
122+
my_report = adr_service_query.get_report(report_name="My Top Report")
123+
success = my_report.export_html(directory_name="htmltest_again")
124+
except Exception:
125+
success = False
126+
adr_service_query.stop()
127+
assert success is True

0 commit comments

Comments
 (0)