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])