reporting.py 3.47 KB
Newer Older
1
2
3
4
# reporting.py - Routines for generating proc reports
#
# Author: William Clarke <william.clarke@ndcn.ox.ac.uk>
#
5
# Copyright (C) 2019 University of Oxford
6
7
# SHBASECOPYRIGHT

8
9
10
11
from jinja2 import FileSystemLoader, Environment
import os.path as op
from dataclasses import dataclass
from datetime import datetime
12
13
templatePath = op.join(op.dirname(__file__), 'templates')

14
15
16
17
18
19

@dataclass
class figgroup:
    '''Keep figure data together'''
    fig: str = ''
    foretext: str = ''
20
    afttext: str = ''
21
22
    name: str = ''

23

24
def plotStyles():
25
26
    '''Return plot styles'''
    # colors = {'in':'rgb(67,67,67)',
27
28
29
    #            'out':'rgb(0,0,255)',
    #            'emph':'rgb(255,0,0)',
    #            'diff':'rgb(0,255,0)'}
30
31
32
33
34
35
36
37
38
39
40
41
    colors = {'in': 'rgb(166,206,227)',
              'out': 'rgb(31,120,180)',
              'emph': 'rgb(251,154,153)',
              'diff': 'rgb(51,160,44)',
              'spare': 'rgb(178,223,138)',
              'blk': 'rgb(0,0,0)'}
    line_size = {'in': 2,
                 'out': 2,
                 'emph': 2,
                 'diff': 2,
                 'spare': 2,
                 'blk': 2}
42
43
    line = {}
    for key in colors:
44
45
        line.update({key: {'color': colors[key],
                           'width': line_size[key]}})
46

47
    return line, colors, line_size
48

49
50

def plotAxesStyle(fig, ppmlim, title=None):
51
52
53
    if ppmlim is not None:
        fig.layout.xaxis.update(title_text='Chemical shift (ppm)',
                                tick0=2, dtick=.5,
54
55
                                range=[ppmlim[1], ppmlim[0]])
    else:
56
        fig.layout.xaxis.update(title_text='Chemical shift (ppm)',
57
58
                                tick0=2, dtick=.5,
                                autorange="reversed")
59
60
61
62
63
    fig.layout.yaxis.update(zeroline=True,
                            zerolinewidth=1,
                            zerolinecolor='Gray',
                            showgrid=False,
                            showticklabels=False)
64
65
    if title is not None:
        fig.layout.update({'title': title})
66
67
    fig.update_layout(template='plotly_white')

68

69
def singleReport(outfile, opName, headerinfo, figurelist):
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
    """
    Entry point for the script.
    Render a template and write it to file.
    :return:
    """
    # Configure Jinja and ready the loader

    env = Environment(
        loader=FileSystemLoader(searchpath=templatePath)
    )

    # Assemble the templates we'll use
    base_template = env.get_template("report.html")
    figure_section_template = env.get_template("figure_section.html")
    hdr_section_template = env.get_template("header_section.html")
    op_section_template = env.get_template("operation_section.html")
86

87
88
89
90
91
92
93
94
    # Content to be published
    datestr = datetime.now().strftime("%Y-%m-%d %H:%M")
    title = f"Report for {opName} - {datestr}"

    # Construct figures
    figsections = []
    for figs in figurelist:
        figsections.append(figure_section_template.render(
95
96
97
98
                           figName=figs.name,
                           foretext=figs.foretext,
                           figure=figs.fig,
                           afttext=figs.afttext))
99

100
101
    hdr = hdr_section_template.render(hdrName=opName + '_hdr',
                                      hdrText=headerinfo)
102
103
104
105
106
107

    sections = []
    sections.append(op_section_template.render(
        opName=opName,
        header_sec=hdr,
        fig_sections=figsections
108
    ))
109
110

    with open(outfile, "w") as f:
111
        f.write(base_template.render(
112
                title=title,
113
                sections=sections))