【问题标题】:different rendering results using jinja2 between centos and windowscentos和windows之间使用jinja2渲染结果不同
【发布时间】:2015-12-21 08:35:29
【问题描述】:

我正在尝试将自定义样式的 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 但找不到代码。

【问题讨论】:

  • 你在 Windows 和 Linux 上使用相同版本的 jinja2 和 Python 吗?如果您未绑定到 Python 2.6,我建议您至少更新到 2.7 的最新版本。
  • 我将 Python 更改为 2.7 版,并安装了 Jinja2 2.8 版所需的一切。还是不行。

标签: python pandas jinja2


【解决方案1】:

我找到了解决方法。

通过检查代码,我发现给定的模板没有正确呈现。 round(precision) 是原因。

所以,我稍微更改了原始模板以“修复”它。 具体来说,我改变了

        {% 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()

我还是不明白为什么不同平台的渲染不一样。

【讨论】:

    猜你喜欢
    • 2020-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-23
    • 2011-04-12
    • 2021-12-07
    • 1970-01-01
    • 2017-02-04
    相关资源
    最近更新 更多