1
|
|
|
"""utils.export.
|
2
|
|
|
---
|
3
|
|
|
|
4
|
|
|
Exports the plots as interactive html
|
5
|
|
|
"""
|
6
|
1 |
|
import logging
|
7
|
1 |
|
import os
|
8
|
|
|
|
9
|
|
|
|
10
|
1 |
|
class export:
|
11
|
|
|
"""export.
|
12
|
|
|
---
|
13
|
|
|
|
14
|
|
|
Deals with the export to html and images. Probably not the best way to run everything but it
|
15
|
|
|
works the best.
|
16
|
|
|
"""
|
17
|
|
|
|
18
|
1 |
|
def __init__(self, choice: str, output_dir: str, plots: dict):
|
19
|
1 |
|
self.log = logging.getLogger("MetaStalk")
|
20
|
1 |
|
self.choice = choice
|
21
|
1 |
|
self.output_dir = output_dir
|
22
|
1 |
|
self.plots = plots
|
23
|
1 |
|
self.directory_management()
|
24
|
1 |
|
if choice in ["html", "html_offline"]:
|
25
|
1 |
|
self.html_export()
|
26
|
|
|
elif choice in ["pdf", "svg", "webp", "jpeg", "png"]:
|
27
|
|
|
self.image_export()
|
28
|
|
|
|
29
|
1 |
|
def directory_management(self) -> None:
|
30
|
|
|
"""directory_management.
|
31
|
|
|
---
|
32
|
|
|
Creates directory for output if it does not exist and if it does then it will check if there
|
33
|
|
|
are already file in it.
|
34
|
|
|
"""
|
35
|
1 |
|
if not os.path.isdir(self.output_dir):
|
36
|
1 |
|
os.makedirs(self.output_dir)
|
37
|
|
|
else:
|
38
|
|
|
if len(os.listdir(self.output_dir)) != 0:
|
39
|
|
|
self.log.warning("The chosen output directory contain files.")
|
40
|
|
|
|
41
|
1 |
|
def image_export(self) -> None:
|
42
|
|
|
"""image_export.
|
43
|
|
|
---
|
44
|
|
|
|
45
|
|
|
Deals with export images
|
46
|
|
|
|
47
|
|
|
Raises:
|
48
|
|
|
EnvironmentError: Raised if packages from metastalk[image] are not installed.
|
49
|
|
|
"""
|
50
|
|
|
for name, chart in self.plots.items():
|
51
|
|
|
try:
|
52
|
|
|
chart.write_image(f"{self.output_dir}/{name}.{self.choice}")
|
53
|
|
|
except ValueError as err:
|
54
|
|
|
self.log.error("Missing packages from metastalk[image]")
|
55
|
|
|
raise EnvironmentError(
|
56
|
|
|
"Missing packages from metastalk[image]"
|
57
|
|
|
) from err
|
58
|
|
|
|
59
|
1 |
|
def html_export(self) -> None:
|
60
|
|
|
"""html_export
|
61
|
|
|
---
|
62
|
|
|
Deals with export of html.
|
63
|
|
|
If offline is true then there will be a script tag in the .html file making it much larger
|
64
|
|
|
but if it is cdn then the html will cotain a script tag that points to
|
65
|
|
|
https://cdn.plot.ly/plotly-latest.min.js
|
66
|
|
|
"""
|
67
|
1 |
|
if self.choice == "html_offline":
|
68
|
1 |
|
offline = True
|
69
|
|
|
else:
|
70
|
1 |
|
offline = "cdn"
|
71
|
1 |
|
for name, chart in self.plots.items():
|
72
|
1 |
|
chart.write_html(
|
73
|
|
|
f"{self.output_dir}/{name}.html",
|
74
|
|
|
include_plotlyjs=offline,
|
75
|
|
|
)
|
76
|
|
|
|