-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Series repr html only #29383
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
Series repr html only #29383
Changes from all commits
a771c97
982a9e1
58628e0
73722b8
4bb34fa
a217806
a6e4e89
c08e2ad
b245c30
4672ae5
60ec007
18877d9
446ec80
4d40bb6
a1aac92
f61aed2
49fa29b
7a270cb
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 |
---|---|---|
|
@@ -1576,6 +1576,150 @@ def __repr__(self) -> str: | |
|
||
return result | ||
|
||
def _repr_html_(self): | ||
""" | ||
Return a html representation for a particular DataFrame. | ||
|
||
Mainly for IPython notebook. | ||
""" | ||
# TODO: Full independent HTML generation in SeriesFormatter, rather | ||
# than depending on a limited subset of functionality via to_frame(). | ||
if get_option("display.notebook_repr_html"): | ||
max_rows = get_option("display.max_rows") | ||
min_rows = get_option("display.min_rows") | ||
show_dimensions = get_option("display.show_dimensions") | ||
|
||
formatter = fmt.DataFrameFormatter( | ||
big-o marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.to_frame(), | ||
columns=None, | ||
col_space=None, | ||
na_rep="NaN", | ||
formatters=None, | ||
float_format=None, | ||
sparsify=None, | ||
justify=None, | ||
index_names=True, | ||
header=False, | ||
index=True, | ||
bold_rows=True, | ||
escape=True, | ||
max_rows=max_rows, | ||
min_rows=min_rows, | ||
show_dimensions=False, # We do this later for a series. | ||
decimal=".", | ||
table_id=None, | ||
render_links=False, | ||
) | ||
html = formatter.to_html(notebook=True).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. Is this approach drastically different than what was proposed in #27228 ? 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. Not really, the only difference is in using the Formatter class rather than just invoking it via 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. I think we would prefer to define a SeriesFormatter that inherits a common base class with DataFrameFormatter (callit GenericFromatter). This should't be much more complicated and would be way less fragile than this. |
||
|
||
# Find out where the column ends - we will insert footer information here. | ||
tbl_end = [ | ||
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. @big-o I think would be better on this PR if you can do this inside of DataFrameFormatter in its construction itself, rather than 'finding' then end later. is this possible? 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. Do you mean adding an extra |
||
rownum for (rownum, row) in enumerate(html) if "</table>" in row | ||
][-1] | ||
|
||
footer = [] | ||
if self.name is not None: | ||
footer.append("Name: <b>{name}</b>".format(name=self.name)) | ||
if show_dimensions: | ||
footer.append("Length: {rows}".format(rows=len(self))) | ||
footer.append("dtype: <tt>{dtype}</tt>".format(dtype=self.dtype)) | ||
|
||
html.insert(tbl_end + 1, "<p>{footer}</p>".format(footer=", ".join(footer))) | ||
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. Would this still insert paragraph tags when no footer is required at all? 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. Yes, but a footer will always be required so there's no need to check I've merged in the latest master and all CI checks pass now. |
||
|
||
return "\n".join(html) | ||
else: | ||
return None | ||
|
||
@Substitution( | ||
header_type="bool", | ||
header="Whether to print column labels, default True", | ||
col_space_type="str or int", | ||
col_space="The minimum width of each column in CSS length " | ||
"units. An int is assumed to be px units.\n\n" | ||
" .. versionadded:: 0.25.0\n" | ||
" Ability to use str", | ||
) | ||
@Substitution(shared_params=fmt.common_docstring, returns=fmt.return_docstring) | ||
def to_html( | ||
self, | ||
buf=None, | ||
col_space=None, | ||
header=True, | ||
index=True, | ||
na_rep="NaN", | ||
formatters=None, | ||
float_format=None, | ||
sparsify=None, | ||
index_names=True, | ||
justify=None, | ||
max_rows=None, | ||
show_dimensions=False, | ||
decimal=".", | ||
bold_rows=True, | ||
classes=None, | ||
escape=True, | ||
notebook=False, | ||
border=None, | ||
series_id=None, | ||
render_links=False, | ||
encoding=None, | ||
): | ||
""" | ||
Render a Series as a single-column HTML table. | ||
%(shared_params)s | ||
bold_rows : bool, default True | ||
Make the row labels bold in the output. | ||
classes : str or list or tuple, default None | ||
CSS class(es) to apply to the resulting html table. | ||
escape : bool, default True | ||
Convert the characters <, >, and & to HTML-safe sequences. | ||
notebook : {True, False}, default False | ||
Whether the generated HTML is for IPython Notebook. | ||
border : int | ||
A ``border=border`` attribute is included in the opening | ||
`<table>` tag. Default ``pd.options.display.html.border``. | ||
encoding : str, default "utf-8" | ||
Set character encoding | ||
series_id : str, optional | ||
A css id is included in the opening `<table>` tag if specified. | ||
render_links : bool, default False | ||
Convert URLs to HTML links. | ||
%(returns)s | ||
See Also | ||
-------- | ||
to_string : Convert Series to a string. | ||
""" | ||
|
||
if justify is not None and justify not in fmt._VALID_JUSTIFY_PARAMETERS: | ||
raise ValueError("Invalid value for justify parameter") | ||
|
||
formatter = fmt.SeriesFormatter( | ||
self, | ||
col_space=col_space, | ||
na_rep=na_rep, | ||
formatters=formatters, | ||
float_format=float_format, | ||
sparsify=sparsify, | ||
justify=justify, | ||
index_names=index_names, | ||
header=header, | ||
index=index, | ||
bold_rows=bold_rows, | ||
escape=escape, | ||
max_rows=max_rows, | ||
show_dimensions=show_dimensions, | ||
decimal=decimal, | ||
series_id=series_id, | ||
render_links=render_links, | ||
) | ||
return formatter.to_html( | ||
buf=buf, | ||
classes=classes, | ||
notebook=notebook, | ||
border=border, | ||
encoding=encoding, | ||
) | ||
|
||
def to_string( | ||
self, | ||
buf=None, | ||
|
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.
As a follow up I think should put this in the NDFrame superclass shared by DataFrame and Series rather than copy paste