# HG changeset patch # User Jan Kanis # Date 1403179620 -7200 # Node ID 7d0d46168fd539c85c1f9ae77d652f397c21ecd7 # Parent 4d2c25baf5a3aff771f3b7c9212306b1bb95d892 Format all numbers in a predictable way diff -r 4d2c25baf5a3 -r 7d0d46168fd5 blast2html.html.jinja --- a/blast2html.html.jinja Wed Jun 18 17:50:32 2014 +0200 +++ b/blast2html.html.jinja Thu Jun 19 14:07:00 2014 +0200 @@ -467,7 +467,7 @@
{% for s in result|queryscale %} -
+
{% if s.width > 3.0 %}
{{s.label}}
{% else %} @@ -490,7 +490,7 @@
{% for hit in line.colors %}
+ style="background-color: {{hit[1]}}; width: {{hit[0]|numfmt}}%">
{% endfor %}
diff -r 4d2c25baf5a3 -r 7d0d46168fd5 blast2html.py --- a/blast2html.py Wed Jun 18 17:50:32 2014 +0200 +++ b/blast2html.py Thu Jun 19 14:07:00 2014 +0200 @@ -21,7 +21,7 @@ -_filters = dict(int='int', float='float') +_filters = dict(float='float') def filter(func_or_name): "Decorator to register a function as filter in the current jinja environment" if isinstance(func_or_name, six.string_types): @@ -50,6 +50,21 @@ return format(float(val), fmt) @filter +def numfmt(val): + """Format numbers in decimal notation, but without excessive trailing 0's. + Default python float formatting will use scientific notation for some values, + or append trailing zeros with the 'f' format type, and the number of digits differs + between python 2 and 3.""" + fpart, ipart = math.modf(val) + if fpart == 0: + return str(int(val)) + # round to 10 to get identical representations in python 2 and 3 + s = format(round(val, 10), '.10f').rstrip('0') + if s[-1] == '.': + s += '0' + return s + +@filter def firsttitle(hit): return hit.Hit_def.text.split('>')[0]