-
Notifications
You must be signed in to change notification settings - Fork 19
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
Replace DataFrames's default _repr_html_
(closes #76)
#175
base: master
Are you sure you want to change the base?
Changes from all commits
e3c106b
4c756b9
3329217
6b8b2f7
0655121
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from io import StringIO | ||
from typing import Optional | ||
|
||
from pandas._config import get_option, config | ||
from pandas.io.formats import format as fmt | ||
|
||
|
||
pc_render_links_doc = """ | ||
: bool | ||
This sets if URLs in DataFrame should be rendered as clickable anchors. | ||
""" | ||
|
||
# Register `render_links` option | ||
with config.config_prefix("display"): | ||
config.register_option( | ||
"render_links", True, pc_render_links_doc, validator=config.is_bool | ||
) | ||
|
||
|
||
def _repr_html_(self) -> Optional[str]: | ||
""" | ||
Return a html representation for a particular DataFrame. | ||
Mainly for IPython notebook. | ||
""" | ||
if self._info_repr(): | ||
buf = StringIO("") | ||
self.info(buf=buf) | ||
# need to escape the <class>, should be the first line. | ||
val = buf.getvalue().replace("<", r"<", 1) | ||
val = val.replace(">", r">", 1) | ||
return "<pre>" + val + "</pre>" | ||
|
||
if get_option("display.notebook_repr_html"): | ||
max_rows = get_option("display.max_rows") | ||
min_rows = get_option("display.min_rows") | ||
max_cols = get_option("display.max_columns") | ||
show_dimensions = get_option("display.show_dimensions") | ||
render_links = get_option("display.render_links") | ||
|
||
formatter = fmt.DataFrameFormatter( | ||
self, | ||
columns=None, | ||
col_space=None, | ||
na_rep="NaN", | ||
formatters=None, | ||
float_format=None, | ||
sparsify=None, | ||
justify=None, | ||
index_names=True, | ||
header=True, | ||
index=True, | ||
bold_rows=True, | ||
escape=True, | ||
max_rows=max_rows, | ||
min_rows=min_rows, | ||
max_cols=max_cols, | ||
show_dimensions=show_dimensions, | ||
decimal=".", | ||
table_id=None, | ||
render_links=render_links, | ||
) | ||
formatter.to_html(notebook=True) | ||
return formatter.buf.getvalue() | ||
else: | ||
return None |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import pandas as pd | ||
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. To be consistent with test files naming - |
||
import pytest | ||
|
||
|
||
@pytest.fixture() | ||
def df_with_urls(): | ||
pd.set_option("display.notebook_repr_html", True) | ||
data = {"col1": [1, 2], "col2": ["http://foo.com", "https://bar.com"]} | ||
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. Let's add index here too, that's the main use case. |
||
return pd.DataFrame(data) | ||
|
||
|
||
def test_df_has_clickable_urls(df_with_urls): | ||
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 convention we use is Like Does it look better? Mainly it's done for quick search. |
||
html = df_with_urls._repr_html_() | ||
|
||
assert '<a href="http://foo.com" target="_blank">http://foo.com</a>' in html | ||
assert '<a href="https://bar.com" target="_blank">https://bar.com</a>' in html | ||
|
||
|
||
def test_derivaded_df_has_clickable_urls(df_with_urls): | ||
html = df_with_urls.head()._repr_html_() | ||
assert '<a href="http://foo.com" target="_blank">http://foo.com</a>' in html | ||
assert '<a href="https://bar.com" target="_blank">https://bar.com</a>' in html | ||
|
||
|
||
def test_arche_df_does_not_add_links_if_no_url_found(): | ||
df = pd.DataFrame({"col1": [1, 2], "col2": ["foo", "bar"]}) | ||
html = df._repr_html_() | ||
assert "<a href=" not in html | ||
|
||
|
||
def test_large_repr(df_with_urls): | ||
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 am not sure what this test does. Could you please clarify? |
||
df_with_urls._info_repr = lambda: True | ||
html = df_with_urls._repr_html_() | ||
|
||
assert "<pre><class 'pandas.core.frame.DataFrame'>\n" in html | ||
assert "<a href=" not in html |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I compared to the source, trying to find if there's an alternative to this hack and noticed this.
Why
buf.getvalue()
is returned instead of formatted as in the source https://github.com/pandas-dev/pandas/blob/c23649143781c658f792e8f7a5b4368ed01f719c/pandas/core/frame.py#L724?