Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Deprecated development CLI #3065

Merged
merged 27 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions features/activate_nbstripout.feature

This file was deleted.

11 changes: 0 additions & 11 deletions features/build_docs.feature

This file was deleted.

13 changes: 0 additions & 13 deletions features/build_reqs.feature

This file was deleted.

8 changes: 0 additions & 8 deletions features/jupyter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,3 @@ Feature: Jupyter targets in new project
When I execute the kedro jupyter command "lab --no-browser"
Then I wait for the jupyter webserver to run for up to "120" seconds
Then Jupyter Lab should run on port 8888

Scenario: Execute node convert into Python files
Given I have added a test jupyter notebook
When I execute the test jupyter notebook and save changes
And I execute the kedro jupyter command "convert --all"
And Wait until the process is finished for up to "120" seconds
Then I should get a successful exit code
And Code cell with node tag should be converted into kedro node
11 changes: 0 additions & 11 deletions features/package.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,3 @@ Feature: Package target in new project
When I install the project's python package
And I execute the installed project package
Then I should get a successful exit code

@fresh_venv
Scenario: Install package after running kedro build-reqs
Given I have updated kedro requirements
When I execute the kedro command "build-reqs --resolver=backtracking"
Then I should get a successful exit code
When I execute the kedro command "package"
Then I should get a successful exit code
When I install the project's python package
And I execute the installed project package
Then I should get a successful exit code
13 changes: 0 additions & 13 deletions features/test.feature

This file was deleted.

123 changes: 0 additions & 123 deletions kedro/framework/cli/jupyter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,13 @@
import json
import os
import shutil
import sys
from collections import Counter
from glob import iglob
from pathlib import Path
from typing import Any
from warnings import warn

import click
from click import secho

from kedro.framework.cli.utils import (
KedroCliError,
_check_module_importable,
command_with_verbosity,
env_option,
forward_command,
python_call,
Expand Down Expand Up @@ -190,119 +183,3 @@ def _create_kernel(kernel_name: str, display_name: str) -> str:
f"Cannot setup kedro kernel for Jupyter.\nError: {exc}"
) from exc
return kernel_path


@command_with_verbosity(jupyter, "convert")
@click.option("--all", "-a", "all_flag", is_flag=True, help=CONVERT_ALL_HELP)
@click.option("-y", "overwrite_flag", is_flag=True, help=OVERWRITE_HELP)
@click.argument(
"filepath",
type=click.Path(exists=True, dir_okay=False, resolve_path=True),
required=False,
nargs=-1,
)
@env_option
@click.pass_obj # this will pass the metadata as first argument
def convert_notebook(
metadata: ProjectMetadata, all_flag, overwrite_flag, filepath, env, **kwargs
): # noqa: unused-argument, too-many-locals
"""Convert selected or all notebooks found in a Kedro project
to Kedro code, by exporting code from the appropriately-tagged cells:
Cells tagged as `node` will be copied over to a Python file matching
the name of the notebook, under `<source_dir>/<package_name>/nodes`.
*Note*: Make sure your notebooks have unique names!
FILEPATH: Path(s) to exact notebook file(s) to be converted. Both
relative and absolute paths are accepted.
Should not be provided if --all flag is already present. (DEPRECATED)
"""

deprecation_message = (
"DeprecationWarning: Command 'kedro jupyter convert' is deprecated and "
"will not be available from Kedro 0.19.0."
)
click.secho(deprecation_message, fg="red")

project_path = metadata.project_path
source_path = metadata.source_dir
package_name = metadata.package_name

if not filepath and not all_flag:
secho(
"Please specify a notebook filepath "
"or add '--all' to convert all notebooks."
)
sys.exit(1)

if all_flag:
# pathlib glob does not ignore hidden directories,
# whereas Python glob does, which is more useful in
# ensuring checkpoints will not be included
pattern = project_path / "**" / "*.ipynb"
notebooks = sorted(Path(p) for p in iglob(str(pattern), recursive=True))
else:
notebooks = [Path(f) for f in filepath]

counter = Counter(n.stem for n in notebooks)
non_unique_names = [name for name, counts in counter.items() if counts > 1]
if non_unique_names:
names = ", ".join(non_unique_names)
raise KedroCliError(
f"Found non-unique notebook names! Please rename the following: {names}"
)

output_dir = source_path / package_name / "nodes"
if not output_dir.is_dir():
output_dir.mkdir()
(output_dir / "__init__.py").touch()

for notebook in notebooks:
secho(f"Converting notebook '{notebook}'...")
output_path = output_dir / f"{notebook.stem}.py"

if output_path.is_file():
overwrite = overwrite_flag or click.confirm(
f"Output file {output_path} already exists. Overwrite?", default=False
)
if overwrite:
_export_nodes(notebook, output_path)
else:
_export_nodes(notebook, output_path)

secho("Done!", color="green") # type: ignore


def _export_nodes(filepath: Path, output_path: Path) -> None:
"""Copy code from Jupyter cells into nodes in src/<package_name>/nodes/,
under filename with same name as notebook.

Args:
filepath: Path to Jupyter notebook file
output_path: Path where notebook cells' source code will be exported
Raises:
KedroCliError: When provided a filepath that cannot be read as a
Jupyer notebook and loaded into json format.
"""
try:
content = json.loads(filepath.read_text())
except json.JSONDecodeError as exc:
raise KedroCliError(
f"Provided filepath is not a Jupyter notebook: {filepath}"
) from exc
cells = [
cell
for cell in content["cells"]
if cell["cell_type"] == "code" and "node" in cell["metadata"].get("tags", {})
]

if cells:
output_path.write_text("")
for cell in cells:
_append_source_code(cell, output_path)
else:
warn(f"Skipping notebook '{filepath}' - no nodes to export.")


def _append_source_code(cell: dict[str, Any], path: Path) -> None:
source_code = "".join(cell["source"]).strip() + "\n"
with path.open(mode="a") as file_:
file_.write(source_code)
Loading
Loading