centos 和 windows 之间使用 jinja2 的不同渲染结果
different rendering results using jinja2 between centos and windows
我正在尝试将自定义样式的 pandas 数据帧输出到 html。
我想不通的一个问题是,当代码在 Centos 中呈现时,整数会呈现为浮点数。这不会发生在 windows.
> /usr/local/lib64/python2.6/site-packages/pandas/core/style.py(267)render()
266 any(any(y) for y in x['props'])]
--> 267 d['cellstyle'] = trimmed
268 return self.template.render(**d)
ipdb> n
> /usr/local/lib64/python2.6/site-packages/pandas/core/style.py(268)render()
267 d['cellstyle'] = trimmed
--> 268 return self.template.render(**d)
269
ipdb> s
--Call--
> /usr/local/lib/python2.6/site-packages/jinja2/environment.py(974)render()
973
--> 974 def render(self, *args, **kwargs):
975 """This method accepts the same arguments as the `dict` constructor:
ipdb> n
> /usr/local/lib/python2.6/site-packages/jinja2/environment.py(984)render()
983 """
--> 984 vars = dict(*args, **kwargs)
985 try:
ipdb>
> /usr/local/lib/python2.6/site-packages/jinja2/environment.py(985)render()
984 vars = dict(*args, **kwargs)
--> 985 try:
986 return concat(self.root_render_func(self.new_context(vars)))
ipdb>
> /usr/local/lib/python2.6/site-packages/jinja2/environment.py(986)render()
985 try:
--> 986 return concat(self.root_render_func(self.new_context(vars)))
987 except Exception:
ipdb> for i in self.root_render_func(self.new_context(vars)): print i
产出
<meta charset="utf-8">
<style type="text/css" >
#T_685db4be_a7ba_11e5_9fb7_0ad4bed99552 th {
text-align: center;
}
#T_685db4be_a7ba_11e5_9fb7_0ad4bed99552row0_col0 {
text-align: right;
}
#T_685db4be_a7ba_11e5_9fb7_0ad4bed99552row0_col1 {
text-align: right;
}
#T_685db4be_a7ba_11e5_9fb7_0ad4bed99552row1_col0 {
text-align: right;
}
#T_685db4be_a7ba_11e5_9fb7_0ad4bed99552row1_col1 {
text-align: right;
}
#T_685db4be_a7ba_11e5_9fb7_0ad4bed99552row2_col0 {
text-align: right;
}
#T_685db4be_a7ba_11e5_9fb7_0ad4bed99552row2_col1 {
text-align: right;
}
</style>
<table id="T_685db4be_a7ba_11e5_9fb7_0ad4bed99552" None>
<thead>
<tr>
<th class="blank">
<th class="col_heading level0 col0">id
<th class="col_heading level0 col1">count
</tr>
</thead>
<tbody>
<tr>
<th id="T_685db4be_a7ba_11e5_9fb7_0ad4bed99552" class="row_heading level1 row0">
0.0
<td id="T_685db4be_a7ba_11e5_9fb7_0ad4bed99552row0_col0" class="data row0 col0">
7.0
<td id="T_685db4be_a7ba_11e5_9fb7_0ad4bed99552row0_col1" class="data row0 col1">
2.0
</tr>
<tr>
<th id="T_685db4be_a7ba_11e5_9fb7_0ad4bed99552" class="row_heading level1 row1">
1.0
<td id="T_685db4be_a7ba_11e5_9fb7_0ad4bed99552row1_col0" class="data row1 col0">
56.0
<td id="T_685db4be_a7ba_11e5_9fb7_0ad4bed99552row1_col1" class="data row1 col1">
2.0
</tr>
<tr>
<th id="T_685db4be_a7ba_11e5_9fb7_0ad4bed99552" class="row_heading level1 row2">
2.0
<td id="T_685db4be_a7ba_11e5_9fb7_0ad4bed99552row2_col0" class="data row2 col0">
4.0
<td id="T_685db4be_a7ba_11e5_9fb7_0ad4bed99552row2_col1" class="data row2 col1">
3.0
</tr>
</tbody>
</table>
self.root_render_func 似乎很可疑,因为它的输入没问题:
ipdb> print self.new_context(vars)
<Context {'body': [[{'type': 'th', 'class': 'row_heading level1 row0', 'value': 0}, {'type': 'td', 'class': 'data row0 col0', 'value': 7, 'id': 'row0_col0'}, {'type': 'td', 'class': 'data row0 col1', 'value': 2, 'id': 'row0_col1'}], [{'type': 'th', 'class': 'row_heading level1 row1', 'value': 1}, {'type': 'td', 'class': 'data row1 col0', 'value': 56, 'id': 'row1_col0'}, {'type': 'td', 'class': 'data row1 col1', 'value': 2, 'id': 'row1_col1'}], [{'type': 'th', 'class': 'row_heading level1 row2', 'value': 2}, {'type': 'td', 'class': 'data row2 col0', 'value': 39, 'id': 'row2_col0'}, {'type': 'td', 'class': 'data row2 col1', 'value': 3, 'id': 'row2_col1'}]], 'cellstyle': [{'selector': 'row0_col0', 'props': [['text-align', ' right']]}, {'selector': 'row0_col1', 'props': [['text-align', ' right']]}, {'selector': 'row1_col0', 'props': [['text-align', ' right']]}, {'selector': 'row1_col1', 'props': [['text-align', ' right']]}, {'selector': 'row2_col0', 'props': [['text-align', ' right']]}, {'selector': 'row2_col1', 'props': [['text-align', ' right']]}], 'uuid': '0639c780_a7bc_11e5_adbc_0ad4bed99552', 'table_attributes': None, 'precision': 6, 'lipsum': <function generate_lorem_ipsum at 0x2fb5de8>, 'caption': None, 'range': <type 'xrange'>, 'dict': <type 'dict'>, 'cycler': <class 'jinja2.utils.Cycler'>, 'joiner': <class 'jinja2.utils.Joiner'>, 'head': [[{'type': 'th', 'class': 'blank', 'value': ''}, {'type': 'th', 'class': 'col_heading level0 col0', 'value': u'id'}, {'type': 'th', 'class': 'col_heading level0 col1', 'value': u'count'}]], 'table_styles': [{'props': [('text-align', 'center')], 'selector': 'th'}]} of None>
好的,我的意思是这些值不像 0.0、1.0,它们是整数。
有人知道为什么会这样吗?
我也试图理解 root_render_func 但找不到代码。
我找到了解决方法。
通过检查代码,我发现给定的模板没有正确呈现。 round(precision)
是原因。
因此,我将原始模板稍微更改为"fix"。
具体来说,我改了
{% if c.value is number and c.value|int < c.value %}
{{c.value|round(precision)}}
{% else %}
{{c.value}}
{% endif %}
原来是{% if c.value is number %}
。
from jinja2 import Template
template = Template("""
<style type="text/css" >
{% for s in table_styles %}
#T_{{uuid}} {{s.selector}} {
{% for p,val in s.props %}
{{p}}: {{val}};
{% endfor %}
}
{% endfor %}
{% for s in cellstyle %}
#T_{{uuid}}{{s.selector}} {
{% for p,val in s.props %}
{{p}}: {{val}};
{% endfor %}
}
{% endfor %}
</style>
<table id="T_{{uuid}}" {{ table_attributes }}>
{% if caption %}
<caption>{{caption}}</caption>
{% endif %}
<thead>
{% for r in head %}
<tr>
{% for c in r %}
<{{c.type}} class="{{c.class}}">{{c.value}}
{% endfor %}
</tr>
{% endfor %}
</thead>
<tbody>
{% for r in body %}
<tr>
{% for c in r %}
<{{c.type}} id="T_{{uuid}}{{c.id}}" class="{{c.class}}">
{% if c.value is number and c.value|int < c.value %}
{{c.value|round(precision)}}
{% else %}
{{c.value}}
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
""")
st = pd.core.style.Styler(v1)
st.template = template
st.render()
我仍然不知道为什么不同的平台呈现不同。
我正在尝试将自定义样式的 pandas 数据帧输出到 html。 我想不通的一个问题是,当代码在 Centos 中呈现时,整数会呈现为浮点数。这不会发生在 windows.
> /usr/local/lib64/python2.6/site-packages/pandas/core/style.py(267)render()
266 any(any(y) for y in x['props'])]
--> 267 d['cellstyle'] = trimmed
268 return self.template.render(**d)
ipdb> n
> /usr/local/lib64/python2.6/site-packages/pandas/core/style.py(268)render()
267 d['cellstyle'] = trimmed
--> 268 return self.template.render(**d)
269
ipdb> s
--Call--
> /usr/local/lib/python2.6/site-packages/jinja2/environment.py(974)render()
973
--> 974 def render(self, *args, **kwargs):
975 """This method accepts the same arguments as the `dict` constructor:
ipdb> n
> /usr/local/lib/python2.6/site-packages/jinja2/environment.py(984)render()
983 """
--> 984 vars = dict(*args, **kwargs)
985 try:
ipdb>
> /usr/local/lib/python2.6/site-packages/jinja2/environment.py(985)render()
984 vars = dict(*args, **kwargs)
--> 985 try:
986 return concat(self.root_render_func(self.new_context(vars)))
ipdb>
> /usr/local/lib/python2.6/site-packages/jinja2/environment.py(986)render()
985 try:
--> 986 return concat(self.root_render_func(self.new_context(vars)))
987 except Exception:
ipdb> for i in self.root_render_func(self.new_context(vars)): print i
产出
<meta charset="utf-8">
<style type="text/css" >
#T_685db4be_a7ba_11e5_9fb7_0ad4bed99552 th {
text-align: center;
}
#T_685db4be_a7ba_11e5_9fb7_0ad4bed99552row0_col0 {
text-align: right;
}
#T_685db4be_a7ba_11e5_9fb7_0ad4bed99552row0_col1 {
text-align: right;
}
#T_685db4be_a7ba_11e5_9fb7_0ad4bed99552row1_col0 {
text-align: right;
}
#T_685db4be_a7ba_11e5_9fb7_0ad4bed99552row1_col1 {
text-align: right;
}
#T_685db4be_a7ba_11e5_9fb7_0ad4bed99552row2_col0 {
text-align: right;
}
#T_685db4be_a7ba_11e5_9fb7_0ad4bed99552row2_col1 {
text-align: right;
}
</style>
<table id="T_685db4be_a7ba_11e5_9fb7_0ad4bed99552" None>
<thead>
<tr>
<th class="blank">
<th class="col_heading level0 col0">id
<th class="col_heading level0 col1">count
</tr>
</thead>
<tbody>
<tr>
<th id="T_685db4be_a7ba_11e5_9fb7_0ad4bed99552" class="row_heading level1 row0">
0.0
<td id="T_685db4be_a7ba_11e5_9fb7_0ad4bed99552row0_col0" class="data row0 col0">
7.0
<td id="T_685db4be_a7ba_11e5_9fb7_0ad4bed99552row0_col1" class="data row0 col1">
2.0
</tr>
<tr>
<th id="T_685db4be_a7ba_11e5_9fb7_0ad4bed99552" class="row_heading level1 row1">
1.0
<td id="T_685db4be_a7ba_11e5_9fb7_0ad4bed99552row1_col0" class="data row1 col0">
56.0
<td id="T_685db4be_a7ba_11e5_9fb7_0ad4bed99552row1_col1" class="data row1 col1">
2.0
</tr>
<tr>
<th id="T_685db4be_a7ba_11e5_9fb7_0ad4bed99552" class="row_heading level1 row2">
2.0
<td id="T_685db4be_a7ba_11e5_9fb7_0ad4bed99552row2_col0" class="data row2 col0">
4.0
<td id="T_685db4be_a7ba_11e5_9fb7_0ad4bed99552row2_col1" class="data row2 col1">
3.0
</tr>
</tbody>
</table>
self.root_render_func 似乎很可疑,因为它的输入没问题:
ipdb> print self.new_context(vars)
<Context {'body': [[{'type': 'th', 'class': 'row_heading level1 row0', 'value': 0}, {'type': 'td', 'class': 'data row0 col0', 'value': 7, 'id': 'row0_col0'}, {'type': 'td', 'class': 'data row0 col1', 'value': 2, 'id': 'row0_col1'}], [{'type': 'th', 'class': 'row_heading level1 row1', 'value': 1}, {'type': 'td', 'class': 'data row1 col0', 'value': 56, 'id': 'row1_col0'}, {'type': 'td', 'class': 'data row1 col1', 'value': 2, 'id': 'row1_col1'}], [{'type': 'th', 'class': 'row_heading level1 row2', 'value': 2}, {'type': 'td', 'class': 'data row2 col0', 'value': 39, 'id': 'row2_col0'}, {'type': 'td', 'class': 'data row2 col1', 'value': 3, 'id': 'row2_col1'}]], 'cellstyle': [{'selector': 'row0_col0', 'props': [['text-align', ' right']]}, {'selector': 'row0_col1', 'props': [['text-align', ' right']]}, {'selector': 'row1_col0', 'props': [['text-align', ' right']]}, {'selector': 'row1_col1', 'props': [['text-align', ' right']]}, {'selector': 'row2_col0', 'props': [['text-align', ' right']]}, {'selector': 'row2_col1', 'props': [['text-align', ' right']]}], 'uuid': '0639c780_a7bc_11e5_adbc_0ad4bed99552', 'table_attributes': None, 'precision': 6, 'lipsum': <function generate_lorem_ipsum at 0x2fb5de8>, 'caption': None, 'range': <type 'xrange'>, 'dict': <type 'dict'>, 'cycler': <class 'jinja2.utils.Cycler'>, 'joiner': <class 'jinja2.utils.Joiner'>, 'head': [[{'type': 'th', 'class': 'blank', 'value': ''}, {'type': 'th', 'class': 'col_heading level0 col0', 'value': u'id'}, {'type': 'th', 'class': 'col_heading level0 col1', 'value': u'count'}]], 'table_styles': [{'props': [('text-align', 'center')], 'selector': 'th'}]} of None>
好的,我的意思是这些值不像 0.0、1.0,它们是整数。
有人知道为什么会这样吗?
我也试图理解 root_render_func 但找不到代码。
我找到了解决方法。
通过检查代码,我发现给定的模板没有正确呈现。 round(precision)
是原因。
因此,我将原始模板稍微更改为"fix"。 具体来说,我改了
{% if c.value is number and c.value|int < c.value %}
{{c.value|round(precision)}}
{% else %}
{{c.value}}
{% endif %}
原来是{% if c.value is number %}
。
from jinja2 import Template
template = Template("""
<style type="text/css" >
{% for s in table_styles %}
#T_{{uuid}} {{s.selector}} {
{% for p,val in s.props %}
{{p}}: {{val}};
{% endfor %}
}
{% endfor %}
{% for s in cellstyle %}
#T_{{uuid}}{{s.selector}} {
{% for p,val in s.props %}
{{p}}: {{val}};
{% endfor %}
}
{% endfor %}
</style>
<table id="T_{{uuid}}" {{ table_attributes }}>
{% if caption %}
<caption>{{caption}}</caption>
{% endif %}
<thead>
{% for r in head %}
<tr>
{% for c in r %}
<{{c.type}} class="{{c.class}}">{{c.value}}
{% endfor %}
</tr>
{% endfor %}
</thead>
<tbody>
{% for r in body %}
<tr>
{% for c in r %}
<{{c.type}} id="T_{{uuid}}{{c.id}}" class="{{c.class}}">
{% if c.value is number and c.value|int < c.value %}
{{c.value|round(precision)}}
{% else %}
{{c.value}}
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
""")
st = pd.core.style.Styler(v1)
st.template = template
st.render()
我仍然不知道为什么不同的平台呈现不同。