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

Refactor backend integration with Kedro by replacing bootstrap_project with configure_project #1796

Merged
merged 20 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 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
2 changes: 2 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ Please follow the established format:
## Major features and improvements

## Bug fixes and other changes

- Upgrade the gitpod workspace-full to a newer version which includes both Node 18 and Python 3.11.5. (#1862)
- Refactor backend integration with Kedro by replacing bootstrap_project with configure_project. (#1796)

# Release 9.0.0

Expand Down
17 changes: 11 additions & 6 deletions package/kedro_viz/integrations/kedro/data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
load data from projects created in a range of Kedro versions.
"""

# pylint: disable=import-outside-toplevel, protected-access
# pylint: disable=protected-access

import json
import logging
from pathlib import Path
from typing import Any, Dict, Optional, Tuple

from kedro import __version__
from kedro.framework.project import configure_project, pipelines
from kedro.framework.session import KedroSession
from kedro.framework.session.store import BaseSessionStore
from kedro.framework.startup import bootstrap_project
from kedro.io import DataCatalog
from kedro.pipeline import Pipeline

Expand Down Expand Up @@ -69,14 +71,16 @@ def load_data(
project_path: Path,
env: Optional[str] = None,
include_hooks: bool = False,
package_name: Optional[str] = None,
extra_params: Optional[Dict[str, Any]] = None,
) -> Tuple[DataCatalog, Dict[str, Pipeline], BaseSessionStore, Dict]:
"""Load data from a Kedro project.
Args:
project_path: the path whether the Kedro project is located.
project_path: the path where the Kedro project is located.
env: the Kedro environment to load the data. If not provided.
it will use Kedro default, which is local.
include_hooks: A flag to include all registered hooks in your Kedro Project.
package_name: The name of the current package
extra_params: Optional dictionary containing extra project parameters
for underlying KedroContext. If specified, will update (and therefore
take precedence over) the parameters retrieved from the project
Expand All @@ -85,10 +89,11 @@ def load_data(
A tuple containing the data catalog and the pipeline dictionary
and the session store.
"""
from kedro.framework.project import pipelines
from kedro.framework.startup import bootstrap_project

bootstrap_project(project_path)
if package_name:
configure_project(package_name)
else:
# bootstrap project when viz is run in dev mode
ravi-kumar-pilla marked this conversation as resolved.
Show resolved Hide resolved
bootstrap_project(project_path)

with KedroSession.create(
project_path=project_path,
Expand Down
15 changes: 13 additions & 2 deletions package/kedro_viz/launchers/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from click_default_group import DefaultGroup
from kedro.framework.cli.project import PARAMS_ARG_HELP
from kedro.framework.cli.utils import KedroCliError, _split_params
from kedro.framework.project import PACKAGE_NAME
from packaging.version import parse
from watchgod import RegExpWatcher, run_process

Expand Down Expand Up @@ -153,6 +154,7 @@ def run(
"env": env,
"autoreload": autoreload,
"include_hooks": include_hooks,
"package_name": PACKAGE_NAME,
"extra_params": params,
}
if autoreload:
Expand Down Expand Up @@ -268,6 +270,7 @@ def create_shareableviz_process(
endpoint,
bucket_name,
include_hooks,
PACKAGE_NAME,
process_completed,
exception_queue,
),
Expand Down Expand Up @@ -338,11 +341,19 @@ def create_shareableviz_process(


def load_and_deploy_viz(
platform, endpoint, bucket_name, include_hooks, process_completed, exception_queue
platform,
endpoint,
bucket_name,
include_hooks,
package_name,
process_completed,
exception_queue,
):
"""Loads Kedro Project data, creates a deployer and deploys to a platform"""
try:
load_and_populate_data(Path.cwd(), include_hooks=include_hooks)
load_and_populate_data(
Path.cwd(), include_hooks=include_hooks, package_name=package_name
)

# Start the deployment
deployer = DeployerFactory.create_deployer(platform, endpoint, bucket_name)
Expand Down
3 changes: 3 additions & 0 deletions package/kedro_viz/launchers/jupyter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""`kedro_viz.launchers.jupyter` provides line_magic to launch the viz server
from a jupyter notebook.
"""

# pragma: no cover
import logging
import multiprocessing
Expand All @@ -12,6 +13,7 @@

import IPython
from IPython.display import HTML, display
from kedro.framework.project import PACKAGE_NAME
from watchgod import RegExpWatcher, run_process

from kedro_viz.launchers.utils import _check_viz_up, _wait_for
Expand Down Expand Up @@ -140,6 +142,7 @@ def run_viz( # pylint: disable=too-many-locals
"env": env,
"autoreload": autoreload,
"include_hooks": include_hooks,
"package_name": PACKAGE_NAME,
"extra_params": params,
"project_path": project_path,
}
Expand Down
24 changes: 18 additions & 6 deletions package/kedro_viz/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,19 @@ def load_and_populate_data(
path: Path,
env: Optional[str] = None,
include_hooks: bool = False,
extra_params: Optional[Dict[str, Any]] = None,
package_name: Optional[str] = None,
pipeline_name: Optional[str] = None,
extra_params: Optional[Dict[str, Any]] = None,
):
"""Loads underlying Kedro project data and populates Kedro Viz Repositories"""

# Loads data from underlying Kedro Project
catalog, pipelines, session_store, stats_dict = kedro_data_loader.load_data(
path, env, include_hooks, extra_params
path,
env,
include_hooks,
package_name,
extra_params,
)

pipelines = (
Expand All @@ -83,6 +88,7 @@ def run_server(
project_path: Optional[str] = None,
autoreload: bool = False,
include_hooks: bool = False,
package_name: Optional[str] = None,
extra_params: Optional[Dict[str, Any]] = None,
): # pylint: disable=redefined-outer-name
"""Run a uvicorn server with a FastAPI app that either launches API response data from a file
Expand All @@ -101,15 +107,24 @@ def run_server(
project_path: the optional path of the Kedro project that contains the pipelines
to visualise. If not supplied, the current working directory will be used.
include_hooks: A flag to include all registered hooks in your Kedro Project.
package_name: The name of the current package
extra_params: Optional dictionary containing extra project parameters
for underlying KedroContext. If specified, will update (and therefore
take precedence over) the parameters retrieved from the project
configuration.
"""

path = Path(project_path) if project_path else Path.cwd()

if load_file is None:
load_and_populate_data(path, env, include_hooks, extra_params, pipeline_name)
load_and_populate_data(
path,
env,
include_hooks,
package_name,
pipeline_name,
extra_params,
)

if save_file:
save_api_responses_to_fs(save_file, fsspec.filesystem("file"))
Expand All @@ -127,8 +142,6 @@ def run_server(
if __name__ == "__main__": # pragma: no cover
import argparse

from kedro.framework.startup import bootstrap_project

parser = argparse.ArgumentParser(description="Launch a development viz server")
parser.add_argument("project_path", help="Path to a Kedro project")
parser.add_argument(
Expand All @@ -140,7 +153,6 @@ def run_server(
args = parser.parse_args()

project_path = (Path.cwd() / args.project_path).absolute()
bootstrap_project(project_path)

run_process_kwargs = {
"path": project_path,
Expand Down
19 changes: 15 additions & 4 deletions package/tests/test_launchers/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def mock_project_path(mocker):
"env": None,
"autoreload": False,
"include_hooks": False,
"package_name": None,
"extra_params": {},
},
),
Expand All @@ -101,6 +102,7 @@ def mock_project_path(mocker):
"env": None,
"autoreload": False,
"include_hooks": False,
"package_name": None,
"extra_params": {},
},
),
Expand All @@ -120,6 +122,7 @@ def mock_project_path(mocker):
"env": None,
"autoreload": False,
"include_hooks": False,
"package_name": None,
"extra_params": {},
},
),
Expand Down Expand Up @@ -150,6 +153,7 @@ def mock_project_path(mocker):
"env": "local",
"autoreload": False,
"include_hooks": False,
"package_name": None,
"extra_params": {"extra_param": "param"},
},
),
Expand All @@ -164,6 +168,7 @@ def mock_project_path(mocker):
"env": None,
"autoreload": False,
"include_hooks": True,
"package_name": None,
"extra_params": {},
},
),
Expand Down Expand Up @@ -284,6 +289,7 @@ def test_kedro_viz_command_with_autoreload(
"autoreload": True,
"project_path": mock_project_path,
"include_hooks": False,
"package_name": None,
"extra_params": {},
},
"watcher_cls": RegExpWatcher,
Expand Down Expand Up @@ -579,6 +585,7 @@ def test_create_shareableviz_process(
endpoint,
bucket_name,
include_hooks,
None,
mock_process_completed.return_value,
mock_exception_queue.return_value,
),
Expand Down Expand Up @@ -614,29 +621,32 @@ def test_create_shareableviz_process(


@pytest.mark.parametrize(
"platform, endpoint, bucket_name, include_hooks",
"platform, endpoint, bucket_name, include_hooks, package_name",
[
(
"azure",
"https://example-bucket.web.core.windows.net",
"example-bucket",
False,
"demo_project",
),
(
"aws",
"http://example-bucket.s3-website.us-east-2.amazonaws.com/",
"example-bucket",
True,
"demo_project",
),
("gcp", "http://34.120.87.227/", "example-bucket", False),
("local", None, None, True),
("gcp", "http://34.120.87.227/", "example-bucket", False, "demo_project"),
("local", None, None, True, "demo_project"),
],
)
def test_load_and_deploy_viz_success(
platform,
endpoint,
bucket_name,
include_hooks,
package_name,
mock_DeployerFactory,
mock_load_and_populate_data,
mock_process_completed,
Expand All @@ -651,12 +661,13 @@ def test_load_and_deploy_viz_success(
endpoint,
bucket_name,
include_hooks,
package_name,
mock_process_completed,
mock_exception_queue,
)

mock_load_and_populate_data.assert_called_once_with(
mock_project_path, include_hooks=include_hooks
mock_project_path, include_hooks=include_hooks, package_name=package_name
)
mock_DeployerFactory.create_deployer.assert_called_once_with(
platform, endpoint, bucket_name
Expand Down
3 changes: 3 additions & 0 deletions package/tests/test_launchers/test_jupyter.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def test_run_viz(self, mocker, patched_check_viz_up):
"env": None,
"autoreload": False,
"include_hooks": False,
"package_name": None,
"extra_params": "",
},
)
Expand All @@ -59,6 +60,7 @@ def test_run_viz(self, mocker, patched_check_viz_up):
"env": None,
"autoreload": False,
"include_hooks": True,
"package_name": None,
"extra_params": "",
},
)
Expand Down Expand Up @@ -105,6 +107,7 @@ def test_run_viz_on_databricks(self, mocker, patched_check_viz_up, monkeypatch):
"env": None,
"autoreload": False,
"include_hooks": False,
"package_name": None,
"extra_params": "",
},
)
Expand Down
Loading