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

Release 0.4.1 #1197

Merged
merged 18 commits into from
Dec 11, 2024
Merged
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
add verbose parameter to print_display to toggle on or off printing o…
…f progress messages when generating table report (#1188)
priscilla-b authored and jeromedockes committed Dec 11, 2024
commit ea11506ffab40f6eef06e35f0f063ff37f473d7f
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -32,6 +32,10 @@ Changes
printing of progress information when a report is being generated.
:pr:`1182` by :user:`Priscilla Baah<priscilla-b>`.

* A parameter `verbose` has been added to the :func:`patch_display` to toggle on or off the
printing of progress information when a table report is being generated.
:pr:`1188` by :user:`Priscilla Baah<priscilla-b>`.

Bug fixes
---------

23 changes: 17 additions & 6 deletions skrub/_reporting/_patching.py
Original file line number Diff line number Diff line change
@@ -11,13 +11,17 @@ def _stashed_name(method_name):
return f"_skrub_{method_name}"


def _patch(cls, method_name):
def _patch(cls, method_name, verbose):
if (original_method := getattr(cls, method_name, None)) is None:
return
stashed_name = _stashed_name(method_name)
if not hasattr(cls, stashed_name):
setattr(cls, stashed_name, original_method)
setattr(cls, method_name, lambda df: getattr(TableReport(df), method_name)())
setattr(
cls,
method_name,
lambda df: getattr(TableReport(df, verbose=verbose), method_name)(),
)


def _unpatch(cls, method_name):
@@ -27,7 +31,7 @@ def _unpatch(cls, method_name):
setattr(cls, method_name, original_method)


def _change_display(transform, to_patch):
def _change_display(transform, to_patch, **transform_kwargs):
for module_name, class_names in to_patch:
try:
mod = importlib.import_module(module_name)
@@ -36,7 +40,7 @@ def _change_display(transform, to_patch):
for cls_name in class_names:
cls = getattr(mod, cls_name)
for method_name in _METHODS_TO_PATCH:
transform(cls, method_name)
transform(cls, method_name, **transform_kwargs)


def _get_to_patch(pandas, polars):
@@ -48,7 +52,7 @@ def _get_to_patch(pandas, polars):
return to_patch


def patch_display(pandas=True, polars=True):
def patch_display(pandas=True, polars=True, verbose=1):
"""Replace the default DataFrame HTML displays with ``skrub.TableReport``.

This function replaces the HTML displays (what is shown when an object is
@@ -63,6 +67,11 @@ def patch_display(pandas=True, polars=True):
If False, do not override the displays for pandas dataframes.
polars : bool, optional (default=True)
If False, do not override the displays for polars dataframes.
verbose : int, default = 1
Whether to print progress information while table report is being generated.

* verbose = 1 prints how many columns have been processed so far.
* verbose = 0 silences the output.

See Also
--------
@@ -72,7 +81,9 @@ def patch_display(pandas=True, polars=True):
TableReport :
Directly create a report from a dataframe.
"""
_change_display(_patch, _get_to_patch(pandas=pandas, polars=polars))
_change_display(
_patch, _get_to_patch(pandas=pandas, polars=polars), verbose=verbose
)


def unpatch_display(pandas=True, polars=True):
28 changes: 26 additions & 2 deletions skrub/_reporting/tests/test_patch_display.py
Original file line number Diff line number Diff line change
@@ -7,8 +7,14 @@

@pytest.mark.parametrize("repeat_patch", [1, 2])
@pytest.mark.parametrize("repeat_unpatch", [1, 2])
def test_patch_display(df_module, repeat_patch, repeat_unpatch):
df = df_module.example_dataframe
def test_patch_display(df_module, repeat_patch, repeat_unpatch, capsys):
df = df_module.make_dataframe(
dict(
a=[1, 2, 3, 4],
b=["one", "two", "three", "four"],
c=[11.1, 11.2, 11.3, 11.4],
)
)
assert "<table" in df._repr_html_()
assert "<skrub-table-report" not in df._repr_html_()
patch_display(pandas=False, polars=False)
@@ -25,3 +31,21 @@ def test_patch_display(df_module, repeat_patch, repeat_unpatch):
pickle.loads(pickle.dumps(df))
assert "<table" in df._repr_html_()
assert "<skrub-table-report" not in df._repr_html_()

try:
capsys.readouterr()
patch_display(verbose=0)
df._repr_html_()
assert capsys.readouterr().out == ""

capsys.readouterr()
patch_display()
df._repr_html_()
assert capsys.readouterr().out != ""

capsys.readouterr()
patch_display(verbose=1)
df._repr_html_()
assert capsys.readouterr().out != ""
finally:
unpatch_display()