Skip to content

Commit 1339535

Browse files
authoredOct 16, 2023
Skip optional plugins while running Kedro Viz (#1544)
* debugging skip hooks Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * initial draft to skip plugins Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * format and lint check Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * remove datasetstats hook from mandatory plugins Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * modify ignore plugins based on PR comments Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * fix lint issues Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * fix e2e tests Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * modify options flag and add read me Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * fix PR comments Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * addressing PR comments Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> --------- Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com>
1 parent c43fb03 commit 1339535

File tree

7 files changed

+91
-15
lines changed

7 files changed

+91
-15
lines changed
 

‎README.md

+2
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ Options:
115115
--autoreload Autoreload viz server when a Python or YAML file change in
116116
the Kedro project
117117

118+
--ignore-plugins A flag to ignore all installed plugins in the Kedro Project
119+
118120
--params TEXT Specify extra parameters that you want to pass to
119121
the context initializer. Items must be separated
120122
by comma, keys - by colon, example:

‎RELEASE.md

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Please follow the established format:
88

99
# Next Release
1010

11+
## Major features and improvements
12+
13+
- Skip all plugins while running Kedro Viz using the `--ignore-plugins` option. (#1544)
14+
1115
## Bug fixes and other changes
1216
- Fix improper display of 'run-command' inside the metadata panel. (#1569)
1317

‎package/kedro_viz/integrations/kedro/data_loader.py

+42-10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from typing import Any, Dict, Optional, Tuple
1313

1414
from kedro import __version__
15+
from kedro.framework.session import KedroSession
1516
from kedro.framework.session.store import BaseSessionStore
1617

1718
try:
@@ -32,10 +33,30 @@
3233
from kedro.io import DataCatalog
3334
from kedro.io.core import get_filepath_str
3435
from kedro.pipeline import Pipeline
35-
from semver import VersionInfo
36+
37+
from kedro_viz.constants import KEDRO_VERSION
3638

3739
logger = logging.getLogger(__name__)
38-
KEDRO_VERSION = VersionInfo.parse(__version__)
40+
41+
42+
class _VizNullPluginManager:
43+
"""This class creates an empty ``hook_manager`` that will ignore all calls to hooks
44+
and registered plugins allowing the runner to function if no ``hook_manager``
45+
has been instantiated.
46+
47+
NOTE: _VizNullPluginManager is a clone of _NullPluginManager class in Kedro.
48+
This was introduced to support the earliest version of Kedro which does not
49+
have _NullPluginManager defined
50+
"""
51+
52+
def __init__(self, *args, **kwargs):
53+
pass
54+
55+
def __getattr__(self, name):
56+
return self
57+
58+
def __call__(self, *args, **kwargs):
59+
pass
3960

4061

4162
def _bootstrap(project_path: Path):
@@ -58,7 +79,7 @@ def _bootstrap(project_path: Path):
5879
return
5980

6081

61-
def get_dataset_stats(project_path: Path) -> Dict:
82+
def _get_dataset_stats(project_path: Path) -> Dict:
6283
"""Return the stats saved at stats.json as a dictionary if found.
6384
If not, return an empty dictionary
6485
@@ -87,13 +108,15 @@ def get_dataset_stats(project_path: Path) -> Dict:
87108
def load_data(
88109
project_path: Path,
89110
env: Optional[str] = None,
111+
ignore_plugins: bool = False,
90112
extra_params: Optional[Dict[str, Any]] = None,
91113
) -> Tuple[DataCatalog, Dict[str, Pipeline], BaseSessionStore, Dict]:
92114
"""Load data from a Kedro project.
93115
Args:
94116
project_path: the path whether the Kedro project is located.
95117
env: the Kedro environment to load the data. If not provided.
96118
it will use Kedro default, which is local.
119+
ignore_plugins: the flag to unregister all installed plugins in a kedro project.
97120
extra_params: Optional dictionary containing extra project parameters
98121
for underlying KedroContext. If specified, will update (and therefore
99122
take precedence over) the parameters retrieved from the project
@@ -106,41 +129,46 @@ def load_data(
106129

107130
if KEDRO_VERSION.match(">=0.17.3"):
108131
from kedro.framework.project import pipelines
109-
from kedro.framework.session import KedroSession
110132

111133
with KedroSession.create(
112134
project_path=project_path,
113135
env=env, # type: ignore
114136
save_on_close=False,
115137
extra_params=extra_params, # type: ignore
116138
) as session:
139+
# check for --ignore-plugins option
140+
if ignore_plugins:
141+
session._hook_manager = _VizNullPluginManager()
142+
117143
context = session.load_context()
118144
session_store = session._store
119145
catalog = context.catalog
146+
120147
# Pipelines is a lazy dict-like object, so we force it to populate here
121148
# in case user doesn't have an active session down the line when it's first accessed.
122149
# Useful for users who have `get_current_session` in their `register_pipelines()`.
123150
pipelines_dict = dict(pipelines)
124-
stats_dict = get_dataset_stats(project_path)
151+
stats_dict = _get_dataset_stats(project_path)
125152

126153
return catalog, pipelines_dict, session_store, stats_dict
127154
elif KEDRO_VERSION.match(">=0.17.1"):
128-
from kedro.framework.session import KedroSession
129-
130155
with KedroSession.create(
131156
project_path=project_path,
132157
env=env, # type: ignore
133158
save_on_close=False,
134159
extra_params=extra_params, # type: ignore
135160
) as session:
161+
# check for --ignore-plugins option
162+
if ignore_plugins:
163+
session._hook_manager = _VizNullPluginManager()
164+
136165
context = session.load_context()
137166
session_store = session._store
138-
stats_dict = get_dataset_stats(project_path)
167+
stats_dict = _get_dataset_stats(project_path)
139168

140169
return context.catalog, context.pipelines, session_store, stats_dict
141170
else:
142171
# Since Viz is only compatible with kedro>=0.17.0, this just matches 0.17.0
143-
from kedro.framework.session import KedroSession
144172
from kedro.framework.startup import _get_project_metadata
145173

146174
metadata = _get_project_metadata(project_path)
@@ -151,9 +179,13 @@ def load_data(
151179
save_on_close=False,
152180
extra_params=extra_params, # type: ignore
153181
) as session:
182+
# check for --ignore-plugins option
183+
if ignore_plugins:
184+
session._hook_manager = _VizNullPluginManager()
185+
154186
context = session.load_context()
155187
session_store = session._store
156-
stats_dict = get_dataset_stats(project_path)
188+
stats_dict = _get_dataset_stats(project_path)
157189

158190
return context.catalog, context.pipelines, session_store, stats_dict
159191

‎package/kedro_viz/launchers/cli.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ def commands(): # pylint: disable=missing-function-docstring
7676
is_flag=True,
7777
help="Autoreload viz server when a Python or YAML file change in the Kedro project",
7878
)
79+
@click.option(
80+
"--ignore-plugins",
81+
is_flag=True,
82+
help="A flag to ignore all installed plugins in the Kedro Project",
83+
)
7984
@click.option(
8085
"--params",
8186
type=click.UNPROCESSED,
@@ -84,7 +89,18 @@ def commands(): # pylint: disable=missing-function-docstring
8489
callback=_split_params,
8590
)
8691
# pylint: disable=import-outside-toplevel, too-many-locals
87-
def viz(host, port, browser, load_file, save_file, pipeline, env, autoreload, params):
92+
def viz(
93+
host,
94+
port,
95+
browser,
96+
load_file,
97+
save_file,
98+
pipeline,
99+
env,
100+
autoreload,
101+
ignore_plugins,
102+
params,
103+
):
88104
"""Visualise a Kedro pipeline using Kedro viz."""
89105
from kedro_viz.server import run_server
90106

@@ -115,6 +131,7 @@ def viz(host, port, browser, load_file, save_file, pipeline, env, autoreload, pa
115131
"pipeline_name": pipeline,
116132
"env": env,
117133
"autoreload": autoreload,
134+
"ignore_plugins": ignore_plugins,
118135
"extra_params": params,
119136
}
120137
if autoreload:

‎package/kedro_viz/server.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ def run_server(
5353
env: Optional[str] = None,
5454
project_path: Optional[str] = None,
5555
autoreload: bool = False,
56+
ignore_plugins: bool = False,
5657
extra_params: Optional[Dict[str, Any]] = None,
57-
): # pylint: disable=redefined-outer-name
58+
): # pylint: disable=redefined-outer-name, too-many-locals
5859
"""Run a uvicorn server with a FastAPI app that either launches API response data from a file
5960
or from reading data from a real Kedro project.
6061
@@ -70,6 +71,7 @@ def run_server(
7071
autoreload: Whether the API app should support autoreload.
7172
project_path: the optional path of the Kedro project that contains the pipelines
7273
to visualise. If not supplied, the current working directory will be used.
74+
ignore_plugins: the flag to unregister all installed plugins in a kedro project.
7375
extra_params: Optional dictionary containing extra project parameters
7476
for underlying KedroContext. If specified, will update (and therefore
7577
take precedence over) the parameters retrieved from the project
@@ -81,7 +83,7 @@ def run_server(
8183

8284
if load_file is None:
8385
catalog, pipelines, session_store, stats_dict = kedro_data_loader.load_data(
84-
path, env, extra_params
86+
path, env, ignore_plugins, extra_params
8587
)
8688
pipelines = (
8789
pipelines

‎package/setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"kedro.global_commands": ["kedro-viz = kedro_viz.launchers.cli:commands"],
4848
"kedro.line_magic": ["line_magic = kedro_viz.launchers.jupyter:run_viz"],
4949
"kedro.hooks": [
50-
"plugin_name = kedro_viz.integrations.kedro.hooks:dataset_stats_hook"
50+
"kedro-dataset-stats = kedro_viz.integrations.kedro.hooks:dataset_stats_hook"
5151
],
5252
},
5353
)

‎package/tests/test_launchers/test_cli.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def patched_start_browser(mocker):
3232
"pipeline_name": None,
3333
"env": None,
3434
"autoreload": False,
35+
"ignore_plugins": False,
3536
"extra_params": {},
3637
},
3738
),
@@ -49,6 +50,7 @@ def patched_start_browser(mocker):
4950
"pipeline_name": None,
5051
"env": None,
5152
"autoreload": False,
53+
"ignore_plugins": False,
5254
"extra_params": {},
5355
},
5456
),
@@ -77,9 +79,24 @@ def patched_start_browser(mocker):
7779
"pipeline_name": "data_science",
7880
"env": "local",
7981
"autoreload": False,
82+
"ignore_plugins": False,
8083
"extra_params": {"extra_param": "param"},
8184
},
8285
),
86+
(
87+
["viz", "--ignore-plugins"],
88+
{
89+
"host": "127.0.0.1",
90+
"port": 4141,
91+
"load_file": None,
92+
"save_file": None,
93+
"pipeline_name": None,
94+
"env": None,
95+
"autoreload": False,
96+
"ignore_plugins": True,
97+
"extra_params": {},
98+
},
99+
),
83100
],
84101
)
85102
def test_kedro_viz_command_run_server(
@@ -91,8 +108,9 @@ def test_kedro_viz_command_run_server(
91108
):
92109
process_init = mocker.patch("multiprocessing.Process")
93110
runner = CliRunner()
94-
# Reduce the timeout argument from 60 to 1 to make test run faster.
111+
95112
mocker.patch("kedro_viz.launchers.cli._wait_for.__defaults__", (True, 1, True, 1))
113+
96114
with runner.isolated_filesystem():
97115
runner.invoke(cli.commands, command_options)
98116

@@ -178,6 +196,7 @@ def test_kedro_viz_command_with_autoreload(
178196
"env": None,
179197
"autoreload": True,
180198
"project_path": mock_project_path,
199+
"ignore_plugins": False,
181200
"extra_params": {},
182201
},
183202
"watcher_cls": RegExpWatcher,

0 commit comments

Comments
 (0)
Please sign in to comment.