Skip to content

Commit

Permalink
Add and use utils.is_running_jupyter()
Browse files Browse the repository at this point in the history
  • Loading branch information
elgalu committed Oct 23, 2017
1 parent b471d34 commit 1cb7aab
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
25 changes: 17 additions & 8 deletions click/core.py
Expand Up @@ -6,7 +6,7 @@
from functools import update_wrapper

from .types import convert_type, IntRange, BOOL
from .utils import make_str, make_default_short_help, echo, get_os_args
from .utils import make_str, make_default_short_help, echo, get_os_args, is_running_jupyter
from .exceptions import ClickException, UsageError, BadParameter, Abort, \
MissingParameter
from .termui import prompt, confirm
Expand Down Expand Up @@ -698,33 +698,40 @@ def main(self, args=None, prog_name=None, complete_var=None,
# Hook for the Bash completion. This only activates if the Bash
# completion is actually enabled, otherwise this is quite a fast
# noop.
_bashcomplete(self, prog_name, complete_var)
if is_running_jupyter():
args = []
else:
_bashcomplete(self, prog_name, complete_var)

try:
try:
with self.make_context(prog_name, args, **extra) as ctx:
rv = self.invoke(ctx)
if not standalone_mode:
return rv
ctx.exit()
if not is_running_jupyter():
ctx.exit()
except (EOFError, KeyboardInterrupt):
echo(file=sys.stderr)
raise Abort()
except ClickException as e:
if not standalone_mode:
raise
e.show()
sys.exit(e.exit_code)
if not is_running_jupyter():
sys.exit(e.exit_code)
except IOError as e:
if e.errno == errno.EPIPE:
sys.exit(1)
if not is_running_jupyter():
sys.exit(1)
else:
raise
except Abort:
if not standalone_mode:
raise
echo('Aborted!', file=sys.stderr)
sys.exit(1)
if not is_running_jupyter():
sys.exit(1)

def __call__(self, *args, **kwargs):
"""Alias for :meth:`main`."""
Expand Down Expand Up @@ -819,7 +826,8 @@ def get_help_option(self, ctx):
def show_help(ctx, param, value):
if value and not ctx.resilient_parsing:
echo(ctx.get_help(), color=ctx.color)
ctx.exit()
if not is_running_jupyter():
ctx.exit()
return Option(help_options, is_flag=True,
is_eager=True, expose_value=False,
callback=show_help,
Expand Down Expand Up @@ -1027,7 +1035,8 @@ def format_commands(self, ctx, formatter):
def parse_args(self, ctx, args):
if not args and self.no_args_is_help and not ctx.resilient_parsing:
echo(ctx.get_help(), color=ctx.color)
ctx.exit()
if not is_running_jupyter():
ctx.exit()

rest = Command.parse_args(self, ctx, args)
if self.chain:
Expand Down
20 changes: 17 additions & 3 deletions click/utils.py
Expand Up @@ -211,11 +211,17 @@ def echo(message=None, file=None, nl=True, err=False, color=None):
:param color: controls if the terminal supports ANSI colors or not. The
default is autodetection.
"""
if file is None:
if is_running_jupyter():
if err:
file = _default_text_stderr()
file = sys.stderr
else:
file = _default_text_stdout()
file = sys.stdout
else:
if file is None:
if err:
file = _default_text_stderr()
else:
file = _default_text_stdout()

# Convert non bytes/text into the native string type.
if message is not None and not isinstance(message, echo_native_types):
Expand Down Expand Up @@ -348,6 +354,14 @@ def get_os_args():
return sys.argv[1:]


def is_running_jupyter() -> bool:
"""This returns true if running inside a Jupyter Notebook
.. versionadded:: elgalu's fork: github.com/elgalu/click@jupyter
"""
prog_name = make_str(os.path.basename(sys.argv[0]))
return prog_name == 'ipykernel_launcher.py'


def format_filename(filename, shorten=False):
"""Formats a filename for user display. The main purpose of this
function is to ensure that the filename can be displayed at all. This
Expand Down

0 comments on commit 1cb7aab

Please sign in to comment.