-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson2html.py
executable file
·204 lines (168 loc) · 9.02 KB
/
json2html.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python
import json, re, os
from mendeleev import element
from larch.xray import xray_edge
class CommonMaterials():
'''A class for managing a JSON file of items in the BMM standards
collection and writing a useful HTML file summarizing the collection.
Example
=======
from json2html import Standards
m = Standards()
m.html = 'test.html'
m.make_html()
'''
def __init__(self):
self.json = 'standards.json'
self.html = 'test.html'
self.cssfile = 'standards.css'
self.element_data = 'pt.json'
self.nosamp = '[___]'
self.beneath_table = self.slurp('tmpl/beneath_table.tmpl')
self.goto_anchor = self.slurp('tmpl/goto_anchor.tmpl')
def slurp(self, fname):
with open(fname, 'r') as myfile:
text=myfile.read()
return text
def make_html(self):
'''Write the contents of the JSON file to a pretty html file'''
notfound = []
with open('standards.json', 'r') as myfile:
data=json.loads(myfile.read())
## page header and sidebar
page = self.slurp('tmpl/head.tmpl').format(cssfile=self.cssfile)
page += self.slurp('tmpl/sidebar.tmpl').format(nosamp=self.nosamp)
## generate each element box in the periodic table
with open(self.element_data, 'r') as ptjson:
ptdata=json.loads(ptjson.read())
for el in ptdata.keys():
ptdata[el]["key"] = el.lower()
if ptdata[el]["link"]:
page += self.slurp('tmpl/with_samples.tmpl').format(**ptdata[el])
else:
page += self.slurp('tmpl/without_samples.tmpl').format(**ptdata[el])
## the color explanation table and the javascript for jumping to
## an anchor from the periodic table
page += self.beneath_table
page += self.goto_anchor
## generate a table of samples in this collection for every element
for z in range(20, 95):
el = element(z)
print(el.symbol, end=' ', flush=True)
if el.symbol not in data:
continue
if xray_edge(el.symbol, 'K')[0] > 23500:
kcolor, lcolor = 'outofrange', 'inrange'
else:
kcolor, lcolor = 'inrange', 'outofrange'
if xray_edge(el.symbol, 'L3')[0] < 5000:
lcolor = 'outofrange'
## start the table for this element
if len(data[el.symbol]) > 0:
page += self.slurp('tmpl/element_table.tmpl').format(name = el.name,
symbol = el.symbol,
z = z,
kcolor = kcolor,
lcolor = lcolor,
kedge = xray_edge(el.symbol, 'K')[0],
l1edge = xray_edge(el.symbol, 'L1')[0],
l2edge = xray_edge(el.symbol, 'L2')[0],
l3edge = xray_edge(el.symbol, 'L3')[0])
else:
continue # this element has no samples
## for each element, generate a table of samples from standards.json, parse and manipulate
## each poorly specified entry in that json file
for i, this in enumerate(data[el.symbol]):
missing = 'present'
if 'missing' in this and this['missing'] is True:
missing = 'missing'
## subscripts for chemical formulas
formula = re.sub(r'(\d+\.\d+|\d+)(?!\+)', r'<sub>\1</sub>', this['material'])
## superscripts for charge state
formula = re.sub(r'((\d+\.\d+|\d+)\+)', r'<sup>\1</sup>', formula)
name = this['name']
if len(name) > 0:
name = name[0].upper() + name[1:]
onrefwheel, location = '', ''
if 'refwheel' in this and this['refwheel'] is True:
location = 'reference wheel'
onrefwheel = '✔'
elif this['location'] == el.symbol:
location = '<span class="cabinet">check sample cabinet</span>'
else:
location = this['location']
if this['incollection'] is False:
location = f'<span class="missing">{self.nosamp}</span>'
ed = edge = 'K'
if el.atomic_number > 46:
edge = 'L<sub>3</sub>'
ed = 'L3'
if 'datafile' not in this:
datafile = ''
elif this['datafile'] is False:
datafile = ''
elif not os.path.isfile(f'Data/{el.symbol}/{this["datafile"]}'):
if this["datafile"] != '':
notfound.append(this["datafile"])
datafile = ""
else:
datafile = f'<a href="https://nsls-ii.github.io/webxdiviewer/#/xdi/{el.symbol}/{this["datafile"]}">'
datafile += f'<img src="xas.svg" width="18" title="Plot {el.symbol} {ed} edge">'
datafile += '</a>'
datafile += f' {edge} : <a href="Data/{el.symbol}/{this["datafile"]}">{this["datafile"]}</a>'
if 'datafile2' not in this:
datafile2 = ''
elif this['datafile2'] is False:
datafile2 = ''
elif not os.path.isfile(f'Data/{el.symbol}/{this["datafile2"]}'):
if this["datafile2"] != '':
notfound.append(this["datafile2"])
datafile2 = ''
else:
datafile2 = f'<br><a href="https://nsls-ii.github.io/webxdiviewer/#/xdi/{el.symbol}/{this["datafile2"]}">'
datafile2 += f'<img src="xas.svg" width="18" title="Plot {el.symbol} L1 edge">'
datafile2 += '</a>'
datafile2 += f' L<sub>1</sub> : <a href="Data/{el.symbol}/{this["datafile2"]}">{this["datafile2"]}</a>'
# if 'datafile' in this and this["datafile"] is not False:
# target = this["datafile"]
# if z < 55:
# plot = f'<a class="noul" href="https://nsls-ii.github.io/webxdiviewer/#/xdi/{el.symbol}/{target}"><section class="element plot">K</section></a>'
# else:
# plot = f'<a class="noul" href="https://nsls-ii.github.io/webxdiviewer/#/xdi/{el.symbol}/{target}"><section class="element plot">L3</section></a>'
# if datafile2 != '':
# target2 = this["datafile2"]
# plot += f'<a class="noul" href="https://nsls-ii.github.io/webxdiviewer/#/xdi/{el.symbol}/{target2}"><section class="element plot">L1</section></a>'
# else:
# plot = ' '
fluo = ''
if 'fluorescence' in this and this['fluorescence'] is True:
fluo = '<span style="font-family: \'Brush Script MT\', cursive;">Fl</span>'
## make the table entry for this sample
page += self.slurp('tmpl/element_entry.tmpl').format(missing = missing,
onrefwheel = onrefwheel,
formula = formula,
name = name,
location = location,
#plot = plot,
fluo = fluo,
datafile = datafile,
datafile2 = datafile2)
page += ' </table>\n'
page += ' </div>\n'
##########
# footer #
##########
page += self.slurp('tmpl/bottom.tmpl')
with open(self.html, 'w') as fh:
fh.write(page)
print(f'\nWrote html to {self.html}')
if len(notfound) > 0:
print('\nMissing or misspelled files:')
for f in notfound:
print(f'\t{f}')
def main():
collection = CommonMaterials()
collection.html = 'BMM-standards.html'
collection.make_html()
if __name__ == "__main__":
main()