import functools
import builtins
import logging


def requires_ipython(fn):
  """Decorator for methods that can only be run in IPython."""

  @functools.wraps(fn)
  def run_if_ipython(*args, **kwargs):
    """Invokes `fn` if called from IPython, otherwise just emits a warning."""
    if getattr(builtins, '__IPYTHON__', None):
      # __IPYTHON__ variable is set by IPython, see
      # https://ipython.org/ipython-doc/rel-0.10.2/html/interactive/reference.html#embedding-ipython.
      return fn(*args, **kwargs)
    else:
      logging.warning(
          'Method "%s" is a no-op when invoked outside of IPython.',
          fn.__name__)

  return run_if_ipython