Skip to content

Commit

Permalink
To html encoding add (pandas-dev#28692)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jinyang Zhou authored and proost committed Dec 19, 2019
1 parent 3943edc commit 010c8a2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
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 @@ -109,6 +109,7 @@ Other enhancements
(:issue:`28368`)
- :meth:`DataFrame.to_json` now accepts an ``indent`` integer argument to enable pretty printing of JSON output (:issue:`12004`)
- :meth:`read_stata` can read Stata 119 dta files. (:issue:`28250`)
- Added ``encoding`` argument to :func:`DataFrame.to_html` for non-ascii text (:issue:`28663`)

Build Changes
^^^^^^^^^^^^^
Expand Down
11 changes: 10 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2218,6 +2218,7 @@ def to_html(
border=None,
table_id=None,
render_links=False,
encoding=None,
):
"""
Render a DataFrame as an HTML table.
Expand All @@ -2233,6 +2234,10 @@ def to_html(
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
.. versionadded:: 1.0
table_id : str, optional
A css id is included in the opening `<table>` tag if specified.
Expand Down Expand Up @@ -2274,7 +2279,11 @@ def to_html(
)
# TODO: a generic formatter wld b in DataFrameFormatter
return formatter.to_html(
buf=buf, classes=classes, notebook=notebook, border=border
buf=buf,
classes=classes,
notebook=notebook,
border=border,
encoding=encoding,
)

# ----------------------------------------------------------------------
Expand Down
5 changes: 4 additions & 1 deletion pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,7 @@ def _format_col(self, i: int) -> List[str]:
def to_html(
self,
buf: Optional[FilePathOrBuffer[str]] = None,
encoding: Optional[str] = None,
classes: Optional[Union[str, List, Tuple]] = None,
notebook: bool = False,
border: Optional[int] = None,
Expand All @@ -963,7 +964,9 @@ def to_html(
from pandas.io.formats.html import HTMLFormatter, NotebookFormatter

Klass = NotebookFormatter if notebook else HTMLFormatter
return Klass(self, classes=classes, border=border).get_result(buf=buf)
return Klass(self, classes=classes, border=border).get_result(
buf=buf, encoding=encoding
)

def _get_formatted_column_labels(self, frame: "DataFrame") -> List[List[str]]:
from pandas.core.index import _sparsify
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/io/formats/test_to_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ def test_to_html_unicode(df, expected, datapath):
assert result == expected


def test_to_html_encoding(float_frame, tmp_path):
# GH 28663
path = tmp_path / "test.html"
float_frame.to_html(path, encoding="gbk")
with open(str(path), "r", encoding="gbk") as f:
assert float_frame.to_html() == f.read()


def test_to_html_decimal(datapath):
# GH 12031
df = DataFrame({"A": [6.0, 3.1, 2.2]})
Expand Down

0 comments on commit 010c8a2

Please sign in to comment.