[4] | 1 | from __future__ import absolute_import
|
---|
| 2 |
|
---|
| 3 | import collections
|
---|
| 4 | import datetime
|
---|
| 5 | import os
|
---|
| 6 | import shutil
|
---|
| 7 |
|
---|
| 8 | from mako.template import Template
|
---|
| 9 | from mako.lookup import TemplateLookup
|
---|
| 10 |
|
---|
| 11 | class Renderer(object):
|
---|
| 12 | """Class to render results into html.
|
---|
| 13 | """
|
---|
| 14 |
|
---|
| 15 | def __init__(self, result):
|
---|
| 16 | self.result = result
|
---|
| 17 |
|
---|
| 18 | def run(self):
|
---|
| 19 | # make a directory to contain this result
|
---|
| 20 | now = datetime.datetime.today()
|
---|
| 21 | dirname = 'fisica-%s' % (now.strftime('%Y%m%dT%H%M%S'))
|
---|
| 22 | os.mkdir(dirname)
|
---|
| 23 |
|
---|
| 24 | # make a resources directory and copy the Bootstrap stuff there
|
---|
| 25 | shutil.copytree('dist', os.path.join(dirname, 'resources'))
|
---|
| 26 |
|
---|
| 27 | # copy the css file there too
|
---|
| 28 | shutil.copy('bootstrap-3.0.3/examples/navbar-fixed-top/navbar-fixed-top.css', dirname)
|
---|
| 29 |
|
---|
| 30 | # where to find the template files
|
---|
| 31 | mylookup = TemplateLookup(directories=['templates'])
|
---|
| 32 |
|
---|
| 33 | # render the simulation steps first
|
---|
| 34 | for stage in self.result.items():
|
---|
| 35 | if 'substages' in stage[1].keys():
|
---|
| 36 | for substage in stage[1]['substages'].items():
|
---|
| 37 | print 'rendering', stage[0], substage[0]
|
---|
| 38 | context = {'dirname':dirname, 'renderstage':stage[0],
|
---|
| 39 | 'rendersubstage':substage[0], 'data':self.result}
|
---|
| 40 |
|
---|
| 41 | link = os.path.join(context['dirname'], '%s-%s.html' % (stage[0], substage[0]))
|
---|
| 42 | with open(link, 'w') as f:
|
---|
| 43 | template = mylookup.get_template('%s-%s.html' % (stage[0], substage[0]))
|
---|
| 44 | f.write(template.render(**context))
|
---|
| 45 |
|
---|
| 46 | else:
|
---|
| 47 | # no substages
|
---|
| 48 | print 'rendering', stage[0]
|
---|
| 49 | context = {'mylookup':mylookup, 'dirname':dirname,
|
---|
| 50 | 'renderstage':stage[0],
|
---|
| 51 | 'data':self.result}
|
---|
| 52 |
|
---|
| 53 | link = os.path.join(context['dirname'], '%s.html' % (stage[0]))
|
---|
| 54 | with open(link, 'w') as f:
|
---|
| 55 | template = mylookup.get_template('%s.html' % (stage[0]))
|
---|
| 56 | f.write(template.render(**context))
|
---|
| 57 |
|
---|