-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Add Series._repr_html_ #27228
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1611,6 +1611,32 @@ def __repr__(self): | |
|
||
return result | ||
|
||
def _repr_html_(self): | ||
text = self.to_frame()._repr_html_() | ||
|
||
lines = text.split("\n") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, |
||
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, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1769,6 +1769,24 @@ def test_repr_html(self, float_frame): | |
|
||
tm.reset_display_options() | ||
|
||
def test_series(series): | ||
df = DataFrame({"abc": range(1000)}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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_() | ||
|
There was a problem hiding this comment.
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 useself.to_frame().to_html(header=False, notebook=True)
?There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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