diff --git a/pandas/core/format.py b/pandas/core/format.py
index 429051b5815fa..5d2009f79fe29 100644
--- a/pandas/core/format.py
+++ b/pandas/core/format.py
@@ -6,6 +6,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,
@@ -307,7 +308,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
@@ -329,6 +331,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")
@@ -821,6 +824,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))
@@ -853,6 +857,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 = '{escaped_url}'.format(url=s,
+ escaped_url=rs)
self.write(
'%s%s%s>' % (start_tag, rs, kind), indent)
diff --git a/pandas/core/frame.py b/pandas/core/frame.py
index ab6f11a4b8d5b..aec047c424748 100644
--- a/pandas/core/frame.py
+++ b/pandas/core/frame.py
@@ -1349,7 +1349,8 @@ 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, bold_rows=True, classes=None, escape=True,
- max_rows=None, max_cols=None, show_dimensions=False):
+ max_rows=None, max_cols=None, show_dimensions=False,
+ urls_as_links=False):
"""
Render a DataFrame as an HTML table.
@@ -1367,6 +1368,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.
"""
@@ -1387,7 +1390,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)
if buf is None:
diff --git a/pandas/tests/test_format.py b/pandas/tests/test_format.py
index 4d21190e7a50d..fc119ba11f919 100644
--- a/pandas/tests/test_format.py
+++ b/pandas/tests/test_format.py
@@ -731,6 +731,77 @@ def test_to_html_multiindex_sparsify_false_multi_sparse(self):
"""
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 = """\
+
+
+
+ |
+ foo |
+ bar |
+ None |
+
+
+
+
+ 0 |
+ 0 |
+ http://pandas.pydata.org/ |
+ pydata.org |
+
+
+ 1 |
+ 0 |
+ http://pandas.pydata.org/?q1=a&q2=b |
+ pydata.org |
+
+
+
"""
+ expected_with_links = """\
+"""
+ 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])