From 698d881ed965d573f20961f0b6af64494ba619a0 Mon Sep 17 00:00:00 2001 From: Garrett Drapala Date: Sat, 23 Feb 2013 16:20:22 -0500 Subject: [PATCH 1/2] ENH: add escape parameter to to_html() --- pandas/core/format.py | 6 +++++- pandas/core/frame.py | 7 +++++-- pandas/tests/test_format.py | 40 +++++++++++++++++++++++++++++++++---- 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/pandas/core/format.py b/pandas/core/format.py index 862b09f5e84e3..89e24fa34f070 100644 --- a/pandas/core/format.py +++ b/pandas/core/format.py @@ -495,6 +495,7 @@ def __init__(self, formatter, classes=None): self.columns = formatter.columns self.elements = [] self.bold_rows = self.fmt.kwds.get('bold_rows', False) + self.escape = self.fmt.kwds.get('escape', True) def write(self, s, indent=0): rs = com.pprint_thing(s) @@ -517,7 +518,10 @@ def _write_cell(self, s, kind='td', indent=0, tags=None): else: start_tag = '<%s>' % kind - esc = {'<' : r'<', '>' : r'>'} + if self.escape: + esc = {'<' : r'<', '>' : r'>', '&' : r'&'} + else: + esc = {} rs = com.pprint_thing(s, escape_chars=esc) self.write( '%s%s' % (start_tag, rs, kind), indent) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 0d7913819f115..091b9926500d9 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1459,13 +1459,15 @@ def to_html(self, buf=None, columns=None, col_space=None, colSpace=None, header=True, index=True, na_rep='NaN', formatters=None, float_format=None, sparsify=None, index_names=True, justify=None, force_unicode=None, bold_rows=True, - classes=None): + classes=None, escape=True): """ to_html-specific options bold_rows : boolean, 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 : boolean, default True + Convert the characters <, >, and & to HTML-safe sequences. Render a DataFrame to an html table. """ @@ -1488,7 +1490,8 @@ def to_html(self, buf=None, columns=None, col_space=None, colSpace=None, justify=justify, index_names=index_names, header=header, index=index, - bold_rows=bold_rows) + bold_rows=bold_rows, + escape=escape) formatter.to_html(classes=classes) if buf is None: diff --git a/pandas/tests/test_format.py b/pandas/tests/test_format.py index 0ae8934c898b0..f013f1a7ca14d 100644 --- a/pandas/tests/test_format.py +++ b/pandas/tests/test_format.py @@ -275,8 +275,8 @@ def test_to_html_unicode(self): df.to_html() def test_to_html_escaped(self): - a = 'str", b: ""}, @@ -293,12 +293,12 @@ def test_to_html_escaped(self): - str<ing1 + str<ing1 &amp; <type 'str'> <type 'str'> - stri>ng2 + stri>ng2 &amp; <type 'str'> <type 'str'> @@ -306,6 +306,38 @@ def test_to_html_escaped(self): """ self.assertEqual(xp, rs) + def test_to_html_escape_disabled(self): + a = 'strbold", + b: "bold"}, + 'co>l2': {a: "bold", + b: "bold"}} + rs = pd.DataFrame(test_dict).to_html(escape=False) + xp = """ + + + + + + + + + + + + + + + + + +
co + co>l2
str + bold bold
stri>ng2 & bold bold
""" + self.assertEqual(xp, rs) + def test_to_html_multiindex_sparsify(self): index = pd.MultiIndex.from_arrays([[0, 0, 1, 1], [0, 1, 0, 1]], names=['foo', None]) From f3d01cb91d11611e7d4f8e9cf906dd5c4856442b Mon Sep 17 00:00:00 2001 From: Garrett Drapala Date: Tue, 9 Apr 2013 09:37:04 -0400 Subject: [PATCH 2/2] DOC: mention new to_html() escape argument and & escaping --- RELEASE.rst | 4 ++++ doc/source/v0.11.0.txt | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/RELEASE.rst b/RELEASE.rst index b7a79e3e24175..11fe5ef1e0860 100644 --- a/RELEASE.rst +++ b/RELEASE.rst @@ -131,6 +131,9 @@ pandas 0.11.0 - Add ``time()`` method to DatetimeIndex (GH3180_) - Return NA when using Series.str[...] for values that are not long enough (GH3223_) + - to_html() now accepts an optional "escape" argument to control reserved + HTML character escaping (enabled by default) and escapes ``&``, in addition + to ``<`` and ``>``. (GH2919_) **API Changes** @@ -390,6 +393,7 @@ pandas 0.11.0 .. _GH3238: https://github.com/pydata/pandas/issues/3238 .. _GH3258: https://github.com/pydata/pandas/issues/3258 .. _GH3283: https://github.com/pydata/pandas/issues/3283 +.. _GH2919: https://github.com/pydata/pandas/issues/2919 pandas 0.10.1 ============= diff --git a/doc/source/v0.11.0.txt b/doc/source/v0.11.0.txt index c6553b909f7a6..1385c217b6550 100644 --- a/doc/source/v0.11.0.txt +++ b/doc/source/v0.11.0.txt @@ -325,6 +325,10 @@ Enhancements - Treat boolean values as integers (values 1 and 0) for numeric operations. (GH2641_) + - to_html() now accepts an optional "escape" argument to control reserved + HTML character escaping (enabled by default) and escapes ``&``, in addition + to ``<`` and ``>``. (GH2919_) + See the `full release notes `__ or issue tracker on GitHub for a complete list. @@ -350,3 +354,4 @@ on GitHub for a complete list. .. _GH3070: https://github.com/pydata/pandas/issues/3070 .. _GH3075: https://github.com/pydata/pandas/issues/3075 .. _GH2641: https://github.com/pydata/pandas/issues/2641 +.. _GH2919: https://github.com/pydata/pandas/issues/2919