diff --git a/RELEASE.md b/RELEASE.md index c287a896a8..fae46a37ed 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -16,6 +16,7 @@ Please follow the established format: - Fix incorrect rendering of datasets in modular pipelines. (#1439) - Fix broken SVG/PNG exports in light theme. (#1463) - Fix dataset and global toolbar error with standalone React component (#1351) +- Fix `ImportError` as kedro-datasets is now lazily loaded (#1481). # Release 6.3.4 diff --git a/package/kedro_viz/integrations/kedro/data_loader.py b/package/kedro_viz/integrations/kedro/data_loader.py index 4b78b44d95..ca4b1565b6 100644 --- a/package/kedro_viz/integrations/kedro/data_loader.py +++ b/package/kedro_viz/integrations/kedro/data_loader.py @@ -21,7 +21,7 @@ plotly, tracking, ) -except ImportError: +except ImportError: # kedro_datasets is not installed. from kedro.extras.datasets import ( # Safe since ImportErrors are suppressed within kedro. json as json_dataset, matplotlib, @@ -158,12 +158,12 @@ def load_data( return context.catalog, context.pipelines, session_store, stats_dict -# The dataset type is available as an attribute if and only if the import from kedro -# did not suppress an ImportError. i.e. hasattr(matplotlib, "MatplotlibWriter") is True -# when matplotlib dependencies are installed. +# Try to access the attribute to trigger the import of dependencies, only modify the _load +# if dependencies are installed. # These datasets do not have _load methods defined (tracking and matplotlib) or do not # load to json (plotly), hence the need to define _load here. -if hasattr(matplotlib, "MatplotlibWriter"): +try: + getattr(matplotlib, "MatplotlibWriter") # Trigger the lazy import def matplotlib_writer_load(dataset: matplotlib.MatplotlibWriter) -> str: load_path = get_filepath_str(dataset._get_load_path(), dataset._protocol) @@ -172,15 +172,29 @@ def matplotlib_writer_load(dataset: matplotlib.MatplotlibWriter) -> str: return base64_bytes.decode("utf-8") matplotlib.MatplotlibWriter._load = matplotlib_writer_load +except (ImportError, AttributeError): + pass -if hasattr(plotly, "JSONDataSet"): +try: + getattr(plotly, "JSONDataSet") # Trigger import plotly.JSONDataSet._load = json_dataset.JSONDataSet._load +except (ImportError, AttributeError): + pass -if hasattr(plotly, "PlotlyDataSet"): +try: + getattr(plotly, "PlotlyDataSet") # Trigger import plotly.PlotlyDataSet._load = json_dataset.JSONDataSet._load +except (ImportError, AttributeError): + pass -if hasattr(tracking, "JSONDataSet"): +try: + getattr(tracking, "JSONDataSet") # Trigger import tracking.JSONDataSet._load = json_dataset.JSONDataSet._load +except (ImportError, AttributeError): + pass -if hasattr(tracking, "MetricsDataSet"): +try: + getattr(tracking, "MetricsDataSet") # Trigger import tracking.MetricsDataSet._load = json_dataset.JSONDataSet._load +except (ImportError, AttributeError): + pass diff --git a/package/kedro_viz/models/flowchart.py b/package/kedro_viz/models/flowchart.py index bcf1d9b232..a2638851a1 100644 --- a/package/kedro_viz/models/flowchart.py +++ b/package/kedro_viz/models/flowchart.py @@ -595,7 +595,9 @@ def __post_init__(self, data_node: DataNode, dataset_stats: Dict): self.tracking_data = dataset.load() elif data_node.is_preview_node(): try: - self.preview = dataset._preview(**data_node.get_preview_args()) + if hasattr(dataset, "_preview"): + self.preview = dataset._preview(**data_node.get_preview_args()) + except Exception as exc: # pylint: disable=broad-except # pragma: no cover logger.warning( "'%s' could not be previewed. Full exception: %s: %s",