-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_html.py
99 lines (77 loc) · 2.7 KB
/
generate_html.py
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
"""Generate the HTML pages needed for the UI."""
import glob
import json
# sys.path.append("/")
#
from utils.get_domain_name import get_domain_name # noqa: E402
all_function_names = []
with open("./ui_templates/main_template.html") as f:
main_template = f.read()
with open("./ui_templates/index_template.html") as f:
index_template = f.read()
drop_down_template = """
<label for="{0}">{1}</label>
<select id="{0}">
{2}
</select>
"""
drop_down_options_template = """<option value="{0}">{0}</option>"""
bullet_list_template = """
<ul>
{0}
</ul>
"""
bullet_list_item_template = """\t<li><a href="/{0}.html">{0}</a></li>"""
form_template = """
<form id="textForm">
{0}
<button type="submit">Submit</button>
</form>
"""
text_area_template = """
\t<label for="{0}">{1}</label><br>
\t<textarea id="{0}" name="{0}" rows="7" cols="75"></textarea><br>
"""
for file in glob.glob("./lib/configs/lambdas/*.json"):
html_fields = []
with open(file) as f:
function_metadata = json.load(f)
function_name = file.split("/")[-1].split(".")[0]
all_function_names.append(function_name)
form_data = ",".join(
[
f"{i['name']}: document.getElementById('{i['name']}').value"
for i in function_metadata["form_fields"]
]
)
for field in function_metadata["form_fields"]:
if field["type"] == "text_area":
html_fields.append(text_area_template.format(field["name"], field["label"]))
if field["type"] == "drop_down":
html_fields.append(
drop_down_template.format(
field["name"],
field["label"],
"\n".join(
[
drop_down_options_template.format(i)
for i in field["drop_down_options"]
]
),
),
)
full_form = form_template.format("\n".join(html_fields))
function_template = main_template.replace("FUNCTION_NAME", function_name)
function_template = function_template.replace("FULL_FORM", full_form)
function_template = function_template.replace("FORM_DATA", form_data)
function_template = function_template.replace("DOMAIN_NAME", get_domain_name())
with open(f"./ui_compiled/{function_name}.html", "w") as f:
f.write(function_template)
page_list = "\n".join(
[bullet_list_item_template.format(i) for i in all_function_names]
)
new_index = bullet_list_template.format(page_list)
new_index = index_template.replace("PAGE_LIST", new_index)
with open("./ui_compiled/index.html", "w") as f:
f.write(new_index)
# {"b":"1","a":"2"}