Skip to content

Commit ed436b4

Browse files
committed
REF: Move template to its own file
Use Environment and PackageLoader to load it TODO: packaging, __init__.py?
1 parent 7d04391 commit ed436b4

File tree

2 files changed

+54
-51
lines changed

2 files changed

+54
-51
lines changed

pandas/formats/style.py

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

3636

37+
3738
@contextmanager
3839
def _mpl(func):
3940
if has_mpl:
@@ -103,56 +104,10 @@ class Styler(object):
103104
--------
104105
pandas.DataFrame.style
105106
"""
106-
template = Template("""
107-
<style type="text/css" >
108-
{% for s in table_styles %}
109-
#T_{{uuid}} {{s.selector}} {
110-
{% for p,val in s.props %}
111-
{{p}}: {{val}};
112-
{% endfor %}
113-
}
114-
{% endfor %}
115-
{% for s in cellstyle %}
116-
#T_{{uuid}}{{s.selector}} {
117-
{% for p,val in s.props %}
118-
{{p}}: {{val}};
119-
{% endfor %}
120-
}
121-
{% endfor %}
122-
</style>
123-
124-
<table id="T_{{uuid}}" {{ table_attributes }}>
125-
{% if caption %}
126-
<caption>{{caption}}</caption>
127-
{% endif %}
128-
129-
<thead>
130-
{% for r in head %}
131-
<tr>
132-
{% for c in r %}
133-
{% if c.is_visible != False %}
134-
<{{c.type}} class="{{c.class}}" {{ c.attributes|join(" ") }}>
135-
{{c.value}}
136-
{% endif %}
137-
{% endfor %}
138-
</tr>
139-
{% endfor %}
140-
</thead>
141-
<tbody>
142-
{% for r in body %}
143-
<tr>
144-
{% for c in r %}
145-
{% if c.is_visible != False %}
146-
<{{c.type}} id="T_{{uuid}}{{c.id}}"
147-
class="{{c.class}}" {{ c.attributes|join(" ") }}>
148-
{{ c.display_value }}
149-
{% endif %}
150-
{% endfor %}
151-
</tr>
152-
{% endfor %}
153-
</tbody>
154-
</table>
155-
""")
107+
env = Environment(
108+
loader=PackageLoader("pandas", "formats/templates")
109+
)
110+
template = env.get_template("html.tpl")
156111

157112
def __init__(self, data, precision=None, table_styles=None, uuid=None,
158113
caption=None, table_attributes=None):

pandas/formats/templates/html.tpl

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<style type="text/css" >
2+
{% for s in table_styles %}
3+
#T_{{uuid}} {{s.selector}} {
4+
{% for p,val in s.props %}
5+
{{p}}: {{val}};
6+
{% endfor %}
7+
}
8+
{% endfor %}
9+
{% for s in cellstyle %}
10+
#T_{{uuid}}{{s.selector}} {
11+
{% for p,val in s.props %}
12+
{{p}}: {{val}};
13+
{% endfor %}
14+
}
15+
{% endfor %}
16+
</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>

0 commit comments

Comments
 (0)