make_static_page.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import config
4 import os, subprocess
5 
6 import json
7 from time import strftime
8 
9 
10 # Ensure closing tag is written. Don't use for standalone tags like <img>
11 class Tag:
12  # **kwargs seems like a nice idea here. Except "class" is a common thing to
13  # want and is a reserved word :(
14  def __init__(self, fout, tag, args = {}):
15  self._fout = fout
16  self._tag = tag
17  self._args = args
18 
19  def __enter__(self):
20  self._fout.write('<'+self._tag)
21 
22  for key in self._args:
23  self._fout.write(' '+key+'="'+self._args[key]+'"')
24  self._fout.write('>')
25 
26  def __exit__(self, a, b, c):
27  self._fout.write('</'+self._tag+'>')
28 
29 
30 header = '''<!DOCTYPE html>
31 <html><head>
32 <meta charset="UTF-8">
33 <title>NOvA'''+(' PROPOSED ' if config.PROPOSED else ' ')+'''blessed plots</title>
34 
35 <style type="text/css">
36  a:link{color:black}
37  a:visited{color:black}
38  a:hover{color:red}
39  a:active{color:red}
40 
41  .plot
42  {
43  display: inline-block;
44  text-align: center;
45  border: 2px solid gray;
46  background-color: white;
47  padding: 5px;
48  margin: 5px;
49  vertical-align: top
50  }
51  .sublink
52  {
53  display: inline-block;
54  border: 2px solid gray;
55  background-color: white;
56  padding: 5px;
57  margin: 5px;
58  vertical-align: top;
59  width: 400px
60  }
61  .caption
62  {
63  text-align: left;
64  max-width: 400px;
65  }
66 </style>
67 
68 <script>
69 function Hide()
70 {
71  document.getElementById("showbutton").hidden = false;
72  document.getElementById("hidebutton").hidden = true;
73 
74  cs = document.getElementsByClassName("caption")
75  for(var i = 0; i < cs.length; i++){
76  cs[i].hidden = true;
77  }
78 }
79 
80 function Show()
81 {
82  document.getElementById("hidebutton").hidden = false;
83  document.getElementById("showbutton").hidden = true;
84 
85  cs = document.getElementsByClassName("caption")
86  for(var i = 0; i < cs.length; i++){
87  cs[i].hidden = false;
88  }
89 }
90 </script>
91 
92 <script type="text/x-mathjax-config">
93  MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$']]}});
94 </script>
95 <script type="text/javascript"
96  src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
97 </script>
98 
99 </head>
100 <body bgcolor="'''+('#ffffcc' if config.PROPOSED else '#f0f0f0')+'''">
101 '''
102 
103 header2 = '''
104 <div style="position:fixed;right:0;top:0">
105  <button type="button" title="Hide" onclick="Hide()" id="hidebutton">Hide captions</button>
106  <button type="button" title="Show" onclick="Show()" hidden id="showbutton">Show captions</button>
107 </div>
108 '''
109 
110 disclaimer = 'These plots <b>are not yet blessed</b>. Please review them and send comments to the authors. You may only show them publicly once they have passed the review period and migrated to the main blessed plots page.'
111 
112 def footer():
113  return '\n<hr>\n<a href="https://cdcvs.fnal.gov/redmine/projects/novaart/wiki/Where_to_Find_Graphics_for_NOvA_Public_Presentations">Other sources of images</a>\n\n<hr>\nLast updated '+strftime('%Y-%m-%d %H:%M:%S %Z')+'\n</body></html>'
114 
115 
116 def AddFile(fout, deets, f):
117  thumb = 'plots/'+str(deets['id'])+'/thumbs/'+f['base']+'_thumb.png'
118 
119  with Tag(fout, 'span', {'class': 'plot'}):
120  fout.write('<img src="'+thumb+'"><br>\n')
121  for ext in f['exts']:
122  with Tag(fout, 'a', {'href': 'plots/'+str(deets['id'])+'/'+f['base']+'.'+ext}):
123  fout.write('['+ext+']')
124  fout.write('\n')
125 
126  with Tag(fout, 'div', {'class': 'caption'}):
127  fout.write(f['caption'])
128 
129  fout.write('\n\n')
130 
131 
132 page_cfg = json.load(file(config.BLESSED_PLOTS))
133 doc_deets = json.load(file(config.WEB_PATH + config.JSON_FILENAME))
134 
135 allNos = [d['id'] for d in doc_deets]
136 allCatNos = [[n for n in page['docs']] for page in page_cfg]
137 allCatNos = sum(allCatNos, []) # flatten
138 new = [n for n in allNos if n not in allCatNos]
139 
140 # Synthesize a "New" page at the top
141 if len(new) > 0:
142  page_cfg = [{'category': 'New / uncategorized',
143  'docs': new,
144  'caption': 'Plots that have not yet been categorized. Please edit Utilities/BlessedPlots/'+('BlessedPlotsProposed.json' if config.PROPOSED else 'BlessedPlots.json')+' to do so.'}] + page_cfg
145 
146 
147 fout_main = open(config.WEB_PATH + '/index.html', 'w')
148 fout_main.write(header)
149 with Tag(fout_main, 'h1'):
150  fout_main.write('NOvA <em>PROPOSED</em> blessed plots' if config.PROPOSED else 'NOvA blessed plots')
151 
152 if config.PROPOSED:
153  with Tag(fout_main, 'p'):
154  fout_main.write(disclaimer)
155 
156 for page in page_cfg:
157  cat = page['category']
158  safe_cat = cat.replace(' ', '_').replace('/', 'and')
159 
160  with Tag(fout_main, 'span', {'class': 'sublink'}):
161  with Tag(fout_main, 'h3'):
162  with Tag(fout_main, 'a', {'href': safe_cat+'.html'}):
163  fout_main.write(cat)
164 
165  with Tag(fout_main, 'table'):
166  with Tag(fout_main, 'tr'):
167  if 'thumb' in page:
168  with Tag(fout_main, 'td', {'valign': 'top'}):
169  # Extract the height of the thumbnail. Divide by two since we
170  # made a 400px wide thumbnail that we're displaying 200px wide.
171  thumb = config.WEB_PATH+"plots/"+page['thumb'];
172  if os.path.isfile(thumb) :
173  height = subprocess.check_output("identify -format %h "+thumb, shell=True)
174  else:
175  print "Warning: thumbnail " + thumb + " is missing"
176  height="200"
177  fout_main.write('<img src="plots/'+page['thumb']+'" width="200px" height="'+str(int(height)/2)+'px">')
178  if 'caption' in page:
179  with Tag(fout_main, 'td', {'valign': 'top'}):
180  fout_main.write(page['caption'])
181 
182  fout_main.write('\n')
183 
184  fout_sub = open(config.WEB_PATH + '/' + safe_cat+'.html', 'w')
185  fout_sub.write(header)
186  fout_sub.write(header2)
187  with Tag(fout_sub, 'h1'): fout_sub.write(cat)
188 
189  if config.PROPOSED:
190  with Tag(fout_sub, 'p'):
191  fout_sub.write(disclaimer)
192 
193  for docNo in page['docs']:
194  deets = [d for d in doc_deets if d['id'] == docNo]
195  if len(deets) == 0:
196  print 'No info for doc', docNo, 'skipping'
197  continue
198  assert len(deets) == 1
199  deets = deets[0]
200 
201  with Tag(fout_sub, 'h2'):
202  with Tag(fout_sub, 'a', {'href': deets['url']}):
203  fout_sub.write('docdb '+str(docNo))
204  fout_sub.write(' - '+deets['title'])
205 
206  fout_sub.write(' (')
207  # Some authors don't have a firstname (Entire Collaboration), some
208  # have accents in their names (which we probably mangle, but better
209  # than crashing).
210  auths = [(a['firstname'] or '')+' '+(a['lastname'] or '').encode('ascii', 'ignore') for a in deets['authors']]
211  fout_sub.write(', '.join(auths))
212  fout_sub.write(')')
213 
214  for f in deets['files']:
215  AddFile(fout_sub, deets, f)
216 
217  fout_sub.write(footer())
218 
219 fout_main.write(footer())
def __exit__(self, a, b, c)
ps_atom_t encode(std::string const &)
def AddFile(fout, deets, f)
def __init__(self, fout, tag, args={})
procfile open("FD_BRL_v0.txt")
TFile * file
Definition: cellShifts.C:17
Double_t sum
Definition: plot.C:31