-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathyaml_to_html.py
More file actions
executable file
·55 lines (45 loc) · 1.67 KB
/
yaml_to_html.py
File metadata and controls
executable file
·55 lines (45 loc) · 1.67 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
#!/usr/bin/env python3
import pandas as pd
import yaml
import shutil
yaml_file = "features.yaml"
html_dir = "docs/"
html_table = f"{html_dir}features.html"
html_header = f"{html_dir}header.html"
html_scripts = f"{html_dir}javascript.html"
html_footer = f"{html_dir}footer.html"
html_index = f"{html_dir}index.html"
# Load data
with open(yaml_file) as in_file:
data = pd.json_normalize(yaml.safe_load(in_file))
# Choose desired columns
all_columns = False
default_columns = ["name",
"domain",
"objectives",
"variable type",
"constraints",
"reference",
"implementation",
"textual description"]
if all_columns is False:
columns = default_columns
data = data[columns]
# Generate plain table
table = data.to_html(render_links=True,
index=False,
table_id="features",
classes=["display compact", "display", "styled-table"], # Set display style
border=0,
na_rep="") # Leave NaN cells empty
# Add footer to facilitate individual column search
idx = table.index('</table>')
final_table = table[:idx] + "<tfoot><tr>" + " ".join(["<th>"+ i +"</th>" for i in data.columns])+"</tr> </tfoot>" + table[idx:]
# Write table to file
with open(html_table, "w") as table_file:
table_file.write(final_table)
# Merge table and scripts into HTML page
with open(html_index, "wb") as output_file:
for in_file in [html_header, html_table, html_scripts, html_footer]:
with open(in_file, "rb") as in_file:
shutil.copyfileobj(in_file, output_file)