Skip to content

Commit

Permalink
Fix image sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
pwwang committed Jul 30, 2019
1 parent e69a01c commit 93155e3
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 17 deletions.
54 changes: 43 additions & 11 deletions pyppl-report/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import sys
from pathlib import Path
import yaml
from pyppl.plugin import hookimpl, pypplPostRunFunc
from pyppl.plugin import hookimpl, postrun, addmethod
from pyppl.logger import logger
from pyppl.utils import fs, Box
from pyppl.exception import ProcAttributeError
from cmdy import CmdyReturnCodeException
from .report import Report

__version__ = "0.0.1"

@hookimpl
def setup(config):
config['report'] = ''
config['report'] = ''
config['tplenvs'].update(report = Box(
level = 2,
pre = '',
post = '',
))

@hookimpl
def procSetAttr(proc, name, value):
Expand Down Expand Up @@ -41,7 +47,7 @@ def procSetAttr(proc, name, value):
def procGetAttr(proc, name):
"""Pre-calculate the attribute"""
if name == 'report':
return Path(proc.workdir) / 'proc.report'
return Path(proc.workdir) / 'proc.report.md'

@hookimpl
def procPostRun(proc):
Expand Down Expand Up @@ -69,8 +75,33 @@ def procPostRun(proc):
if datafile.is_file():
data.update(yaml.safe_load(str(datafile)))
rptdata.jobs.append(Box(i = jobdata.i, o = jobdata.o, **data))
proc.report.write_text(report.render(rptdata))

rptenvs = Box(
level = 2,
pre = '',
post = '',
title = proc.desc)
rptenvs.update(proc.tplenvs.get('report', {}))
rptdata.title = rptenvs.title
reportmd = report.render(rptdata).splitlines()

codeblock = False
for i, line in enumerate(reportmd):
if line.startswith('#') and not codeblock:
reportmd[i] = '#' * (rptenvs.level - 1) + line
elif codeblock:
if line.startswith('```') and len(line) - len(line.lstrip('`')) == codeblock:
codeblock = False
elif line.startswith('```'):
codeblock = len(line) - len(line.lstrip('`'))

proc.report.write_text(
proc.template(rptenvs.pre, **proc.tplenvs).render(rptdata) + '\n\n' +
'\n'.join(reportmd) + '\n\n' +
proc.template(rptenvs.post, **proc.tplenvs).render(rptdata) + '\n'
)

@postrun
def pyppl_report(ppl, outfile = None,
title = 'A report generated by pipeline powered by PyPPL.',
standalone = True, template = False, filters = False):
Expand All @@ -85,18 +116,19 @@ def pyppl_report(ppl, outfile = None,
@returns:
(PyPPL): The pipeline object itppl.
"""
outfile = outfile or (Path('.') / Path(sys.artv[0]).stem).with_suffix(
outfile = outfile or (Path('.') / Path(sys.argv[0]).stem).with_suffix(
'%s.report.html' % ('.' + str(ppl.counter) if ppl.counter else ''))
logger.report('Generating report using pandoc ...')
reports = [proc.report for proc in ppl.procs if proc.report.exists()]
cmd = Report(reports, outfile, title).generate(standalone, template, filters)
logger.report('Command: ' + cmd.cmd)
if cmd.rc == 0:
try:
logger.debug('Running: ' + cmd.cmd)
cmd.run()
logger.report('Report generated: ' + str(outfile))
else:
logger.error(cmd.stderr)
except CmdyReturnCodeException as ex:
logger.error(str(ex))
sys.exit(1)

@hookimpl
def pypplRegisterPreRunFunc(ppl):
pypplPostRunFunc(ppl, 'report', pyppl_report)
def pypplInit(ppl):
addmethod(ppl, 'report', pyppl_report)
10 changes: 9 additions & 1 deletion pyppl-report/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,28 @@ def replace(m):
i=index, cite=cite))

def generate(self, standalone, template, filters):
from pyppl import __version__ as pyppl_version
from . import __version__ as report_version
template = template or DEFAULT_TEMPLATE
if template and '/' not in template:
template = RESOURCE_DIR / 'templates' / template / 'standalone.html'
return pandoc(
self.mdfile,
metadata = 'pagetitle="%s"' % self.title,
metadata = [
'pagetitle=%s' % self.title,
'pyppl_version=%s' % pyppl_version,
'report_version=%s' % report_version],
read = 'markdown',
write = 'html',
template = template,
filter = [RESOURCE_DIR / 'filters' / (filt + '.py')
for filt in DEFAULT_FILTERS] + (filters or []),
toc = True,
output = self.outfile,
_raise = True,
_sep = 'auto',
_dupkey = True,
_hold = True,
**{ 'toc-depth': 3,
'self-contained': standalone,
'resource-path': Path(template).parent})
2 changes: 2 additions & 0 deletions pyppl-report/resources/filters/filetable.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def fenced_action(options, data, element, doc):
ncols = len(row)
if has_header:
header = body.pop(0)
if body and len(body[0].content) == len(header.content) + 1:
header.content.insert(0, pf.TableCell(pf.Plain(pf.Str(''))))
elif nrows:
body = body[:nrows]

Expand Down
10 changes: 9 additions & 1 deletion pyppl-report/resources/templates/bootstrap/standalone.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,18 @@
$include-after$
$endfor$
</div>

<div class="page-footer">
<div class="text-sm-right font-weight-lighter font-italic py-1">
Best experience with <a target="_blank" href="https://www.google.com/chrome/">Chrome</a>,
powered by <a target="_blank" href="https://github.com/pwwang/PyPPL">PyPPL</a> v$pyppl_version$ with <a target="_blank" href="https://github.com/pwwang/pyppl-report">pyppl-report</a> v$report_version$
</div>
</div>

</div>
</div>
</div>

</div>
<script src="static/jquery-3.4.1.slim.min.js"></script>
<script src="static/popper.min.js"></script>
<script src="static/bootstrap.min.js"></script>
Expand Down
15 changes: 13 additions & 2 deletions pyppl-report/resources/templates/bootstrap/static/template.css
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ h2[text=Reference] {
}

h3 {
font-size: 1.4rem
font-size: 1.4rem;
margin-top: .8rem;
}

h4 {
Expand All @@ -54,12 +55,22 @@ h4 {

div.nav-tabs a.nav-item h3,
div.nav-tabs a.nav-item h4,
div.nav-tabs a.nav-item h5,
div.card div.card-header h3 button,
div.card div.card-header h4 button {
div.card div.card-header h4 button,
div.card div.card-header h5 button {
font-size: .9rem;
font-weight: bold
}

div.card-header {
padding: .25rem
}

img {
max-width: 80%
}

.page-footer{
font-size: .8rem
}
39 changes: 37 additions & 2 deletions pyppl-report/resources/templates/bootstrap/static/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ var Tab = function(index, wrap) {
tabtitle.replaceWith('')
}
if (tabtitle.length == 0) {
tabtitle = $('<h4>Tab '+ (i+1) +'</h4>')
tabtitle = $(this).children('h5:first')
tabtitle.replaceWith('')
}
if (tabtitle.length == 0) {
tabtitle = $('<h5>Tab '+ (i+1) +'</h5>')
}

tabid = wrapid + '_' + (i + 1)
Expand Down Expand Up @@ -38,7 +42,11 @@ var Collapse = function(index, wrap) {
tabtitle.replaceWith('')
}
if (tabtitle.length == 0) {
tabtitle = $('<h4>Tab '+ (i+1) +'</h4>')
tabtitle = $(this).children('h5:first')
tabtitle.replaceWith('')
}
if (tabtitle.length == 0) {
tabtitle = $('<h5>Tab '+ (i+1) +'</h5>')
}

tabid = wrapid + '_' + (i + 1)
Expand Down Expand Up @@ -81,6 +89,30 @@ var TabWrap = function(selector) {
$("div.tab-wrapper").each(TabType)
};

(function() {
var ev = new $.Event('display'),
orig = $.fn.css;
$.fn.css = function() {
orig.apply(this, arguments);
if ($(this).is('div')) {
$(this).trigger(ev);
}
}
})();

var adjustImage = function() {
$('div.tab :has(img)').on('display', function(){
$(this).find('img').each(function(){
// squared images, too large
if ($(this).height() == $(this).width() && $(this).height() > 200) {
$(this).css('max-width', '50%')
}
})
})
$('div.tab :has(img)').trigger('display')
}


$(document).ready(function () {

$("table").addClass("table table-striped table-sm")
Expand All @@ -91,5 +123,8 @@ $(document).ready(function () {
'href': 'https://scholar.google.com/scholar?q=' + $(this).text()
})
})

TabWrap("div.tab")

adjustImage()
});

0 comments on commit 93155e3

Please sign in to comment.