Skip to content

Commit 698d881

Browse files
committed
ENH: add escape parameter to to_html()
1 parent 02445f4 commit 698d881

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

pandas/core/format.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ def __init__(self, formatter, classes=None):
495495
self.columns = formatter.columns
496496
self.elements = []
497497
self.bold_rows = self.fmt.kwds.get('bold_rows', False)
498+
self.escape = self.fmt.kwds.get('escape', True)
498499

499500
def write(self, s, indent=0):
500501
rs = com.pprint_thing(s)
@@ -517,7 +518,10 @@ def _write_cell(self, s, kind='td', indent=0, tags=None):
517518
else:
518519
start_tag = '<%s>' % kind
519520

520-
esc = {'<' : r'&lt;', '>' : r'&gt;'}
521+
if self.escape:
522+
esc = {'<' : r'&lt;', '>' : r'&gt;', '&' : r'&amp;'}
523+
else:
524+
esc = {}
521525
rs = com.pprint_thing(s, escape_chars=esc)
522526
self.write(
523527
'%s%s</%s>' % (start_tag, rs, kind), indent)

pandas/core/frame.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,13 +1459,15 @@ def to_html(self, buf=None, columns=None, col_space=None, colSpace=None,
14591459
header=True, index=True, na_rep='NaN', formatters=None,
14601460
float_format=None, sparsify=None, index_names=True,
14611461
justify=None, force_unicode=None, bold_rows=True,
1462-
classes=None):
1462+
classes=None, escape=True):
14631463
"""
14641464
to_html-specific options
14651465
bold_rows : boolean, default True
14661466
Make the row labels bold in the output
14671467
classes : str or list or tuple, default None
14681468
CSS class(es) to apply to the resulting html table
1469+
escape : boolean, default True
1470+
Convert the characters <, >, and & to HTML-safe sequences.
14691471
14701472
Render a DataFrame to an html table.
14711473
"""
@@ -1488,7 +1490,8 @@ def to_html(self, buf=None, columns=None, col_space=None, colSpace=None,
14881490
justify=justify,
14891491
index_names=index_names,
14901492
header=header, index=index,
1491-
bold_rows=bold_rows)
1493+
bold_rows=bold_rows,
1494+
escape=escape)
14921495
formatter.to_html(classes=classes)
14931496

14941497
if buf is None:

pandas/tests/test_format.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ def test_to_html_unicode(self):
275275
df.to_html()
276276

277277
def test_to_html_escaped(self):
278-
a = 'str<ing1'
279-
b = 'stri>ng2'
278+
a = 'str<ing1 &amp;'
279+
b = 'stri>ng2 &amp;'
280280

281281
test_dict = {'co<l1': {a: "<type 'str'>",
282282
b: "<type 'str'>"},
@@ -293,19 +293,51 @@ def test_to_html_escaped(self):
293293
</thead>
294294
<tbody>
295295
<tr>
296-
<th>str&lt;ing1</th>
296+
<th>str&lt;ing1 &amp;amp;</th>
297297
<td> &lt;type 'str'&gt;</td>
298298
<td> &lt;type 'str'&gt;</td>
299299
</tr>
300300
<tr>
301-
<th>stri&gt;ng2</th>
301+
<th>stri&gt;ng2 &amp;amp;</th>
302302
<td> &lt;type 'str'&gt;</td>
303303
<td> &lt;type 'str'&gt;</td>
304304
</tr>
305305
</tbody>
306306
</table>"""
307307
self.assertEqual(xp, rs)
308308

309+
def test_to_html_escape_disabled(self):
310+
a = 'str<ing1 &amp;'
311+
b = 'stri>ng2 &amp;'
312+
313+
test_dict = {'co<l1': {a: "<b>bold</b>",
314+
b: "<b>bold</b>"},
315+
'co>l2': {a: "<b>bold</b>",
316+
b: "<b>bold</b>"}}
317+
rs = pd.DataFrame(test_dict).to_html(escape=False)
318+
xp = """<table border="1" class="dataframe">
319+
<thead>
320+
<tr style="text-align: right;">
321+
<th></th>
322+
<th>co<l1</th>
323+
<th>co>l2</th>
324+
</tr>
325+
</thead>
326+
<tbody>
327+
<tr>
328+
<th>str<ing1 &amp;</th>
329+
<td> <b>bold</b></td>
330+
<td> <b>bold</b></td>
331+
</tr>
332+
<tr>
333+
<th>stri>ng2 &amp;</th>
334+
<td> <b>bold</b></td>
335+
<td> <b>bold</b></td>
336+
</tr>
337+
</tbody>
338+
</table>"""
339+
self.assertEqual(xp, rs)
340+
309341
def test_to_html_multiindex_sparsify(self):
310342
index = pd.MultiIndex.from_arrays([[0, 0, 1, 1], [0, 1, 0, 1]],
311343
names=['foo', None])

0 commit comments

Comments
 (0)