Repository: kedro-org/kedro
File: kedro/ipython/__init__.py (Python)

Code before:
    for module in to_remove:
        del sys.modules[module]


def _find_kedro_project(current_dir: Path) -> Any:  # pragma: no cover
    while current_dir != current_dir.parent:
        if _is_project(current_dir):
            return current_dir
        current_dir = current_dir.parent

    return None


@typing.no_type_check
@magic_arguments()
@argument(
    "node",
    type=str,
    help=("Name of the Node."),
    nargs="?",
    default=None,
)
def magic_load_node(node: str) -> None:
    """The line magic %load_node <node_name>
    Currently it only support Jupyter Notebook (>7.0) and Jupyter Lab. This line magic
    will generate code in multiple cells to load dataest from Data Catalog, import
    relevant functions and modules, and the function body.
    """
    cells = _load_node(node, pipelines)
    from ipylab import JupyterFrontEnd

    app = JupyterFrontEnd()

    def _create_cell_with_text(text: str) -> None:
        # Noted this only works with Notebook >7.0 or Jupyter Lab. It doesn't work with
        # VS Code Notebook due to imcompatible backends.
        app.commands.execute("notebook:insert-cell-below")
        app.commands.execute("notebook:replace-selection", {"text": text})

    for cell in cells:
        _create_cell_with_text(cell)


def _load_node(node_name: str, pipelines: _ProjectPipelines) -> list[str]:
    """Prepare the code to load dataset from catalog, import statements and function body.

    Args:
        node_name (str): The name of the node.

    Returns:
        list[str]: A list of string which is the generated code, each string represent a

Review comment (suggestion):
```suggestion
    will generate code in multiple cells to load datasets from `DataCatalog`, import
```

Code after:
    for module in to_remove:
        del sys.modules[module]


def _find_kedro_project(current_dir: Path) -> Any:  # pragma: no cover
    while current_dir != current_dir.parent:
        if _is_project(current_dir):
            return current_dir
        current_dir = current_dir.parent

    return None


@typing.no_type_check
@magic_arguments()
@argument(
    "node",
    type=str,
    help=("Name of the Node."),
    nargs="?",
    default=None,
)
def magic_load_node(node: str) -> None:
    """The line magic %load_node <node_name>
    Currently it only supports Jupyter Notebook (>7.0) and Jupyter Lab. This line magic
    will generate code in multiple cells to load datasets from `DataCatalog`, import
    relevant functions and modules, node function definition and a function call.
    """
    cells = _load_node(node, pipelines)
    from ipylab import JupyterFrontEnd

    app = JupyterFrontEnd()

    def _create_cell_with_text(text: str) -> None:
        # Noted this only works with Notebook >7.0 or Jupyter Lab. It doesn't work with
        # VS Code Notebook due to imcompatible backends.
        app.commands.execute("notebook:insert-cell-below")
        app.commands.execute("notebook:replace-selection", {"text": text})

    for cell in cells:
        _create_cell_with_text(cell)


def _load_node(node_name: str, pipelines: _ProjectPipelines) -> list[str]:
    """Prepare the code to load dataset from catalog, import statements and function body.

    Args:
        node_name (str): The name of the node.

    Returns:
        list[str]: A list of string which is the generated code, each string represent a