Skip to content

Add Series._repr_html_ #27228

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
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1611,6 +1611,32 @@ def __repr__(self):

return result

def _repr_html_(self):
text = self.to_frame()._repr_html_()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than all the head_start, head_stop calculation, can we instead use self.to_frame().to_html(header=False, notebook=True)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Played with this briefly, and it seems to work reasonably well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why we wouldn't want to this be housed in SeriesFormatter (pandas.io.formats.format) instead? I think if we did that could consolidate logic in core (maybe move to generic) and leave dispatching to the actual formatter code


lines = text.split("\n")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Answering a question I had: Does this fail on data with embedded newlines? No, to_html escapes the newlines, so we're OK.

head_start = [
i for i, line in enumerate(lines) if line.strip().startswith("<thead>")
][0]
head_stop = [
i for i, line in enumerate(lines) if line.strip().startswith("</thead>")
][0]
del lines[head_start:head_stop]

tail = """
<tr>
<td> name: </td>
<th> %s </th>
</tr>
""" % str(self.name) or "--"

body_end = [
i for i, line in enumerate(lines) if line.strip().startswith("</tbody>")
][0]
lines[body_end:body_end] = tail.split("\n")

return "\n".join(lines)

def to_string(
self,
buf=None,
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1769,6 +1769,24 @@ def test_repr_html(self, float_frame):

tm.reset_display_options()

def test_series(series):
df = DataFrame({"abc": range(1000)})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to create a smaller series and make a complete assertion about the content?


a = df._repr_html_()
b = df.abc._repr_html_()

assert a != b
assert 0.5 * len(b) < len(a) < 2 * len(b)
assert "abc" in b

s = Series([123, 456], name=(1, 2))
assert "123" in s._repr_html_()
assert "(1, 2)" in s._repr_html_()

lines = s._repr_html_().split('\n')
indents = [line.index("<tr>") for line in lines if "<tr>" in line]
assert len(set(indents)) == 1 # cleanly indented HTML

def test_repr_html_mathjax(self):
df = DataFrame([[1, 2], [3, 4]])
assert "tex2jax_ignore" not in df._repr_html_()
Expand Down