-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Add option for DataFrame.to_html() to render URL data as links (#2679) #23715
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
Changes from 20 commits
0702824
61c22a3
67cfe5f
793006d
ccffd5f
13e20ea
db31c1d
13bbca4
61737ed
a59bc4f
b9e5aa7
14b79e1
dd83afa
2f02e28
7ecbeb6
7e0de08
8683d56
7b5b323
505a2f0
05f17f2
bb637a8
c01306d
78ed4b9
9f3e058
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2604,6 +2604,28 @@ table CSS classes. Note that these classes are *appended* to the existing | |
|
||
print(df.to_html(classes=['awesome_table_class', 'even_more_awesome_class'])) | ||
|
||
The ``render_links`` argument provides the ability to add hyperlinks to cells | ||
that contain URLs. | ||
|
||
.. versionadded:: 0.24 | ||
|
||
.. ipython:: python | ||
|
||
url_df = pd.DataFrame({ | ||
'name': ['Python', 'Pandas'], | ||
'url': ['https://www.python.org/', 'http://pandas.pydata.org']}) | ||
print(url_df.to_html(render_links=True)) | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. ipython:: python | ||
:suppress: | ||
|
||
write_html(url_df, 'render_links', render_links=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is this for? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This runs the write_html() call without including its input or output in the notebook cells that appear in the doc. This makes available the static file rendered on line 2625. I copied a pattern used elsewhere in this file, example "bold_rows" section, starting line 2584. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the downside of just printing it? I think creating files like this is hard to keep track of within our codebase so less than ideal There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The generated HTML is also printed in a notebook cell. The lines pointed out by @jreback write that HTML to a separate file that can be rendered. Here's what it looks like after building: That said, if having the rendered sample is not worth the overhead of the extra file, I'm happy to remove. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm i am not sure we actually need to show the html, showing the rendered version is enough. but since we are doing it for all of these, ok |
||
|
||
HTML: | ||
|
||
.. raw:: html | ||
:file: _static/render_links.html | ||
|
||
Finally, the ``escape`` argument allows you to control whether the | ||
"<", ">" and "&" characters escaped in the resulting HTML (by default it is | ||
``True``). So to get the HTML without escaped characters pass ``escape=False`` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ New features | |
- :meth:`DataFrame.corr` and :meth:`Series.corr` now accept a callable for generic calculation methods of correlation, e.g. histogram intersection (:issue:`22684`) | ||
- :func:`DataFrame.to_string` now accepts ``decimal`` as an argument, allowing the user to specify which decimal separator should be used in the output. (:issue:`23614`) | ||
- :func:`DataFrame.read_feather` now accepts ``columns`` as an argument, allowing the user to specify which columns should be read. (:issue:`24025`) | ||
- :func:`DataFrame.to_html` now accepts ``render_links`` as an argument, allowing the user to generate HTML with links to any URLs that appear in the DataFrame. (:issue:`2679`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add a ref to the new docs that you added. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need a link here or just a mention of the addition to docs? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you add a link I can approve and merge There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
.. _whatsnew_0240.values_api: | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2044,8 +2044,8 @@ def to_html(self, buf=None, columns=None, col_space=None, header=True, | |
index=True, na_rep='NaN', formatters=None, float_format=None, | ||
sparsify=None, index_names=True, justify=None, max_rows=None, | ||
max_cols=None, show_dimensions=False, decimal='.', | ||
bold_rows=True, classes=None, escape=True, | ||
notebook=False, border=None, table_id=None): | ||
bold_rows=True, classes=None, escape=True, notebook=False, | ||
border=None, table_id=None, render_links=False): | ||
""" | ||
Render a DataFrame as an HTML table. | ||
%(shared_params)s | ||
|
@@ -2067,6 +2067,12 @@ def to_html(self, buf=None, columns=None, col_space=None, header=True, | |
A css id is included in the opening `<table>` tag if specified. | ||
|
||
.. versionadded:: 0.23.0 | ||
|
||
render_links : boolean, default False | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think our standard is for bool, @datapythonista should probably validate this generally as I seem mixed usage all over the place. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, we still need to fix many cases before we can validate this, but if you run |
||
Convert URLs to HTML links. | ||
|
||
gfyoung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.. versionadded:: 0.24.0 | ||
|
||
%(returns)s | ||
See Also | ||
-------- | ||
|
@@ -2088,7 +2094,8 @@ def to_html(self, buf=None, columns=None, col_space=None, header=True, | |
max_rows=max_rows, | ||
max_cols=max_cols, | ||
show_dimensions=show_dimensions, | ||
decimal=decimal, table_id=table_id) | ||
decimal=decimal, table_id=table_id, | ||
render_links=render_links) | ||
# TODO: a generic formatter wld b in DataFrameFormatter | ||
formatter.to_html(classes=classes, notebook=notebook, border=border) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<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/?q1=a&q2=b</td> | ||
<td>pydata.org</td> | ||
</tr> | ||
<tr> | ||
<th>1</th> | ||
<td>0</td> | ||
<td>www.pydata.org</td> | ||
<td>pydata.org</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add a linefeed here (and on the one below) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<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/?q1=a&q2=b" target="_blank">http://pandas.pydata.org/?q1=a&q2=b</a></td> | ||
<td>pydata.org</td> | ||
</tr> | ||
<tr> | ||
<th>1</th> | ||
<td>0</td> | ||
<td>www.pydata.org</td> | ||
<td>pydata.org</td> | ||
</tr> | ||
</tbody> | ||
</table> |
Uh oh!
There was an error while loading. Please reload this page.