Skip to content

Commit 385e88f

Browse files
committed
Changes based on PR feedback
- urls_as_links -> render_links - Remove global default for render_links - Add 'target="_blank"' to links in generated HTML - Remove unicode string - Parameterize render_links test - Add versionadded tag to render_links on DataFrame
1 parent 4b57892 commit 385e88f

File tree

5 files changed

+48
-99
lines changed

5 files changed

+48
-99
lines changed

pandas/core/config_init.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,6 @@ def use_numexpr_cb(key):
215215
(default: True)
216216
"""
217217

218-
pc_html_urls_as_links_doc = """\
219-
: boolean
220-
When True, DataFrame's to_html() will output cells that appear to contain
221-
a URL with a link to that URL.
222-
(default: False)
223-
"""
224-
225218
pc_width_doc = """
226219
: int
227220
Width of the display in characters. In case python/IPython is running in
@@ -378,8 +371,6 @@ def table_schema_cb(key):
378371
validator=is_int)
379372
cf.register_option('html.use_mathjax', True, pc_html_use_mathjax_doc,
380373
validator=is_bool)
381-
cf.register_option('html.urls_as_links', False, pc_html_urls_as_links_doc,
382-
validator=is_bool)
383374

384375
with cf.config_prefix('html'):
385376
cf.register_option('border', 1, pc_html_border_doc,

pandas/core/frame.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2086,7 +2086,7 @@ def to_html(self, buf=None, columns=None, col_space=None, header=True,
20862086
sparsify=None, index_names=True, justify=None, bold_rows=True,
20872087
classes=None, escape=True, max_rows=None, max_cols=None,
20882088
show_dimensions=False, notebook=False, decimal='.',
2089-
border=None, table_id=None, urls_as_links=None):
2089+
border=None, table_id=None, render_links=False):
20902090
"""
20912091
Render a DataFrame as an HTML table.
20922092
@@ -2115,9 +2115,11 @@ def to_html(self, buf=None, columns=None, col_space=None, header=True,
21152115
21162116
.. versionadded:: 0.23.0
21172117
2118-
urls_as_links : boolean, default False
2118+
render_links : boolean, default False
21192119
Convert URLs to HTML links
21202120
2121+
.. versionadded:: 0.24.0
2122+
21212123
%(returns)s
21222124
21232125
See Also
@@ -2129,9 +2131,6 @@ def to_html(self, buf=None, columns=None, col_space=None, header=True,
21292131
justify not in fmt._VALID_JUSTIFY_PARAMETERS):
21302132
raise ValueError("Invalid value for justify parameter")
21312133

2132-
if urls_as_links is None:
2133-
urls_as_links = get_option("display.html.urls_as_links")
2134-
21352134
formatter = fmt.DataFrameFormatter(self, buf=buf, columns=columns,
21362135
col_space=col_space, na_rep=na_rep,
21372136
formatters=formatters,
@@ -2144,7 +2143,7 @@ def to_html(self, buf=None, columns=None, col_space=None, header=True,
21442143
max_cols=max_cols,
21452144
show_dimensions=show_dimensions,
21462145
decimal=decimal, table_id=table_id,
2147-
urls_as_links=urls_as_links)
2146+
render_links=render_links)
21482147
# TODO: a generic formatter wld b in DataFrameFormatter
21492148
formatter.to_html(classes=classes, notebook=notebook, border=border)
21502149

pandas/io/formats/format.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ def __init__(self, frame, buf=None, columns=None, col_space=None,
381381
justify=None, float_format=None, sparsify=None,
382382
index_names=True, line_width=None, max_rows=None,
383383
max_cols=None, show_dimensions=False, decimal='.',
384-
table_id=None, urls_as_links=False, **kwds):
384+
table_id=None, render_links=False, **kwds):
385385
self.frame = frame
386386
if buf is not None:
387387
self.buf = _expand_user(_stringify_path(buf))
@@ -408,7 +408,7 @@ def __init__(self, frame, buf=None, columns=None, col_space=None,
408408
len(self.frame))
409409
self.show_dimensions = show_dimensions
410410
self.table_id = table_id
411-
self.urls_as_links = urls_as_links
411+
self.render_links = render_links
412412

413413
if justify is None:
414414
self.justify = get_option("display.colheader_justify")
@@ -735,7 +735,7 @@ def to_html(self, classes=None, notebook=False, border=None):
735735
notebook=notebook,
736736
border=border,
737737
table_id=self.table_id,
738-
urls_as_links=self.urls_as_links)
738+
render_links=self.render_links)
739739
if hasattr(self.buf, 'write'):
740740
html_renderer.write_result(self.buf)
741741
elif isinstance(self.buf, compat.string_types):

pandas/io/formats/html.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class HTMLFormatter(TableFormatter):
2828

2929
def __init__(self, formatter, classes=None, max_rows=None, max_cols=None,
3030
notebook=False, border=None, table_id=None,
31-
urls_as_links=False):
31+
render_links=False):
3232
self.fmt = formatter
3333
self.classes = classes
3434

@@ -48,7 +48,7 @@ def __init__(self, formatter, classes=None, max_rows=None, max_cols=None,
4848
border = get_option('display.html.border')
4949
self.border = border
5050
self.table_id = table_id
51-
self.urls_as_links = urls_as_links
51+
self.render_links = render_links
5252

5353
def write(self, s, indent=0):
5454
rs = pprint_thing(s)
@@ -80,14 +80,15 @@ def _write_cell(self, s, kind='td', indent=0, tags=None):
8080

8181
rs = pprint_thing(s, escape_chars=esc).strip()
8282

83-
if self.urls_as_links and _is_url(rs):
83+
if self.render_links and _is_url(rs):
8484
rs_unescaped = pprint_thing(s, escape_chars={}).strip()
85-
start_tag += '<a href="{url}">'.format(url=rs_unescaped)
85+
start_tag += '<a href="{url}" target="_blank">'\
86+
.format(url=rs_unescaped)
8687
end_a = '</a>'
8788
else:
8889
end_a = ''
8990

90-
self.write(u'{start}{rs}{end_a}</{kind}>'
91+
self.write('{start}{rs}{end_a}</{kind}>'
9192
.format(start=start_tag, rs=rs, end_a=end_a, kind=kind),
9293
indent)
9394

pandas/tests/io/formats/test_to_html.py

Lines changed: 34 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,15 +1909,11 @@ def test_to_html_with_id(self):
19091909
result = df.to_html(index_names=False, table_id="TEST_ID")
19101910
assert ' id="TEST_ID"' in result
19111911

1912-
def test_to_html_urls_as_links(self):
1912+
@pytest.mark.parametrize("render_links", [True, False])
1913+
def test_to_html_render_links(self, render_links):
19131914
# GH 2679
19141915

19151916
data = [
1916-
{
1917-
'foo': 0,
1918-
'bar': 'http://pandas.pydata.org/',
1919-
None: 'pydata.org',
1920-
},
19211917
{
19221918
'foo': 0,
19231919
'bar': 'http://pandas.pydata.org/?q1=a&q2=b',
@@ -1931,76 +1927,38 @@ def test_to_html_urls_as_links(self):
19311927
]
19321928
df = DataFrame(data, columns=['foo', 'bar', None],
19331929
index=range(len(data)))
1934-
result_no_links = df.to_html()
1935-
result_with_links = df.to_html(urls_as_links=True)
1936-
expected_no_links = """\
1937-
<table border="1" class="dataframe">
1938-
<thead>
1939-
<tr style="text-align: right;">
1940-
<th></th>
1941-
<th>foo</th>
1942-
<th>bar</th>
1943-
<th>None</th>
1944-
</tr>
1945-
</thead>
1946-
<tbody>
1947-
<tr>
1948-
<th>0</th>
1949-
<td>0</td>
1950-
<td>http://pandas.pydata.org/</td>
1951-
<td>pydata.org</td>
1952-
</tr>
1953-
<tr>
1954-
<th>1</th>
1955-
<td>0</td>
1956-
<td>http://pandas.pydata.org/?q1=a&amp;q2=b</td>
1957-
<td>pydata.org</td>
1958-
</tr>
1959-
<tr>
1960-
<th>2</th>
1961-
<td>0</td>
1962-
<td>www.pydata.org</td>
1963-
<td>pydata.org</td>
1964-
</tr>
1965-
</tbody>
1966-
</table>"""
1967-
expected_with_links = """\
1968-
<table border="1" class="dataframe">
1969-
<thead>
1970-
<tr style="text-align: right;">
1971-
<th></th>
1972-
<th>foo</th>
1973-
<th>bar</th>
1974-
<th>None</th>
1975-
</tr>
1976-
</thead>
1977-
<tbody>
1978-
<tr>
1979-
<th>0</th>
1980-
<td>0</td>
1981-
<td><a href="http://pandas.pydata.org/">http://pandas.pydata.org/</a></td>
1982-
<td>pydata.org</td>
1983-
</tr>
1984-
<tr>
1985-
<th>1</th>
1986-
<td>0</td>
1987-
<td><a href="http://pandas.pydata.org/?q1=a&q2=b">http://pandas.pydata.org/?q1=a&amp;q2=b</a></td>
1988-
<td>pydata.org</td>
1989-
</tr>
1990-
<tr>
1991-
<th>2</th>
1992-
<td>0</td>
1993-
<td>www.pydata.org</td>
1994-
<td>pydata.org</td>
1995-
</tr>
1996-
</tbody>
1997-
</table>"""
19981930

1999-
assert result_with_links == expected_with_links
2000-
assert result_no_links == expected_no_links
1931+
result = df.to_html(render_links=render_links)
1932+
1933+
open_tag = not render_links and '<td>' or\
1934+
('<td><a href="http://pandas.pydata.org/?q1=a&q2=b"'
1935+
' target="_blank">')
1936+
close_tag = not render_links and '</td>' or '</a></td>'
20011937

2002-
pd.options.display.html.urls_as_links = True
2003-
result_no_option_specified = df.to_html()
2004-
pd.options.display.html.urls_as_links = False
1938+
expected = dedent("""\
1939+
<table border="1" class="dataframe">
1940+
<thead>
1941+
<tr style="text-align: right;">
1942+
<th></th>
1943+
<th>foo</th>
1944+
<th>bar</th>
1945+
<th>None</th>
1946+
</tr>
1947+
</thead>
1948+
<tbody>
1949+
<tr>
1950+
<th>0</th>
1951+
<td>0</td>
1952+
{open_tag}http://pandas.pydata.org/?q1=a&amp;q2=b{close_tag}
1953+
<td>pydata.org</td>
1954+
</tr>
1955+
<tr>
1956+
<th>1</th>
1957+
<td>0</td>
1958+
<td>www.pydata.org</td>
1959+
<td>pydata.org</td>
1960+
</tr>
1961+
</tbody>
1962+
</table>""").format(open_tag=open_tag, close_tag=close_tag)
20051963

2006-
assert result_no_option_specified == expected_with_links
1964+
assert result == expected

0 commit comments

Comments
 (0)