Skip to content

ENH: #2679 - DataFrame.to_html() urls_as_links parameter. #10294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion pandas/core/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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 = '<a href="{url}">{escaped_url}</a>'.format(url=s,
escaped_url=rs)
self.write(
'%s%s</%s>' % (start_tag, rs, kind), indent)

Expand Down
8 changes: 6 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.

"""

Expand All @@ -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:
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 @@ -731,6 +731,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