Skip to content

Commit

Permalink
Move widget display to Out[...] (#308)
Browse files Browse the repository at this point in the history
* fix repr to display string of df

* replace repr_html to move widget into Out

* replace repr_html in other files

* fix issue for LuxSeries
  • Loading branch information
westernguy2 committed Mar 24, 2021
1 parent 2553125 commit 955a4a1
Show file tree
Hide file tree
Showing 21 changed files with 167 additions and 166 deletions.
6 changes: 1 addition & 5 deletions lux/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,6 @@ def current_vis(self):
def current_vis(self, current_vis: Dict):
self._current_vis = current_vis

def __repr__(self):
# TODO: _repr_ gets called from _repr_html, need to get rid of this call
return ""

#######################################################
########## SQL Metadata, type, model schema ###########
#######################################################
Expand Down Expand Up @@ -619,7 +615,7 @@ def set_intent_on_click(self, change):
self._widget.observe(self.remove_deleted_recs, names="deletedIndices")
self._widget.observe(self.set_intent_on_click, names="selectedIntentIndex")

def _repr_html_(self):
def _ipython_display_(self):
from IPython.display import display
from IPython.display import clear_output
import ipywidgets as widgets
Expand Down
3 changes: 1 addition & 2 deletions lux/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def to_pandas(self) -> pd.Series:

return lux.core.originalSeries(self, copy=False)

def __repr__(self):
def _ipython_display_(self):
from IPython.display import display
from IPython.display import clear_output
import ipywidgets as widgets
Expand Down Expand Up @@ -188,7 +188,6 @@ def on_button_clicked(b):
)
warnings.warn(traceback.format_exc())
display(self.to_pandas())
return ""

@property
def recommendation(self):
Expand Down
2 changes: 1 addition & 1 deletion lux/vis/Vis.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def set_intent(self, intent: List[Clause]) -> None:
self._intent = intent
self.refresh_source(self._source)

def _repr_html_(self):
def _ipython_display_(self):
from IPython.display import display

check_import_lux_widget()
Expand Down
2 changes: 1 addition & 1 deletion lux/vis/VisList.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def normalize_score(self, invert_order=False):
if invert_order:
dobj.score = 1 - dobj.score

def _repr_html_(self):
def _ipython_display_(self):
self._widget = None
from IPython.display import display
from lux.core.frame import LuxDataFrame
Expand Down
22 changes: 11 additions & 11 deletions tests/test_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_vary_filter_val(global_var):
df = pytest.olympic
vis = Vis(["Height", "SportType=Ball"], df)
df.set_intent_as_vis(vis)
df._repr_html_()
df._ipython_display_()
assert len(df.recommendation["Filter"]) == len(df["SportType"].unique()) - 1
linechart = list(filter(lambda x: x.mark == "line", df.recommendation["Enhance"]))[0]
assert (
Expand All @@ -42,7 +42,7 @@ def test_filter_inequality(global_var):
lux.Clause(attribute="Acceleration", filter_op=">", value=10),
]
)
df._repr_html_()
df._ipython_display_()

from lux.utils.utils import get_filter_specs

Expand All @@ -59,7 +59,7 @@ def test_generalize_action(global_var):
df["Year"], format="%Y"
) # change pandas dtype for the column "Year" to datetype
df.set_intent(["Acceleration", "MilesPerGal", "Cylinders", "Origin=USA"])
df._repr_html_()
df._ipython_display_()
assert len(df.recommendation["Generalize"]) == 4
v1 = df.recommendation["Generalize"][0]
v2 = df.recommendation["Generalize"][1]
Expand All @@ -86,14 +86,14 @@ def test_row_column_group(global_var):
tseries[tseries.columns.min()] = tseries[tseries.columns.min()].fillna(0)
tseries[tseries.columns.max()] = tseries[tseries.columns.max()].fillna(tseries.max(axis=1))
tseries = tseries.interpolate("zero", axis=1)
tseries._repr_html_()
tseries._ipython_display_()
assert list(tseries.recommendation.keys()) == ["Temporal"]


def test_groupby(global_var):
df = pytest.college_df
groupbyResult = df.groupby("Region").sum()
groupbyResult._repr_html_()
groupbyResult._ipython_display_()
assert list(groupbyResult.recommendation.keys()) == ["Column Groups"]


Expand Down Expand Up @@ -160,7 +160,7 @@ def test_crosstab():

df = pd.DataFrame(d, columns=["Name", "Exam", "Subject", "Result"])
result = pd.crosstab([df.Exam], df.Result)
result._repr_html_()
result._ipython_display_()
assert list(result.recommendation.keys()) == ["Row Groups", "Column Groups"]


Expand All @@ -169,7 +169,7 @@ def test_custom_aggregation(global_var):

df = pytest.college_df
df.set_intent(["HighestDegree", lux.Clause("AverageCost", aggregation=np.ptp)])
df._repr_html_()
df._ipython_display_()
assert list(df.recommendation.keys()) == ["Enhance", "Filter", "Generalize"]
df.clear_intent()

Expand All @@ -178,7 +178,7 @@ def test_year_filter_value(global_var):
df = pytest.car_df
df["Year"] = pd.to_datetime(df["Year"], format="%Y")
df.set_intent(["Acceleration", "Horsepower"])
df._repr_html_()
df._ipython_display_()
list_of_vis_with_year_filter = list(
filter(
lambda vis: len(
Expand Down Expand Up @@ -210,7 +210,7 @@ def test_similarity(global_var):
lux.Clause("Origin=USA"),
]
)
df._repr_html_()
df._ipython_display_()
assert len(df.recommendation["Similarity"]) == 2
ranked_list = df.recommendation["Similarity"]

Expand Down Expand Up @@ -264,13 +264,13 @@ def test_similarity2():
def test_intent_retained():
df = pd.read_csv("https://raw.githubusercontent.com/lux-org/lux-datasets/master/data/employee.csv")
df.intent = ["Attrition"]
df._repr_html_()
df._ipython_display_()

df["%WorkingYearsAtCompany"] = df["YearsAtCompany"] / df["TotalWorkingYears"]
assert df.current_vis != None
assert df.intent != None
assert df._recs_fresh == False
assert df._metadata_fresh == False

df._repr_html_()
df._ipython_display_()
assert list(df.recommendation.keys()) == ["Enhance", "Filter"]
10 changes: 5 additions & 5 deletions tests/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,17 @@ def test_underspecified_single_vis(global_var, test_recs):

def test_set_intent_as_vis(global_var, test_recs):
df = pytest.car_df
df._repr_html_()
df._ipython_display_()
vis = df.recommendation["Correlation"][0]
df.intent = vis
df._repr_html_()
df._ipython_display_()
test_recs(df, ["Enhance", "Filter", "Generalize"])


@pytest.fixture
def test_recs():
def test_recs_function(df, actions):
df._repr_html_()
df._ipython_display_()
assert len(df.recommendation) > 0
recKeys = list(df.recommendation.keys())
list_equal(recKeys, actions)
Expand Down Expand Up @@ -350,7 +350,7 @@ def test_populate_options(global_var):
lux.Clause(attribute="MilesPerGal"),
]
)
df._repr_html_()
df._ipython_display_()
col_set = set()
for specOptions in Compiler.populate_wildcard_options(df._intent, df)["attributes"]:
for clause in specOptions:
Expand All @@ -372,7 +372,7 @@ def test_remove_all_invalid(global_var):
lux.Clause(attribute="Origin"),
]
)
df._repr_html_()
df._ipython_display_()
assert len(df.current_vis) == 0
df.clear_intent()

Expand Down
52 changes: 26 additions & 26 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def contain_horsepower(df):

def test_default_actions_registered(global_var):
df = pytest.car_df
df._repr_html_()
df._ipython_display_()
assert "Distribution" in df.recommendation
assert len(df.recommendation["Distribution"]) > 0

Expand All @@ -67,7 +67,7 @@ def test_default_actions_registered(global_var):

def test_fail_validator():
df = register_new_action()
df._repr_html_()
df._ipython_display_()
assert (
"bars" not in df.recommendation,
"Bars should not be rendered when there is no intent 'horsepower' specified.",
Expand All @@ -77,7 +77,7 @@ def test_fail_validator():
def test_pass_validator():
df = register_new_action()
df.set_intent(["Acceleration", "Horsepower"])
df._repr_html_()
df._ipython_display_()
assert len(df.recommendation["bars"]) > 0
assert (
"bars" in df.recommendation,
Expand All @@ -87,7 +87,7 @@ def test_pass_validator():

def test_no_validator():
df = register_new_action(False)
df._repr_html_()
df._ipython_display_()
assert len(df.recommendation["bars"]) > 0
assert "bars" in df.recommendation

Expand Down Expand Up @@ -121,7 +121,7 @@ def random_categorical(ldf):
def test_remove_action():
df = register_new_action()
df.set_intent(["Acceleration", "Horsepower"])
df._repr_html_()
df._ipython_display_()
assert (
"bars" in df.recommendation,
"Bars should be rendered after it has been registered with correct intent.",
Expand All @@ -131,7 +131,7 @@ def test_remove_action():
"Bars should be rendered after it has been registered with correct intent.",
)
lux.config.remove_action("bars")
df._repr_html_()
df._ipython_display_()
assert (
"bars" not in df.recommendation,
"Bars should not be rendered after it has been removed.",
Expand All @@ -148,22 +148,22 @@ def test_remove_invalid_action(global_var):
# TODO: This test does not pass in pytest but is working in Jupyter notebook.
def test_remove_default_actions(global_var):
df = pytest.car_df
df._repr_html_()
df._ipython_display_()

lux.config.remove_action("distribution")
df._repr_html_()
df._ipython_display_()
assert "Distribution" not in df.recommendation

lux.config.remove_action("occurrence")
df._repr_html_()
df._ipython_display_()
assert "Occurrence" not in df.recommendation

lux.config.remove_action("temporal")
df._repr_html_()
df._ipython_display_()
assert "Temporal" not in df.recommendation

lux.config.remove_action("correlation")
df._repr_html_()
df._ipython_display_()
assert "Correlation" not in df.recommendation

assert (
Expand All @@ -173,7 +173,7 @@ def test_remove_default_actions(global_var):

df = register_new_action()
df.set_intent(["Acceleration", "Horsepower"])
df._repr_html_()
df._ipython_display_()
assert (
"bars" in df.recommendation,
"Bars should be rendered after it has been registered with correct intent.",
Expand All @@ -195,7 +195,7 @@ def add_title(fig, ax):

df = pd.read_csv("lux/data/car.csv")
lux.config.plotting_style = add_title
df._repr_html_()
df._ipython_display_()
title_addition = 'ax.set_title("Test Title")'
exported_code_str = df.recommendation["Correlation"][0].to_Altair()
assert title_addition in exported_code_str
Expand All @@ -211,7 +211,7 @@ def change_color_make_transparent_add_title(chart):

df = pd.read_csv("lux/data/car.csv")
lux.config.plotting_style = change_color_make_transparent_add_title
df._repr_html_()
df._ipython_display_()
config_mark_addition = 'chart = chart.configure_mark(color="green", opacity=0.2)'
title_addition = 'chart.title = "Test Title"'
exported_code_str = df.recommendation["Correlation"][0].to_Altair()
Expand All @@ -221,47 +221,47 @@ def change_color_make_transparent_add_title(chart):

def test_sampling_flag_config():
df = pd.read_csv("https://raw.githubusercontent.com/lux-org/lux-datasets/master/data/airbnb_nyc.csv")
df._repr_html_()
df._ipython_display_()
assert df.recommendation["Correlation"][0].data.shape[0] == 30000
lux.config.sampling = False
df = df.copy()
df._repr_html_()
df._ipython_display_()
assert df.recommendation["Correlation"][0].data.shape[0] == 48895
lux.config.sampling = True


def test_sampling_parameters_config():
df = pd.read_csv("lux/data/car.csv")
df._repr_html_()
df._ipython_display_()
assert df.recommendation["Correlation"][0].data.shape[0] == 392
lux.config.sampling_start = 50
lux.config.sampling_cap = 100
df = pd.read_csv("lux/data/car.csv")
df._repr_html_()
df._ipython_display_()
assert df.recommendation["Correlation"][0].data.shape[0] == 100
lux.config.sampling_cap = 30000
lux.config.sampling_start = 10000


def test_heatmap_flag_config():
df = pd.read_csv("https://raw.githubusercontent.com/lux-org/lux-datasets/master/data/airbnb_nyc.csv")
df._repr_html_()
df._ipython_display_()
assert df.recommendation["Correlation"][0]._postbin
lux.config.heatmap = False
df = pd.read_csv("https://raw.githubusercontent.com/lux-org/lux-datasets/master/data/airbnb_nyc.csv")
df._repr_html_()
df._ipython_display_()
assert not df.recommendation["Correlation"][0]._postbin
lux.config.heatmap = True


def test_topk(global_var):
df = pd.read_csv("lux/data/college.csv")
lux.config.topk = False
df._repr_html_()
df._ipython_display_()
assert len(df.recommendation["Correlation"]) == 45, "Turn off top K"
lux.config.topk = 20
df = pd.read_csv("lux/data/college.csv")
df._repr_html_()
df._ipython_display_()
assert len(df.recommendation["Correlation"]) == 20, "Show top 20"
for vis in df.recommendation["Correlation"]:
assert vis.score > 0.2
Expand All @@ -270,20 +270,20 @@ def test_topk(global_var):
def test_sort(global_var):
df = pd.read_csv("lux/data/college.csv")
lux.config.topk = 15
df._repr_html_()
df._ipython_display_()
assert len(df.recommendation["Correlation"]) == 15, "Show top 15"
for vis in df.recommendation["Correlation"]:
assert vis.score > 0.5
df = pd.read_csv("lux/data/college.csv")
lux.config.sort = "ascending"
df._repr_html_()
df._ipython_display_()
assert len(df.recommendation["Correlation"]) == 15, "Show bottom 15"
for vis in df.recommendation["Correlation"]:
assert vis.score < 0.35

lux.config.sort = "none"
df = pd.read_csv("lux/data/college.csv")
df._repr_html_()
df._ipython_display_()
scorelst = [x.score for x in df.recommendation["Distribution"]]
assert sorted(scorelst) != scorelst, "unsorted setting"
lux.config.sort = "descending"
Expand All @@ -300,7 +300,7 @@ def test_sort(global_var):

# df.plot_config = change_color_add_title

# df._repr_html_()
# df._ipython_display_()

# vis_code = df.recommendation["Correlation"][0].to_Altair()
# print (vis_code)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def test_refresh_inplace():
}
)
with pytest.warns(UserWarning, match="Lux detects that the attribute 'date' may be temporal."):
df._repr_html_()
df._ipython_display_()
assert df.data_type["date"] == "temporal"

from lux.vis.Vis import Vis
Expand Down

0 comments on commit 955a4a1

Please sign in to comment.