Passed
Push — master ( 929827...929f74 )
by Cyb3r
02:12
created

MetaStalk.utils.web.graph()   A

Complexity

Conditions 4

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 24
nop 4
dl 0
loc 37
ccs 13
cts 13
cp 1
crap 4
rs 9.304
c 0
b 0
f 0
1
"""Uses dash to create a webpage that contain all the graphs"""
2 1
import timeit
3 1
import logging
4 1
import webbrowser
5 1
from datetime import datetime
6 1
import dash
7 1
import dash_html_components as html
8 1
import dash_core_components as dcc
9
10
11 1
log = logging.getLogger("MetaStalk")
12
13
14 1
def graph(plots: dict, t_start: float, test: bool, no_open: bool):
15
    """graph
16
17
    Displays all the plots that are passed to it.
18
19
    Arguments:
20
        plots {dict} -- All the plot that get displayed
21
        t_start {float} -- The start time of MetaStalk
22
        test {bool} -- Whether or not to start web server (default: {False})
23
        no_open {bool} -- Whether or now to open with the browser (default: {False})
24
    """
25 1
    graphs = []
26 1
    for name, chart in plots.items():
27 1
        graphs.append(dcc.Graph(id="graph-{}".format(name), figure=chart))
28 1
    external_stylesheets = ["https://codepen.io/chriddyp/pen/bWLwgP.css"]
29 1
    app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
30 1
    app.logger.disabled = True
31 1
    app.title = "MetaStalk"
32 1
    t_stop = timeit.default_timer()
33 1
    app.layout = html.Div(
34
        [
35
            html.H1("MetaStalk", style={"textAlign": "center"}),
36
            html.H6(html.A("Cyber Jake", href="https://twitter.com/Cyb3r_Jak3"),
37
                    style={"textAlign": "center"}),
38
            html.Div(children=graphs),
39
            html.Footer("Time Taken = {0:.2f} seconds".format(t_stop - t_start),
40
                        style={"textAlign": "right"}),
41
            html.Footer(f"Run Time: {datetime.now().strftime('%m/%d/%Y, %H:%M:%S')}")
42
        ]
43
    )
44 1
    if not test:
45
        if not no_open:  # pragma: no cover
46
            webbrowser.open("http://localhost:8052", new=2)  # pragma: no cover
47
        app.run_server(port=8052)  # pragma: no cover
48
    else:
49 1
        log.info("Test flag was set. No webpage will be shown.")
50
        log.info("Time Taken = {0:.2f} seconds".format(t_stop - t_start))
51