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

Enhance Documentation for Dataset Previews #2074

Merged
merged 5 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
79 changes: 68 additions & 11 deletions docs/source/preview_custom_datasets.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@
JSONPreview = NewType("JSONPreview", dict)
```

Arbitrary arguments can be included in the `preview()` function, which can be later specified in the `catalog.yml` file.
## TablePreview

Check warning on line 14 in docs/source/preview_custom_datasets.md

View workflow job for this annotation

GitHub Actions / vale

[vale] docs/source/preview_custom_datasets.md#L14

[Kedro-viz.headings] 'TablePreview' should use sentence-style capitalization.
Raw output
{"message": "[Kedro-viz.headings] 'TablePreview' should use sentence-style capitalization.", "location": {"path": "docs/source/preview_custom_datasets.md", "range": {"start": {"line": 14, "column": 4}}}, "severity": "WARNING"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could do it for every Preview we have

For `TablePreview`, the returned dictionary must contain the following keys:

`index`: A list of row indices.
`columns`: A list of column names.
`data`: A list of rows, where each row is itself a list of values.

Arbitrary arguments can be included in the `preview()` function, which can be later specified in the `catalog.yml` file. Ensure that these arguments (like `nrows`, `ncolumns`, and `filters`) match the structure of your dataset.

Below is an example demonstrating how to implement the `preview()` function with user-specified arguments for a `CustomDataset` class that utilizes `TablePreview` to enable previewing tabular data on Kedro-Viz:

```yaml
companies:
type: CustomDataset
type: CustomTableDataset
filepath: ${_base_location}/01_raw/companies.csv
metadata:
kedro-viz:
Expand All @@ -34,19 +41,69 @@

from kedro_datasets._typing import TablePreview

class CustomDataset:
class CustomTableDataset:
def preview(self, nrows, ncolumns, filters) -> TablePreview:
filtered_data = self.data
data = self.load()
for column, value in filters.items():
filtered_data = filtered_data[filtered_data[column] == value]
subset = filtered_data.iloc[:nrows, :ncolumns]
df_dict = {}
for column in subset.columns:
df_dict[column] = subset[column]
return df_dict

data = data[data[column] == value]
astrojuanlu marked this conversation as resolved.
Show resolved Hide resolved
subset = data.iloc[:nrows, :ncolumns]
preview_data = {
'index': list(subset.index), # List of row indices
'columns': list(subset.columns), # List of column names
'data': subset.values.tolist() # List of rows, where each row is a list of values
}
return preview_data
```

## ImagePreview

Check warning on line 58 in docs/source/preview_custom_datasets.md

View workflow job for this annotation

GitHub Actions / vale

[vale] docs/source/preview_custom_datasets.md#L58

[Kedro-viz.headings] 'ImagePreview' should use sentence-style capitalization.
Raw output
{"message": "[Kedro-viz.headings] 'ImagePreview' should use sentence-style capitalization.", "location": {"path": "docs/source/preview_custom_datasets.md", "range": {"start": {"line": 58, "column": 4}}}, "severity": "WARNING"}
For `ImagePreview`, the function should return a base64-encoded string representing the image. This is typically used for datasets that output visual data such as plots or images.

Below is an example implementation:

```python

from kedro_datasets._typing import ImagePreview

class CustomImageDataset:
def preview(self) -> ImagePreview:
image_path = self._get_image_path()
with open(image_path, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
return ImagePreview(encoded_string)
```

## PlotlyPreview

Check warning on line 75 in docs/source/preview_custom_datasets.md

View workflow job for this annotation

GitHub Actions / vale

[vale] docs/source/preview_custom_datasets.md#L75

[Kedro-viz.headings] 'PlotlyPreview' should use sentence-style capitalization.
Raw output
{"message": "[Kedro-viz.headings] 'PlotlyPreview' should use sentence-style capitalization.", "location": {"path": "docs/source/preview_custom_datasets.md", "range": {"start": {"line": 75, "column": 4}}}, "severity": "WARNING"}
For `PlotlyPreview`, the function should return a dictionary containing Plotly figure data. This includes the figure's `data` and `layout` keys.

Below is an example implementation:

```python

from kedro_datasets._typing import PlotlyPreview

class CustomPlotlyDataset:
def preview(self) -> PlotlyPreview:
figure = self._load_plotly_figure()
return PlotlyPreview({
"data": figure["data"],
"layout": figure["layout"]
})
```

## JSONPreview

Check warning on line 93 in docs/source/preview_custom_datasets.md

View workflow job for this annotation

GitHub Actions / vale

[vale] docs/source/preview_custom_datasets.md#L93

[Kedro-viz.Spellings] Did you really mean 'JSONPreview'?
Raw output
{"message": "[Kedro-viz.Spellings] Did you really mean 'JSONPreview'?", "location": {"path": "docs/source/preview_custom_datasets.md", "range": {"start": {"line": 93, "column": 4}}}, "severity": "WARNING"}

Check warning on line 93 in docs/source/preview_custom_datasets.md

View workflow job for this annotation

GitHub Actions / vale

[vale] docs/source/preview_custom_datasets.md#L93

[Kedro-viz.headings] 'JSONPreview' should use sentence-style capitalization.
Raw output
{"message": "[Kedro-viz.headings] 'JSONPreview' should use sentence-style capitalization.", "location": {"path": "docs/source/preview_custom_datasets.md", "range": {"start": {"line": 93, "column": 4}}}, "severity": "WARNING"}
For `JSONPreview`, the function should return a dictionary representing the `JSON` data. This is useful for previewing complex nested data structures.

Below is an example implementation:

```python

from kedro_datasets._typing import JSONPreview

class CustomJSONDataset:
def preview(self) -> JSONPreview:
json_data = self._load_json_data()
return JSONPreview(json.dumps(json_data))
```

## Examples of Previews

Expand Down
2 changes: 2 additions & 0 deletions docs/source/preview_datasets.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ companies:
preview: false
```

You can also disable previews globally through the settings menu on Kedro-Viz.

```{note}
Starting from Kedro-Viz 9.2.0, previews are disabled by default for the CLI commands `kedro viz deploy` and `kedro viz build`. You can control this behavior using the `--include-previews` flag with these commands. For `kedro viz run`, previews are enabled by default and can be controlled from the publish modal dialog, refer to the [Publish and share](./share_kedro_viz) for more instructions.
```
Loading