OLD | NEW |
1 # Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. | 1 # Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
2 # | 2 # |
3 # Use of this source code is governed by a BSD-style license | 3 # Use of this source code is governed by a BSD-style license |
4 # that can be found in the LICENSE file in the root of the source | 4 # that can be found in the LICENSE file in the root of the source |
5 # tree. An additional intellectual property rights grant can be found | 5 # tree. An additional intellectual property rights grant can be found |
6 # in the file PATENTS. All contributing project authors may | 6 # in the file PATENTS. All contributing project authors may |
7 # be found in the AUTHORS file in the root of the source tree. | 7 # be found in the AUTHORS file in the root of the source tree. |
8 | 8 |
9 import logging | 9 import logging |
10 import os | 10 import os |
11 import re | 11 import re |
12 | 12 |
13 | 13 |
14 class HtmlExport(object): | 14 class HtmlExport(object): |
15 | 15 |
16 # Path to CSS and JS files. | 16 # Path to CSS and JS files. |
17 _PATH = os.path.dirname(os.path.realpath(__file__)) | 17 _PATH = os.path.dirname(os.path.realpath(__file__)) |
18 | 18 |
19 # CSS file parameters. | 19 # CSS file parameters. |
20 _CSS_FILEPATH = os.path.join(_PATH, 'results.css') | 20 _CSS_FILEPATH = os.path.join(_PATH, 'results.css') |
21 _INLINE_CSS = True | 21 _INLINE_CSS = False |
| 22 |
| 23 # JS file parameters. |
| 24 _JS_FILEPATH = os.path.join(_PATH, 'results.js') |
| 25 _INLINE_JS = False |
22 | 26 |
23 _NEW_LINE = '\n' | 27 _NEW_LINE = '\n' |
24 | 28 |
25 def __init__(self, output_filepath): | 29 def __init__(self, output_filepath): |
26 self._noise_names = None | 30 self._noise_names = None |
27 self._noise_params = None | 31 self._noise_params = None |
28 self._output_filepath = output_filepath | 32 self._output_filepath = output_filepath |
29 | 33 |
30 def export(self, scores): | 34 def export(self, scores): |
31 """ | 35 """ |
32 Export the scores into an HTML file. | 36 Export the scores into an HTML file. |
33 | 37 |
34 Args: | 38 Args: |
35 scores: nested dictionary containing the scores. | 39 scores: nested dictionary containing the scores. |
36 """ | 40 """ |
37 # Generate one table for each evaluation score. | 41 # Generate one table for each evaluation score. |
38 tables = [] | 42 tables = [] |
39 for score_name in sorted(scores.keys()): | 43 for score_name in sorted(scores.keys()): |
40 tables.append(self._build_score_table(score_name, scores[score_name])) | 44 tables.append(self._build_score_table(score_name, scores[score_name])) |
41 | 45 |
42 # Create the html file. | 46 # Create the html file. |
43 html = ( | 47 html = ( |
44 '<html>' + | 48 '<html>' + |
45 self._build_header() + | 49 self._build_header() + |
46 '<body>' + | 50 '<body onload="initialize()">' + |
47 '<h1>Results from {}</h1>'.format(self._output_filepath) + | 51 '<h1>Results from {}</h1>'.format(self._output_filepath) + |
48 self._NEW_LINE.join(tables) + | 52 self._NEW_LINE.join(tables) + |
49 '</body>' + | 53 '</body>' + |
50 '</html>') | 54 '</html>') |
51 | 55 |
52 self._save(self._output_filepath, html) | 56 self._save(self._output_filepath, html) |
53 | 57 |
54 def _build_header(self): | 58 def _build_header(self): |
55 """ | 59 """ |
56 HTML file header with page title and either embedded or linked CSS and JS | 60 HTML file header with page title and either embedded or linked CSS and JS |
57 files. | 61 files. |
58 """ | 62 """ |
59 html = ['<head>', '<title>Results</title>'] | 63 html = ['<head>', '<title>Results</title>'] |
60 | 64 |
| 65 # Function to append the lines of a text file to html. |
| 66 def _embed_file(filepath): |
| 67 with open(filepath) as f: |
| 68 for l in f: |
| 69 html.append(l.strip()) |
| 70 |
61 # CSS. | 71 # CSS. |
62 if self._INLINE_CSS: | 72 if self._INLINE_CSS: |
63 # Embed. | 73 # Embed. |
64 html.append('<style>') | 74 html.append('<style>') |
65 with open(self._CSS_FILEPATH) as f: | 75 _embed_file(self._CSS_FILEPATH) |
66 for l in f: | |
67 html.append(l.strip()) | |
68 html.append('</style>') | 76 html.append('</style>') |
69 else: | 77 else: |
70 # Link. | 78 # Link. |
71 html.append('<link rel="stylesheet" type="text/css" ' | 79 html.append('<link rel="stylesheet" type="text/css" ' |
72 'href="file://{}?">'.format(self._CSS_FILEPATH)) | 80 'href="file://{}?">'.format(self._CSS_FILEPATH)) |
73 | 81 |
| 82 # Javascript. |
| 83 if self._INLINE_JS: |
| 84 # Embed. |
| 85 html.append('<script>') |
| 86 _embed_file(self._JS_FILEPATH) |
| 87 html.append('</script>') |
| 88 else: |
| 89 # Link. |
| 90 html.append('<script src="file://{}?"></script>'.format( |
| 91 self._JS_FILEPATH)) |
| 92 |
74 html.append('</head>') | 93 html.append('</head>') |
75 | 94 |
76 return self._NEW_LINE.join(html) | 95 return self._NEW_LINE.join(html) |
77 | 96 |
78 def _build_score_table(self, score_name, scores): | 97 def _build_score_table(self, score_name, scores): |
79 """ | 98 """ |
80 Generate a table for a specific evaluation score (e.g., POLQA). | 99 Generate a table for a specific evaluation score (e.g., POLQA). |
81 """ | 100 """ |
82 config_names = sorted(scores.keys()) | 101 config_names = sorted(scores.keys()) |
83 input_names = sorted(scores[config_names[0]].keys()) | 102 input_names = sorted(scores[config_names[0]].keys()) |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 return html | 213 return html |
195 | 214 |
196 @classmethod | 215 @classmethod |
197 def _save(cls, output_filepath, html): | 216 def _save(cls, output_filepath, html): |
198 with open(output_filepath, 'w') as f: | 217 with open(output_filepath, 'w') as f: |
199 f.write(html) | 218 f.write(html) |
200 | 219 |
201 @classmethod | 220 @classmethod |
202 def _format_name(cls, name): | 221 def _format_name(cls, name): |
203 return re.sub(r'[_\-]', ' ', name) | 222 return re.sub(r'[_\-]', ' ', name) |
OLD | NEW |