BUG: Styler HTML is unstructured · Issue #39626 · pandas-dev/pandas (original) (raw)


Currently the jinja2 template for Styler rendering HTML leaves unstructured spaces and newlines.

For example:

df = pd.DataFrame('', index=['a', 'b'], columns=['A', 'B'])
print(df.style.applymap(lambda x: 'color:red;', subset=pd.IndexSlice['a', ['B', 'A']])\
              .set_table_styles([{'selector': 'th', 'props': 'color:green;att:bad;'}]).render())
<style  type="text/css" >
    #T_6b11f_ th {
          color: green;
          att: bad;
    }#T_6b11f_row0_col0,#T_6b11f_row0_col1{
            color: red;
        }</style><table id="T_6b11f_" ><thead>    <tr>        <th class="blank level0" ></th>        <th class="col_heading level0 col0" >A</th>        <th class="col_heading level0 col1" >B</th>    </tr></thead><tbody>
                <tr>
                        <th id="T_6b11f_level0_row0" class="row_heading level0 row0" >a</th>
                        <td id="T_6b11f_row0_col0" class="data row0 col0" ></td>
                        <td id="T_6b11f_row0_col1" class="data row0 col1" ></td>
            </tr>
            <tr>
                        <th id="T_6b11f_level0_row1" class="row_heading level0 row1" >b</th>
                        <td id="T_6b11f_row1_col0" class="data row1 col0" ></td>
                        <td id="T_6b11f_row1_col1" class="data row1 col1" ></td>
            </tr>
    </tbody></table>

It would be more suitable for adopt a standard 2 space tabbed indent for new HTML elements (w3 good code standard), to yield for example:

<style type="text/css">
#T_53778_ th {
  color: green;
  att: bad;
}
#T_53778_row0_col0, #T_53778_row0_col1 {
  color: red;
}
</style>
<table id="T_53778_" >
  <thead>
    <tr>
      <th class="blank level0" ></th>
      <th class="col_heading level0 col0" >A</th>
      <th class="col_heading level0 col1" >B</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th id="T_53778_level0_row0" class="row_heading level0 row0" >a</th>
      <td id="T_53778_row0_col0" class="data row0 col0" ></td>
      <td id="T_53778_row0_col1" class="data row0 col1" ></td>
    </tr>
    <tr>
      <th id="T_53778_level0_row1" class="row_heading level0 row1" >b</th>
      <td id="T_53778_row1_col0" class="data row1 col0" ></td>
      <td id="T_53778_row1_col1" class="data row1 col1" ></td>
    </tr>
  </tbody>
</table>