Skip to content

Commit

Permalink
BUG: when rendering dataframe as html do not produce duplicate elemen…
Browse files Browse the repository at this point in the history
  • Loading branch information
ri938 committed Jun 29, 2017
1 parent 664348c commit 938bd0d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ Performance Improvements
Bug Fixes
~~~~~~~~~

- fix issue when rendering dataframe to html where element id's were not unique (:issue:`16780`)

Conversion
^^^^^^^^^^

Expand Down
5 changes: 3 additions & 2 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,14 @@ def format_attr(pair):
for r, idx in enumerate(self.data.index):
row_es = []
for c, value in enumerate(rlabels[r]):
rid = [ROW_HEADING_CLASS, "level%s" % c, "row%s" % r]
es = {
"type": "th",
"is_visible": _is_visible(r, c, idx_lengths),
"value": value,
"display_value": value,
"class": " ".join([ROW_HEADING_CLASS, "level%s" % c,
"row%s" % r]),
"id": "_".join(rid[1:]),
"class": " ".join(rid)
}
rowspan = idx_lengths.get((c, r), 0)
if rowspan > 1:
Expand Down
18 changes: 14 additions & 4 deletions pandas/tests/io/formats/test_style.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import copy
import textwrap
import re

import pytest
import numpy as np
Expand Down Expand Up @@ -504,7 +505,15 @@ def test_uuid(self):
result = styler.set_uuid('aaa')
assert result is styler
assert result.uuid == 'aaa'


def test_unique_id(self):
# See https://github.com/pandas-dev/pandas/issues/16780
df = pd.DataFrame({'a': [1, 3, 5, 6], 'b': [2, 4, 12, 21]})
result = df.style.render(uuid='test')
assert 'test' in result
ids = re.findall('id="(.*?)"', result)
assert np.unique(ids).size == len(ids)

def test_table_styles(self):
style = [{'selector': 'th', 'props': [('foo', 'bar')]}]
styler = Styler(self.df, table_styles=style)
Expand Down Expand Up @@ -719,26 +728,27 @@ def test_mi_sparse(self):
df = pd.DataFrame({'A': [1, 2]},
index=pd.MultiIndex.from_arrays([['a', 'a'],
[0, 1]]))

result = df.style._translate()
body_0 = result['body'][0][0]
expected_0 = {
"value": "a", "display_value": "a", "is_visible": True,
"type": "th", "attributes": ["rowspan=2"],
"class": "row_heading level0 row0",
"class": "row_heading level0 row0", "id": "level0_row0"
}
tm.assert_dict_equal(body_0, expected_0)

body_1 = result['body'][0][1]
expected_1 = {
"value": 0, "display_value": 0, "is_visible": True,
"type": "th", "class": "row_heading level1 row0",
"type": "th", "class": "row_heading level1 row0", "id": "level1_row0"
}
tm.assert_dict_equal(body_1, expected_1)

body_10 = result['body'][1][0]
expected_10 = {
"value": 'a', "display_value": 'a', "is_visible": False,
"type": "th", "class": "row_heading level0 row1",
"type": "th", "class": "row_heading level0 row1", "id": "level0_row1"
}
tm.assert_dict_equal(body_10, expected_10)

Expand Down

0 comments on commit 938bd0d

Please sign in to comment.