Skip to content

Commit

Permalink
ENH: pandas-dev#2679 - DataFrame.to_html() urls_as_links parameter.
Browse files Browse the repository at this point in the history
New urls_as_links boolean paramater that will output urls as href html
links. ref pandas-dev#2679
  • Loading branch information
lexual committed Nov 12, 2015
1 parent 6c4d2c7 commit 49992e9
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 3 deletions.
11 changes: 10 additions & 1 deletion pandas/core/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from pandas.core.base import PandasObject
from pandas.core.common import adjoin, notnull
from pandas.io.common import _is_url
from pandas.core.index import Index, MultiIndex, _ensure_index
from pandas import compat
from pandas.compat import(StringIO, lzip, range, map, zip, reduce, u,
Expand Down Expand Up @@ -341,7 +342,8 @@ def __init__(self, frame, buf=None, columns=None, col_space=None,
header=True, index=True, na_rep='NaN', formatters=None,
justify=None, float_format=None, sparsify=None,
index_names=True, line_width=None, max_rows=None,
max_cols=None, show_dimensions=False, **kwds):
max_cols=None, show_dimensions=False, urls_as_links=False,
**kwds):
self.frame = frame
self.buf = buf if buf is not None else StringIO()
self.show_index_names = index_names
Expand All @@ -363,6 +365,7 @@ def __init__(self, frame, buf=None, columns=None, col_space=None,
self.max_rows_displayed = min(max_rows or len(self.frame),
len(self.frame))
self.show_dimensions = show_dimensions
self.urls_as_links = urls_as_links

if justify is None:
self.justify = get_option("display.colheader_justify")
Expand Down Expand Up @@ -863,6 +866,7 @@ def __init__(self, formatter, classes=None, max_rows=None, max_cols=None,
self.max_rows = max_rows or len(self.fmt.frame)
self.max_cols = max_cols or len(self.fmt.columns)
self.show_dimensions = self.fmt.show_dimensions
self.urls_as_links = self.fmt.urls_as_links
self.is_truncated = (self.max_rows < len(self.fmt.frame) or
self.max_cols < len(self.fmt.columns))
self.notebook = notebook
Expand Down Expand Up @@ -896,6 +900,11 @@ def _write_cell(self, s, kind='td', indent=0, tags=None):
else:
esc = {}
rs = com.pprint_thing(s, escape_chars=esc).strip()
if self.urls_as_links and isinstance(s, compat.string_types):
s = s.strip()
if _is_url(s):
rs = '<a href="{url}">{escaped_url}</a>'.format(url=s,
escaped_url=rs)
self.write(
'%s%s</%s>' % (start_tag, rs, kind), indent)

Expand Down
7 changes: 5 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1479,7 +1479,7 @@ def to_html(self, buf=None, columns=None, col_space=None, colSpace=None,
float_format=None, sparsify=None, index_names=True,
justify=None, bold_rows=True, classes=None, escape=True,
max_rows=None, max_cols=None, show_dimensions=False,
notebook=False):
notebook=False, urls_as_links=False):
"""
Render a DataFrame as an HTML table.
Expand All @@ -1497,6 +1497,8 @@ def to_html(self, buf=None, columns=None, col_space=None, colSpace=None,
max_cols : int, optional
Maximum number of columns to show before truncating. If None, show
all.
urls_as_links : boolean, default False
Convert urls to HTML links.
"""

Expand All @@ -1517,7 +1519,8 @@ def to_html(self, buf=None, columns=None, col_space=None, colSpace=None,
escape=escape,
max_rows=max_rows,
max_cols=max_cols,
show_dimensions=show_dimensions)
show_dimensions=show_dimensions,
urls_as_links=urls_as_links)
formatter.to_html(classes=classes, notebook=notebook)

if buf is None:
Expand Down
71 changes: 71 additions & 0 deletions pandas/tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,77 @@ def test_to_html_multiindex_sparsify_false_multi_sparse(self):
</table>"""
self.assertEqual(result, expected)

def test_to_html_with_hyperlinks(self):
data = [
{
'foo': 0,
'bar': 'http://pandas.pydata.org/',
None: 'pydata.org',
},
{
'foo': 0,
'bar': 'http://pandas.pydata.org/?q1=a&q2=b',
None: 'pydata.org',
},
]
df = DataFrame(data, columns=['foo', 'bar', None],
index=range(len(data)))

result_no_links = df.to_html()
result_with_links = df.to_html(urls_as_links=True)
expected_no_links = """\
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>foo</th>
<th>bar</th>
<th>None</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>0</td>
<td>http://pandas.pydata.org/</td>
<td>pydata.org</td>
</tr>
<tr>
<th>1</th>
<td>0</td>
<td>http://pandas.pydata.org/?q1=a&amp;q2=b</td>
<td>pydata.org</td>
</tr>
</tbody>
</table>"""
expected_with_links = """\
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>foo</th>
<th>bar</th>
<th>None</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>0</td>
<td><a href="http://pandas.pydata.org/">http://pandas.pydata.org/</a></td>
<td>pydata.org</td>
</tr>
<tr>
<th>1</th>
<td>0</td>
<td><a href="http://pandas.pydata.org/?q1=a&q2=b">http://pandas.pydata.org/?q1=a&amp;q2=b</a></td>
<td>pydata.org</td>
</tr>
</tbody>
</table>"""
self.assertEqual(result_with_links, expected_with_links)
self.assertEqual(result_no_links, expected_no_links)

def test_to_html_multiindex_sparsify(self):
index = MultiIndex.from_arrays([[0, 0, 1, 1], [0, 1, 0, 1]],
names=['foo', None])
Expand Down

0 comments on commit 49992e9

Please sign in to comment.