-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport.py
More file actions
155 lines (120 loc) · 4.2 KB
/
Copy pathreport.py
File metadata and controls
155 lines (120 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
from datetime import datetime
class report():
def __init__(self, dir_path, version):
self._report_path = dir_path + "/_report.html"
now = datetime.now()
current_time = now.strftime("%Y-%m-%d %H:%M")
self.audioFileNumber = 0
# Init report file
html = """<!DOCTYPE html>
<html lang="fi" class="no-js">
<head>
<meta charset="UTF-8">
<title>Baim Report</title>
<link rel='stylesheet' href='styles.css' media='all' />
</head>
<body>
<h1>""" + dir_path + """</h1>
<p>Generated """ + current_time + """ with version """ + version + """</p>
"""
f = open(self._report_path, "w+")
f.write(html)
f.close()
# Init css file
self._css_path = dir_path + "/styles.css"
css = """
body {
font-family: sans-serif;
}
h2 {
margin-top: 2em;
}
.segment {
background-color: rgb(184, 210, 182);
padding: 0.5em;
margin-bottom: 1em;
}
.segment p {
margin: 1em 1em 1em 1em;
}
.p22, .p23 {
font-weight: bold;
}
audio {
outline: none;
border: 7px solid transparent;
margin: -5px -5px -5px -13px;
}
audio:focus {
outline: none;
border: 7px solid #fff;
background-color: #000;
}
figure {
margin-left: 1em;
}
"""
f = open(self._css_path, "w+")
f.write(css)
f.close()
def add_segment(self, props, segment_filename):
self.audioFileNumber += 1
html = ""
html += "<div class='segment'>\n"
html += "<p class='p1'><span class='p11'>" + props["scientific_name"] + "</span> <span class='p12'>" + str(props["confidence"]) + "</span></p>"
html += "<p class='p2'><span class='p21'>" + str(props["file_start_datetime"]) + "</span> / <span class='p22'>" + props["audio_filename"] + "</span> / <span class='p23'>" + props["segment_start"] + "</span></p>"
html += """
<figure>
<figcaption></figcaption>
<audio
controls id='audio""" + str(self.audioFileNumber) + """'
src='""" + segment_filename + """'>
</audio>
</figure>
"""
html += "</div>\n"
file = open(self._report_path, "a")
file.write(html + "\n")
file.close()
def add_taxon_divider(self, taxon_name):
html = ""
html += f"<h2>{taxon_name}</h2>\n"
file = open(self._report_path, "a")
file.write(html + "\n")
file.close()
def finalize(self):
html = ""
html += """
<script defer>
document.addEventListener('keydown', function(event) {
if (event.keyCode === 40 && document.activeElement.tagName === 'AUDIO') {
event.preventDefault();
// Pause the current audio element
document.activeElement.pause();
// Find the next audio element and its play button
var nextIdNumber = parseInt(document.activeElement.id.replace(/\D/g, '')) + 1;
var nextIdName = "audio" + nextIdNumber;
// console.log(nextIdName);
var nextAudioElement = document.getElementById(nextIdName);
// console.log(nextAudioElement)
nextAudioElement.focus();
}
});
document.addEventListener('keydown', function(event) {
if (event.keyCode === 38 && document.activeElement.tagName === 'AUDIO') {
event.preventDefault();
document.activeElement.pause();
var prevIdNumber = parseInt(document.activeElement.id.replace(/\D/g, '')) - 1;
var prevIdName = "audio" + prevIdNumber;
var prevAudioElement = document.getElementById(prevIdName);
prevAudioElement.focus();
}
});
</script>
<div id="eof">EOF</div>
</body>
</html>
"""
file = open(self._report_path, "a")
file.write(html + "\n")
file.close()