From 99d64da748d4c75310622ea7333d2258091df4f6 Mon Sep 17 00:00:00 2001 From: Daniel Williams Date: Tue, 26 Jan 2016 10:19:22 +0000 Subject: [PATCH] Basic new API implemented, with a few Bootstrap components now available. --- docs/conf.py | 21 ++++++++++++++--- otter/bootstrap.py | 51 ++++++++++++++++++++++++++++++++--------- otter/otter.py | 56 +++++++++++++--------------------------------- 3 files changed, 74 insertions(+), 54 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 5f7e7fe..7a596e8 100755 --- a/docs/conf.py +++ b/docs/conf.py @@ -40,7 +40,7 @@ # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom ones. -extensions = ['sphinx.ext.autodoc', 'sphinx.ext.viewcode'] +extensions = ['sphinx.ext.autodoc', 'sphinx.ext.viewcode', 'numpydoc'] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] @@ -56,7 +56,7 @@ # General information about the project. project = u'Otter' -copyright = u'2015, Daniel WIlliams' +copyright = u'2015, Daniel Williams' # The version info for the project you're documenting, acts as replacement # for |version| and |release|, also used in various other places throughout @@ -111,7 +111,22 @@ # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. -html_theme = 'default' +#html_theme = 'default' + +# -- Options for HTML output ------------------------------------------- + +# The theme to use for HTML and HTML Help pages. See the documentation for +# a list of builtin themes. +#html_theme = 'default' +on_rtd = os.environ.get('READTHEDOCS', None) == 'True' +if not on_rtd: # only import and set the theme if we're building docs locally + import sphinx_rtd_theme + html_theme = 'sphinx_rtd_theme' + html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] + +else: + #sys.path.insert(0, os.path.abspath('../../')) + sys.path.insert(0, os.path.abspath('../otter')) # Theme options are theme-specific and customize the look and feel of a # theme further. For a list of options available for each theme, see the diff --git a/otter/bootstrap.py b/otter/bootstrap.py index c8314a8..f2e6ce2 100644 --- a/otter/bootstrap.py +++ b/otter/bootstrap.py @@ -4,6 +4,7 @@ from itertools import cycle +import markdown class Row(): def __init__(self, cols=1, size='md', hclass=''): @@ -36,24 +37,18 @@ def __init__(self, cols=1, size='md', hclass=''): # We want this to degrade nicely so if there's only one column we interface directly with the row def __add__(self, item): - if len(self.columns==1): + if len(self.columns)==1: self.columns[0] + item else: # Need to throw an error pass - - def __set__(self, item): - if len(self.columns==1): - self.columns[0] = item - else: - # Need to throw an error - pass + def __getitem__(self, i): return self.columns[i] - + def __setitem__(self, i, item): - self.columns[i].content = item + self.columns[i].content = markdown.markdown(item, output_format='xhtml5') def __repr__(self): output = '' @@ -77,7 +72,7 @@ def __iadd__(self, item): return self.content def __add__(self, item): - self.content += str(item) + self.content += markdown.markdown(str(item), output_format='xhtml5') def __repr__(self): output = '' @@ -85,3 +80,37 @@ def __repr__(self): output += self.content output += self.colclose return output + + +class Panel(): + def __init__(self, title='', footer = '', style='default', hclass=''): + self.title = title + self.footer = footer + self.style = style + self.hclass = hclass + + self.panopen = "
" + self.panclose = "
" + + self.panbody = "
{}
" + self.panheader = "
{}
" + self.panfooter = "" + + self.content = '' + + def __iadd__(self, item): + self.__add__(item) + return self.content + + def __add__(self, item): + self.content += markdown.markdown(item, output_format='xhtml5') + + def __repr__(self): + output = '' + output += self.panopen.format(style=self.style, hclass=self.hclass) + if self.panheader != '': output += self.panheader.format(self.title) + output += self.panbody.format(self.content) + if self.footer != '': output += self.panfooter.format(self.footer) + return output + + __str__ = __repr__ diff --git a/otter/otter.py b/otter/otter.py index 3d10edf..bf58c1e 100755 --- a/otter/otter.py +++ b/otter/otter.py @@ -3,6 +3,8 @@ import uuid import os +import ConfigParser + from jinja2 import Template, Environment, FileSystemLoader import matplotlib as mpl @@ -15,7 +17,7 @@ class Otter(): reports for long-running or complex jobs where and iPython notebook would be an impractical way of presenting information. """ - def __init__(self, filename, theme, meta): + def __init__(self, filename, theme, **kwargs): """ An Otter report is created by this class. @@ -27,6 +29,16 @@ def __init__(self, filename, theme, meta): A dictionary of metadata. This is likely to change very soon, but at present a dictionary is required for the title, subtitle, and author's name of the report. """ + # Attempt to load in default meta data from a config file + # At the moment just the current directory, but should + # extend to look in home directory and environment variable location too + + config = ConfigParser.ConfigParser() + config.read('otter.cfg') + self.meta = {} + for option in config.options('meta'): + self.meta[option] = config.get('meta', option) + self.env = Environment(loader=FileSystemLoader(theme)) self.reportfolder = filename+"_files" @@ -34,11 +46,8 @@ def __init__(self, filename, theme, meta): if not os.path.exists(self.reportfolder): os.makedirs(self.reportfolder) self.reportfile= open(filename,"w") - self.meta = meta - + self.meta.update(kwargs) self.items = [] - - self.write_preamble() def __add__(self, item): return self.add(item) @@ -51,7 +60,8 @@ def show(self): html = '' for item in self.items: html += repr(item) - self._write(html) + output_html = self.env.get_template('body.html').render(meta=self.meta, body=html) + self._write(output_html) def _write(self, text): self.reportfile.write(text) @@ -68,30 +78,6 @@ def _mkdir_recursive(self, path): if not os.path.exists(path): os.mkdir(path) - def write_preamble(self): - head = self.env.get_template('head.html') - self._write(head.render(meta=self.meta)) - - def write_footer(self): - footer = self.env.get_template('footer.html') - self._write(footer.render(meta=self.meta)) - self.reportfile.close() - - - def write_page_header(self): - header = self.env.get_template('header.html') - self._write(header.render(meta=self.meta)) - - def write_header(self, level, text): - html_str= """ -
- - {1} - -
- """.format(level,text) - self._write(html_str) - def write_image(self, url): html_str= """
@@ -99,16 +85,6 @@ def write_image(self, url):
""".format(url) self._write(html_str) - - def write_breadcrumb(self, crumbs): - start =""" - """) def write_warning(self, warning, text): html_str = """