-
-
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 all 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 |
---|---|---|
|
@@ -2596,6 +2596,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 |
---|---|---|
@@ -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> |
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.