Skip to content

Commit

Permalink
Warn user when trying to show result in a non-interactive shell (deep…
Browse files Browse the repository at this point in the history
…checks#745)

* Warn user when trying to show result in a non-interactive shell

* Fix missing space

* Fix lint

* Remove unused function
  • Loading branch information
matanper authored Jan 25, 2022
1 parent e92ff67 commit 6061902
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 23 deletions.
8 changes: 5 additions & 3 deletions deepchecks/base/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import inspect
import io
import traceback
import warnings
from collections import OrderedDict
from functools import wraps
from typing import Any, Callable, List, Union, Dict, Mapping
Expand All @@ -36,7 +37,7 @@
from deepchecks.base.dataset import Dataset
from deepchecks.base.display_pandas import dataframe_to_html, get_conditions_table
from deepchecks.utils.strings import get_docs_summary, split_camel_case
from deepchecks.utils.ipython import is_ipython_display
from deepchecks.utils.ipython import is_notebook
from deepchecks.errors import DeepchecksValueError, DeepchecksNotSupportedError


Expand Down Expand Up @@ -331,11 +332,12 @@ def priority(self) -> int:

def show(self, unique_id=None, show_additional_outputs=True):
"""Display check result."""
if is_ipython_display():
if is_notebook():
self._ipython_display_(unique_id=unique_id,
show_additional_outputs=show_additional_outputs)
else:
print(self)
warnings.warn('You are running in a non-interactive python shell. in order to show result you have to use '
'an IPython shell (etc Jupyter)')


def wrap_run(func, class_instance):
Expand Down
8 changes: 5 additions & 3 deletions deepchecks/base/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# pylint: disable=broad-except
import abc
import io
import warnings
from collections import OrderedDict
from typing import Union, List, Optional, Tuple, Any, Container, Mapping, Callable

Expand All @@ -26,7 +27,7 @@
from deepchecks.base.dataset import Dataset
from deepchecks.base.check import CheckResult, TrainTestBaseCheck, SingleDatasetBaseCheck, ModelOnlyBaseCheck, \
CheckFailure, ModelComparisonBaseCheck, ModelComparisonContext, BaseCheck
from deepchecks.utils.ipython import is_ipython_display
from deepchecks.utils.ipython import is_notebook
from deepchecks.utils.typing import BasicModel


Expand Down Expand Up @@ -65,10 +66,11 @@ def _ipython_display_(self):

def show(self):
"""Display suite result."""
if is_ipython_display():
if is_notebook():
self._ipython_display_()
else:
print(self)
warnings.warn('You are running in a non-interactive python shell. in order to show result you have to use '
'an IPython shell (etc Jupyter)')

def save_as_html(self, file=None):
"""Save output as html file.
Expand Down
18 changes: 1 addition & 17 deletions deepchecks/utils/ipython.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@
"""Utils module containing useful global functions."""
import re
import subprocess
import sys
from functools import lru_cache
from importlib import import_module

from IPython import get_ipython # TODO: I think we should remove ipython from mandatory dependencies


__all__ = ['is_notebook', 'is_ipython_display', 'is_widgets_enabled']
__all__ = ['is_notebook', 'is_widgets_enabled']


@lru_cache(maxsize=None)
Expand All @@ -37,20 +35,6 @@ def is_notebook() -> bool:
return False # Probably standard Python interpreter


@lru_cache(maxsize=None)
def is_ipython_display() -> bool:
"""Check whether we have IPython display module in current environment."""
module = 'IPython.display'
if module in sys.modules:
return True
try:
import_module(module)
return True
# pylint: disable=broad-except
except Exception:
return False


@lru_cache(maxsize=None)
def is_widgets_enabled() -> bool:
"""Check if we're running in jupyter and having jupyter widgets extension enabled."""
Expand Down

0 comments on commit 6061902

Please sign in to comment.