-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebquiz.py
More file actions
executable file
·136 lines (111 loc) · 4.1 KB
/
Copy pathwebquiz.py
File metadata and controls
executable file
·136 lines (111 loc) · 4.1 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
#!/bin/python3
import json
import argparse
COLORS = [
("rgba(91, 192, 235, 1)", "rgba(195, 232, 247, 1);"),
("rgba(253, 231, 76, 1);", "rgba(254, 246, 189, 1);"),
("rgba(155, 197, 61, 1);", "rgba(218, 233, 184, 1);"),
("rgba(195, 66, 63, 1);", "rgba(233, 186, 185, 1);")
]
def color_styles():
return "\n".join(["""
.question.cat{cat} {{
background: {color};
}}
.answer.cat{cat} {{
background: #424242;
color: {color};
}}
a:visited.overview-link.cat{cat} {{
background: #878787;
color: {color_visited};
}}
a:visited .question.cat{cat} {{
background: {color_visited};
color: #878787;
}}""".format(cat=i, color=color[0], color_visited=color[1])
for i, color in enumerate(COLORS)])
def overview_headings(data):
return "\n".join(["<td class=overview-head>{name}</td>".format(name=cat)
for cat in sorted(data['categories'].keys())])
def overview_cells(data):
ret = ""
max_questions = max([len(qs) for qs in data['categories'].values()])
for qvalue in range(max_questions):
ret += """
<tr>"""
for qcat in range(len(data['categories'])):
cat = sorted(data['categories'].keys())[qcat]
if(len(data['categories'][cat]) > qvalue):
ret += """
<td class="overview-cell">
<a class="overview-link question cat{qcat}" href="#q{qcat}-{qvalue}">
<div class="overview-centered">
{qvalueprint}
</div>
<span class="status right" id="q{qcat}-{qvalue}-right">✔</span><span class="status wrong" id="q{qcat}-{qvalue}-wrong">⨯</span>
</a>
</td>""".format(qcat=qcat, qvalue=qvalue+1,
qvalueprint=(qvalue + 1) * 100)
else:
ret += """
<td class="overview-cell">
</td>"""
ret += """
</tr>"""
return ret
def questions(data):
max_questions = max([len(qs) for qs in data['categories'].values()])
ret = ""
for qvalue in range(max_questions):
for qcat in range(len(data['categories'])):
cat = sorted(data['categories'].keys())[qcat]
if(len(data['categories'][cat]) > qvalue):
ret += """
<a href="#a{qcat}-{qvalue}">
<div id="q{qcat}-{qvalue}" class="screen">
<div class="centered bigone question cat{qcat}">
{question}
</div>
</div>
</a>""".format(question=data['categories'][cat][qvalue]['question'],
qvalue=qvalue+1, qcat=qcat)
return ret
def answers(data):
max_questions = max([len(qs) for qs in data['categories'].values()])
ret = ""
for qvalue in range(max_questions):
for qcat in range(len(data['categories'])):
cat = sorted(data['categories'].keys())[qcat]
if(len(data['categories'][cat]) > qvalue):
ret += """
<a href="#overview">
<div id="a{qcat}-{qvalue}" class="screen">
<div class="bigone answer cat{qcat}">
<div class="centered">{answer}</div>
<div>
<div class="status right" onclick="document.getElementById('q{qcat}-{qvalue}-right').style.display = 'block';">✔</div>
<div class="status wrong" onclick="document.getElementById('q{qcat}-{qvalue}-wrong').style.display = 'block';">⨯</div>
</div>
</div>
</div>
</a>""".format(answer=data['categories'][cat][qvalue]['answer'],
qvalue=qvalue+1, qcat=qcat)
return ret
def main():
parser = argparse.ArgumentParser(
description='Generate a clickable HTML/JS quiz for use on a beamer.')
parser.add_argument('config', help="JSON configuration file")
args = parser.parse_args()
with open(args.config, "r") as config, open("base.html", "r") as basefile:
data = json.load(config)
base = basefile.read()
print(base.format(
color_styles=color_styles(),
overview_headings=overview_headings(data),
overview_cells=overview_cells(data),
questions=questions(data),
answers=answers(data)
))
if __name__ == "__main__":
main()