Passed
Push — master ( 13be50...a4d20f )
by Cyb3r
01:55
created

MetaStalk.utils.export.export.html_export()   A

Complexity

Conditions 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nop 1
dl 0
loc 15
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
b 0
f 0
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:
54
                self.log.error("Missing packages from metastalk[image]")
55
                raise EnvironmentError("Missing packages from metastalk[image]")
56
57 1
    def html_export(self) -> None:
58
        """html_export
59
        ---
60
        Deals with export of html.
61
        If offline is true then there will be a script tag in the .html file making it much larger
62
        but if it is cdn then the html will cotain a script tag that points to
63
        https://cdn.plot.ly/plotly-latest.min.js
64
        """
65 1
        if self.choice == "html_offline":
66 1
            offline = True
67
        else:
68 1
            offline = "cdn"
69 1
        for name, chart in self.plots.items():
70 1
            chart.write_html(
71
                f"{self.output_dir}/{name}.html", include_plotlyjs=offline,
72
            )
73