Skip to content
Myr

Merge pull request #1186 from RonnyPfannschmidt/jupyter-notebook

Before adoption: this commit came in with the history the project adopted, so no landing records it.

enable colored output on jupyter notebooks

authored by David Lord · committed by GitHub · parents d053249, 06d0308 · Browse files · 2 files+35 added−13 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+35 added−13 removed

  • tests:+22 added−12 removed
  • other:+13 added−1 removed

2 files+35 added−13 removed

modified:click/_compat.py+13 added-1 removed
568 unmodified lines
569def strip_ansi(value):570 return _ansi_re.sub('', value)571572573def _is_jupyter_kernel_output(stream):574 if WIN:575 # TODO: Couldn't test on Windows, should't try to support until576 # someone tests the details wrt colorama.577 return578579 while isinstance(stream, (_FixupStream, _NonClosingTextIOWrapper)):580 stream = stream._stream581582 return stream.__class__.__module__.startswith("ipykernel.")583584585def should_strip_ansi(stream=None, color=None):586 if color is None:587 if stream is None:588 stream = sys.stdin577 return not isatty(stream)589 return not isatty(stream) and not _is_jupyter_kernel_output(stream)590 return not color591592593# If we're on Windows, we provide transparent integration through
121 unmodified lines
modified:tests/test_compat.py+22 added-12 removed
1import click2import pytest3from click._compat import should_strip_ansi, WIN455if click.__version__ >= '3.0':6 def test_legacy_callbacks(runner):7 def legacy_callback(ctx, value):8 return value.upper()6def test_legacy_callbacks(runner):7 def legacy_callback(ctx, value):8 return value.upper()910 @click.command()11 @click.option('--foo', callback=legacy_callback)12 def cli(foo):13 click.echo(foo)10 @click.command()11 @click.option('--foo', callback=legacy_callback)12 def cli(foo):13 click.echo(foo)1415 with pytest.warns(Warning, match='Invoked legacy parameter callback'):16 result = runner.invoke(cli, ['--foo', 'wat'])17 assert result.exit_code == 018 assert 'WAT' in result.output15 with pytest.warns(Warning, match='Invoked legacy parameter callback'):16 result = runner.invoke(cli, ['--foo', 'wat'])17 assert result.exit_code == 018 assert 'WAT' in result.output192021def test_bash_func_name():22 from click._bashcomplete import get_completion_script23 script = get_completion_script('foo-bar baz_blah', '_COMPLETE_VAR', 'bash').strip()24 assert script.startswith('_foo_barbaz_blah_completion()')25 assert '_COMPLETE_VAR=complete $1' in script262728@pytest.mark.xfail(WIN, reason="Jupyter not tested/supported on Windows")29def test_is_jupyter_kernel_output():30 class JupyterKernelFakeStream(object):31 pass3233 # implementation detail, aka cheapskate test34 JupyterKernelFakeStream.__module__ = "ipykernel.faked"35 assert not should_strip_ansi(stream=JupyterKernelFakeStream())