Skip to content

Commit aafba92

Browse files
committed
ENH: Add blocks to Style template
Also make the generated HTML nicer Nicer generated HTML
1 parent ed436b4 commit aafba92

File tree

2 files changed

+55
-41
lines changed

2 files changed

+55
-41
lines changed

pandas/formats/style.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from collections import defaultdict, MutableMapping
1111

1212
try:
13-
from jinja2 import Template, PackageLoader, Environment
13+
from jinja2 import PackageLoader, Environment
1414
except ImportError:
1515
msg = "pandas.Styler requires jinja2. "\
1616
"Please install with `conda install Jinja2`\n"\
@@ -34,7 +34,6 @@
3434
no_mpl_message = "{0} requires matplotlib."
3535

3636

37-
3837
@contextmanager
3938
def _mpl(func):
4039
if has_mpl:
@@ -105,7 +104,8 @@ class Styler(object):
105104
pandas.DataFrame.style
106105
"""
107106
env = Environment(
108-
loader=PackageLoader("pandas", "formats/templates")
107+
loader=PackageLoader("pandas", "formats/templates"),
108+
trim_blocks=True,
109109
)
110110
template = env.get_template("html.tpl")
111111

@@ -355,7 +355,7 @@ def format(self, formatter, subset=None):
355355
self._display_funcs[(i, j)] = formatter
356356
return self
357357

358-
def render(self):
358+
def render(self, **kwargs):
359359
"""
360360
Render the built up styles to HTML
361361
@@ -382,6 +382,7 @@ def render(self):
382382
trimmed = [x for x in d['cellstyle']
383383
if any(any(y) for y in x['props'])]
384384
d['cellstyle'] = trimmed
385+
d.update(kwargs)
385386
return self.template.render(**d)
386387

387388
def _update_ctx(self, attrs):

pandas/formats/templates/html.tpl

+50-37
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,61 @@
1+
{%- block before_style -%}{%- endblock before_style -%}
2+
{% block style %}
13
<style type="text/css" >
4+
{% block table_styles %}
25
{% for s in table_styles %}
36
#T_{{uuid}} {{s.selector}} {
47
{% for p,val in s.props %}
5-
{{p}}: {{val}};
6-
{% endfor %}
8+
{{p}}: {{val}};
9+
{% endfor -%}
710
}
8-
{% endfor %}
9-
{% for s in cellstyle %}
11+
{%- endfor -%}
12+
{% endblock table_styles %}
13+
{% block before_cellstyle %}{% endblock before_cellstyle %}
14+
{% block cellstyle %}
15+
{%- for s in cellstyle %}
1016
#T_{{uuid}}{{s.selector}} {
1117
{% for p,val in s.props %}
1218
{{p}}: {{val}};
1319
{% endfor %}
1420
}
15-
{% endfor %}
21+
{%- endfor -%}
22+
{%- endblock cellstyle %}
1623
</style>
17-
18-
<table id="T_{{uuid}}" {{ table_attributes }}>
19-
{% if caption %}
20-
<caption>{{caption}}</caption>
21-
{% endif %}
22-
23-
<thead>
24-
{% for r in head %}
25-
<tr>
26-
{% for c in r %}
27-
{% if c.is_visible != False %}
28-
<{{c.type}} class="{{c.class}}" {{ c.attributes|join(" ") }}>
29-
{{c.value}}
30-
{% endif %}
31-
{% endfor %}
32-
</tr>
33-
{% endfor %}
34-
</thead>
35-
<tbody>
36-
{% for r in body %}
37-
<tr>
38-
{% for c in r %}
39-
{% if c.is_visible != False %}
40-
<{{c.type}} id="T_{{uuid}}{{c.id}}"
41-
class="{{c.class}}" {{ c.attributes|join(" ") }}>
42-
{{ c.display_value }}
43-
{% endif %}
44-
{% endfor %}
45-
</tr>
46-
{% endfor %}
47-
</tbody>
48-
</table>
24+
{%- endblock style %}
25+
{%- block before_table %}{% endblock before_table %}
26+
{%- block table %}
27+
<table id="T_{{uuid}}" {% if table_attributes %}{{ table_attributes }}{% endif %}>
28+
{%- block caption %}
29+
{%- if caption -%}
30+
<caption>{{caption}}</caption>
31+
{%- endif -%}
32+
{%- endblock caption %}
33+
{%- block head %}
34+
<thead>
35+
{%- for r in head %}
36+
<tr>
37+
{%- for c in r %}
38+
{%- if c.is_visible != False %}
39+
<{{ c.type }} class="{{c.class}}" {{ c.attributes|join(" ") }}>{{c.value}}</{{ c.type }}>
40+
{%- endif %}
41+
{%- endfor %}
42+
</tr>
43+
{%- endfor %}
44+
</thead>
45+
{%- endblock head %}
46+
{%- block body %}
47+
<tbody>
48+
{%- for r in body %}
49+
<tr>
50+
{%- for c in r %}
51+
{%- if c.is_visible != False %}
52+
<{{ c.type }} id="T_{{ uuid }}{{ c.id }}" class="{{ c.class }}" {{ c.attributes|join(" ") }}>{{ c.display_value }}</{{ c.type }}>
53+
{%- endif %}
54+
{%- endfor %}
55+
</tr>
56+
{%- endfor %}
57+
</tbody>
58+
{%- endblock body %}
59+
</table>
60+
{%- endblock table %}
61+
{%- block after_table %}{% endblock after_table %}

0 commit comments

Comments
 (0)