Skip to content

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

Closed
wants to merge 18 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ Other enhancements
- Implemented :meth:`pandas.core.window.Window.var` and :meth:`pandas.core.window.Window.std` functions (:issue:`26597`)
- Added ``encoding`` argument to :meth:`DataFrame.to_string` for non-ascii text (:issue:`28766`)
- Added ``encoding`` argument to :func:`DataFrame.to_html` for non-ascii text (:issue:`28663`)
- Added :meth:`Series._repr_html_` to :class:`Series` to provide basic HTML rendering suitable for notebooks (:issue:`5563`).
- :meth:`Styler.background_gradient` now accepts ``vmin`` and ``vmax`` arguments (:issue:`12145`)

Build Changes
Expand Down
144 changes: 144 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Copy link
Member

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

max_rows = get_option("display.max_rows")
min_rows = get_option("display.min_rows")
show_dimensions = get_option("display.show_dimensions")

formatter = fmt.DataFrameFormatter(
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")
Copy link
Member

Choose a reason for hiding this comment

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

Is this approach drastically different than what was proposed in #27228 ?

Copy link
Author

@big-o big-o Nov 5, 2019

Choose a reason for hiding this comment

The 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 to_html. This is to allow support for respecting the min_rows, max_rows and show_dimensions options (which can't be specified in DataFrame.to_html).

Copy link
Contributor

Choose a reason for hiding this comment

The 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 = [
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

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

Do you mean adding an extra footer parameter to either DataFrameFormatter.__init__ or DataFrameFormatter.to_html? Since the DataFrameFormatter isn't limited to single column, data, what should be footer look like for frames with multiple columns? Should there even be one? Should an Exception be raised? Would appreciate some clear guidance on what is expected before I start on another rewrite, as I only have limited time to spend on this.

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)))
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The 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 len(footer) I believe. Just like the string representation, there will always be at least a dtype displayed in the footer. The footer is the main thing that distinguishes between a Series and a single-column DataFrame (the only other difference is the lack of column headers in Series).

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,
Expand Down
Loading