Skip to content

Commit

Permalink
[KED-1225] Make kedro jupyter use Jupyter installation from local e…
Browse files Browse the repository at this point in the history
…nvironment (kedro-org#321)
  • Loading branch information
Lorena Bălan authored Nov 19, 2019
1 parent ffdbf73 commit 02b2c0c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 28 deletions.
1 change: 1 addition & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Allowed the use of nulls in `parameters.yml`.
* Fixed an issue where `%reload_kedro` wasn't reloading all user modules.
* Fixed `pandas_to_spark` and `spark_to_pandas` decorators to work with functions with kwargs.
* Fixed a bug where `kedro jupyter notebook` and `kedro jupyter lab` would run a different Jupyter installation to the one in the local environment.

## Breaking changes to the API
* Renamed entry point for running pip-installed projects to `run_package()` instead of `main()` in `src/<package>/run.py`.
Expand Down
6 changes: 3 additions & 3 deletions kedro/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import sys
from itertools import chain
from pathlib import Path
from typing import Any, Dict, Iterable, Sequence, Tuple, Union
from typing import Any, Dict, Iterable, List, Sequence, Tuple, Union
from warnings import warn

import click
Expand All @@ -45,15 +45,15 @@
NODE_TAG = "node"


def call(cmd, **kwargs): # pragma: no cover
def call(cmd: List[str], **kwargs): # pragma: no cover
"""Run a subprocess command and exit if it fails."""
print(" ".join(shlex.quote(c) for c in cmd))
res = subprocess.run(cmd, **kwargs).returncode
if res:
sys.exit(res)


def python_call(module, arguments, **kwargs): # pragma: no cover
def python_call(module: str, arguments: Iterable[str], **kwargs): # pragma: no cover
"""Run a subprocess command that invokes a Python module."""
call([sys.executable, "-m", module] + list(arguments), **kwargs)

Expand Down
14 changes: 6 additions & 8 deletions kedro/template/{{ cookiecutter.repo_name }}/kedro_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ def activate_nbstripout():
def _build_jupyter_command(
base: str, ip: str, all_kernels: bool, args: Iterable[str]
) -> List[str]:
cmd = [base, "--ip=" + ip]
cmd = [base, "--ip", ip]

if not all_kernels:
project_name = "{{ cookiecutter.project_name }}"
Expand Down Expand Up @@ -355,11 +355,10 @@ def jupyter_notebook(ip, all_kernels, args):
if "-h" not in args and "--help" not in args:
ipython_message(all_kernels)

call(
_build_jupyter_command(
"jupyter-notebook", ip=ip, all_kernels=all_kernels, args=args
)
arguments = _build_jupyter_command(
"notebook", ip=ip, all_kernels=all_kernels, args=args
)
python_call("jupyter", arguments)


@forward_command(jupyter, "lab", forward_help=True)
Expand All @@ -370,9 +369,8 @@ def jupyter_lab(ip, all_kernels, args):
if "-h" not in args and "--help" not in args:
ipython_message(all_kernels)

call(
_build_jupyter_command("jupyter-lab", ip=ip, all_kernels=all_kernels, args=args)
)
arguments = _build_jupyter_command("lab", ip=ip, all_kernels=all_kernels, args=args)
python_call("jupyter", arguments)


@jupyter.command("convert")
Expand Down
48 changes: 31 additions & 17 deletions tests/template/test_kedro_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,28 +378,34 @@ def test_requirements_file_doesnt_exist(


class TestJupyterNotebookCommand:
def test_default_kernel(self, call_mock, fake_kedro_cli, fake_ipython_message):
def test_default_kernel(
self, python_call_mock, fake_kedro_cli, fake_ipython_message
):
result = CliRunner().invoke(
fake_kedro_cli.cli, ["jupyter", "notebook", "--ip=0.0.0.0"]
fake_kedro_cli.cli, ["jupyter", "notebook", "--ip", "0.0.0.0"]
)
assert not result.exit_code, result.stdout
fake_ipython_message.assert_called_once_with(False)
call_mock.assert_called_once_with(
python_call_mock.assert_called_once_with(
"jupyter",
[
"jupyter-notebook",
"--ip=0.0.0.0",
"notebook",
"--ip",
"0.0.0.0",
"--NotebookApp.kernel_spec_manager_class=kedro.cli.jupyter.SingleKernelSpecManager",
"--KernelSpecManager.default_kernel_name='TestProject'",
]
],
)

def test_all_kernels(self, call_mock, fake_kedro_cli, fake_ipython_message):
def test_all_kernels(self, python_call_mock, fake_kedro_cli, fake_ipython_message):
result = CliRunner().invoke(
fake_kedro_cli.cli, ["jupyter", "notebook", "--all-kernels"]
)
assert not result.exit_code, result.stdout
fake_ipython_message.assert_called_once_with(True)
call_mock.assert_called_once_with(["jupyter-notebook", "--ip=127.0.0.1"])
python_call_mock.assert_called_once_with(
"jupyter", ["notebook", "--ip", "127.0.0.1"]
)

@pytest.mark.parametrize("help_flag", ["-h", "--help"])
def test_help(self, help_flag, fake_kedro_cli, fake_ipython_message):
Expand All @@ -411,32 +417,40 @@ def test_help(self, help_flag, fake_kedro_cli, fake_ipython_message):


class TestJupyterLabCommand:
def test_default_kernel(self, call_mock, fake_kedro_cli, fake_ipython_message):
def test_default_kernel(
self, python_call_mock, fake_kedro_cli, fake_ipython_message
):
result = CliRunner().invoke(
fake_kedro_cli.cli, ["jupyter", "lab", "--ip=0.0.0.0"]
fake_kedro_cli.cli, ["jupyter", "lab", "--ip", "0.0.0.0"]
)
assert not result.exit_code, result.stdout
fake_ipython_message.assert_called_once_with(False)
call_mock.assert_called_once_with(
python_call_mock.assert_called_once_with(
"jupyter",
[
"jupyter-lab",
"--ip=0.0.0.0",
"lab",
"--ip",
"0.0.0.0",
"--NotebookApp.kernel_spec_manager_class=kedro.cli.jupyter.SingleKernelSpecManager",
"--KernelSpecManager.default_kernel_name='TestProject'",
]
],
)

def test_all_kernels(self, call_mock, fake_kedro_cli, fake_ipython_message):
def test_all_kernels(self, python_call_mock, fake_kedro_cli, fake_ipython_message):
result = CliRunner().invoke(
fake_kedro_cli.cli, ["jupyter", "lab", "--all-kernels"]
)
assert not result.exit_code, result.stdout
fake_ipython_message.assert_called_once_with(True)
call_mock.assert_called_once_with(["jupyter-lab", "--ip=127.0.0.1"])
python_call_mock.assert_called_once_with(
"jupyter", ["lab", "--ip", "127.0.0.1"]
)

@pytest.mark.parametrize("help_flag", ["-h", "--help"])
def test_help(self, help_flag, fake_kedro_cli, fake_ipython_message):
result = CliRunner().invoke(fake_kedro_cli.cli, ["jupyter", "lab", help_flag])
result = CliRunner().invoke(
fake_kedro_cli.cli, [sys.executable, "-m", "jupyter", "lab", help_flag]
)
assert not result.exit_code, result.stdout
fake_ipython_message.assert_not_called()

Expand Down

0 comments on commit 02b2c0c

Please sign in to comment.