Skip to content

BUG: Styler HTML is unstructured #39626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3 tasks done
attack68 opened this issue Feb 6, 2021 · 0 comments · Fixed by #39627
Closed
3 tasks done

BUG: Styler HTML is unstructured #39626

attack68 opened this issue Feb 6, 2021 · 0 comments · Fixed by #39627
Labels
Bug IO HTML read_html, to_html, Styler.apply, Styler.applymap Styler conditional formatting using DataFrame.style
Milestone

Comments

@attack68
Copy link
Contributor

attack68 commented Feb 6, 2021

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


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>
@attack68 attack68 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 6, 2021
@jreback jreback added IO HTML read_html, to_html, Styler.apply, Styler.applymap Styler conditional formatting using DataFrame.style and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 7, 2021
@jreback jreback added this to the 1.3 milestone Feb 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO HTML read_html, to_html, Styler.apply, Styler.applymap Styler conditional formatting using DataFrame.style
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants