Example #1
0
def convert_notebook(all_flag, overwrite_flag, filepath):
    """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 `src/<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.

    """
    from kedro_tutorial.run import ProjectContext
    if not filepath and not all_flag:
        secho("Please specify a notebook filepath "
              "or add '--all' to convert all notebooks.")
        return

    kedro_project_path = ProjectContext(Path.cwd()).project_path
    kedro_package_name = "kedro_tutorial"

    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 = kedro_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:
        raise KedroCliError("Found non-unique notebook names! "
                            "Please rename the following: {}".format(
                                ", ".join(non_unique_names)))

    for notebook in notebooks:
        secho("Converting notebook '{}'...".format(str(notebook)))
        output_path = (kedro_project_path / "src" / kedro_package_name /
                       "nodes" / "{}.py".format(notebook.stem))

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

    secho("Done!")
Example #2
0
def project_context():
    return ProjectContext(str(Path.cwd()))
Example #3
0
def project_context(mocker):
    # Don't configure the logging module. If it's configured, tests that
    # check logs using the ``caplog`` fixture depend on execution order.
    mocker.patch.object(ProjectContext, "_setup_logging")

    return ProjectContext(str(Path.cwd()))