2015-12-21 93 views
0

我試圖向html輸出一個自定義風格的熊貓數據框。 我無法想象的一個問題是,當代碼在Centos中呈現時,整數呈現爲float。這不會發生在Windows上。在centos和windows之間使用jinja2的不同渲染結果

> /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,但找不到代碼。

+1

您是否在Windows和Linux上使用相同版本的jinja2和Python?如果你沒有綁定到Python 2.6,我會建議至少更新到最新版本的2.7。 – albert

+0

我將Python更改爲2.7版,並安裝了Jinja2 2.8版所需的所有內容。它仍然沒有工作。 – Vasco

回答

0

我找到了解決方法。

通過檢查代碼,我發現給定的模板沒有正確渲染。 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() 

我仍然不知道爲什麼不同的平臺渲染不同。

相關問題