Skip to content

Commit

Permalink
Replace DataFrames's default _repr_html_ (closes #76)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcurvelo committed Oct 24, 2019
1 parent 6be3d7a commit e3c106b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/arche/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
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_

pio.renderers.default = "notebook_connected+jupyterlab"

__all__ = ["basic_json_schema", "Arche", "np", "pd"]
Expand Down
13 changes: 13 additions & 0 deletions src/arche/tools/dataframe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import re


def make_urls_clickable(val):
if isinstance(val, str) and re.search("^https?://", val):
return f'<a target="_blank" href="{val}">{val}</a>'
else:
return val


def _repr_html_(self, *args, **kwargs):
styler = self.style.format(make_urls_clickable)
return styler.render(*args, **kwargs)
26 changes: 26 additions & 0 deletions tests/tools/test_dataframe_html_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pandas as pd
import pytest


@pytest.fixture()
def df_with_urls():
data = {"col1": [1, 2], "col2": ["http://foo.com", "https://bar.com"]}
return pd.DataFrame(data)


def test_df_has_clickable_urls(df_with_urls):
html = df_with_urls._repr_html_()
assert '<a target="_blank" href="http://foo.com">http://foo.com</a>' in html
assert '<a target="_blank" href="https://bar.com">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 target="_blank" href="http://foo.com">http://foo.com</a>' in html
assert '<a target="_blank" href="https://bar.com">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

0 comments on commit e3c106b

Please sign in to comment.