Skip to content
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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions src/arche/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
SH_URL = "https://app.scrapinghub.com/p" # noqa

from arche.arche import Arche
from arche.tools import dataframe
from arche.tools.schema import basic_json_schema
import numpy as np
import pandas as pd
import plotly.io as pio

pd.DataFrame._repr_html_ = dataframe._repr_html_
pd.set_option("display.max_colwidth", -1)
pd.set_option("display.render_links", True)

pio.renderers.default = "notebook_connected+jupyterlab"

__all__ = ["basic_json_schema", "Arche", "np", "pd"]
Expand Down
65 changes: 65 additions & 0 deletions src/arche/tools/dataframe.py
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"&lt;", 1)
val = val.replace(">", r"&gt;", 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()
Copy link
Contributor

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?

else:
return None
36 changes: 36 additions & 0 deletions tests/tools/test_dataframe_html_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import pandas as pd
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be consistent with test files naming - tests/tools/test_dataframe.py

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"]}
Copy link
Contributor

Choose a reason for hiding this comment

The 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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The convention we use is test_function\class_description.

Like test_df_repr_html_true

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):
Copy link
Contributor

Choose a reason for hiding this comment

The 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>&lt;class 'pandas.core.frame.DataFrame'&gt;\n" in html
assert "<a href=" not in html