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

CLI command kedro viz build added #1697

Merged
merged 29 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
66f177d
CLI command kedro viz build added
jitu5 Jan 4, 2024
cc7c144
Lint fix
jitu5 Jan 4, 2024
a389a83
lint fix
jitu5 Jan 4, 2024
6528d36
Lint fix
jitu5 Jan 4, 2024
5a9f2e1
add mypy ignore
jitu5 Jan 4, 2024
b86b578
Missing build file added
jitu5 Jan 8, 2024
40003f8
Lint error fix
jitu5 Jan 8, 2024
0c879a8
BaseDeployer class added
jitu5 Jan 9, 2024
5145ba4
Unused code removed
jitu5 Jan 9, 2024
26f6275
Fix lint issue
jitu5 Jan 10, 2024
c7ed79c
added base_deployer
rashidakanchwala Jan 10, 2024
11ac321
latest
rashidakanchwala Jan 10, 2024
42af3af
add deployer factory
rashidakanchwala Jan 11, 2024
7ad0905
Test and comments of deployers updated
jitu5 Jan 12, 2024
2a4e00f
fix lint
ravi-kumar-pilla Jan 15, 2024
ecb6a05
remove circular dependency
ravi-kumar-pilla Jan 15, 2024
90c285b
Tests and lint fixes
jitu5 Jan 15, 2024
eadb85c
Merge branch 'feature/cli-build' of https://github.com/kedro-org/kedr…
jitu5 Jan 15, 2024
d579bb7
Import fixes
jitu5 Jan 15, 2024
e8e25fa
Lint fixes
jitu5 Jan 15, 2024
0ad5301
Lint fixes
jitu5 Jan 15, 2024
98f11b2
Merge branch 'main' into feature/cli-build
jitu5 Jan 15, 2024
2ffc777
fix lint
rashidakanchwala Jan 15, 2024
4c977a6
fix return on deploy_get_url
rashidakanchwala Jan 15, 2024
d51634f
Exception and class method updated
jitu5 Jan 15, 2024
35b9775
Merge branch 'feature/cli-build' of https://github.com/kedro-org/kedr…
jitu5 Jan 15, 2024
0297a5f
User entered platform added in error
jitu5 Jan 16, 2024
b27cdaf
Fix test
jitu5 Jan 16, 2024
932e654
Merge branch 'main' into feature/cli-build
jitu5 Jan 16, 2024
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ Options:
-h, --help Show this message and exit.
```

To create build directory of local Kedro Viz instance with static data from the command line, use the following command from the root folder of your Kedro project:
jitu5 marked this conversation as resolved.
Show resolved Hide resolved

```bash
kedro viz build
```

Comment on lines +151 to +155
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we have some docs explaining how to view this build?

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, in another PR we will focus on the build docs

### Experiment Tracking usage

To enable [experiment tracking](https://docs.kedro.org/en/stable/experiment_tracking/index.html) in Kedro-Viz, you need to add the Kedro-Viz `SQLiteStore` to your Kedro project.
Expand Down
6 changes: 6 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ Please follow the established format:
- Use present tense (e.g. 'Add new feature')
- Include the ID number for the related PR (or PRs) in parentheses
-->

# Release 7.1.0

## Major features and improvements
- Add build command to the CLI using `kedro viz build` for creating build directory of local Kedro Viz instance with static data. (#1615)

# Release 7.0.0

## Major features and improvements
Expand Down
1 change: 1 addition & 0 deletions package/kedro_viz/api/graphql/router.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""`kedro_viz.api.graphql.router` defines GraphQL routes."""
# mypy: ignore-errors
from fastapi import APIRouter
from strawberry.asgi import GraphQL

Expand Down
49 changes: 49 additions & 0 deletions package/kedro_viz/launchers/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""`kedro_viz.launchers.cli` launches the viz server as a CLI app."""

import multiprocessing
import shutil
import traceback
from pathlib import Path
from typing import Dict
Expand All @@ -24,6 +25,8 @@
from kedro_viz.server import load_and_populate_data

_VIZ_PROCESSES: Dict[str, int] = {}
_HTML_DIR = Path(__file__).parent.parent.absolute() / "html"
_BUILD_PATH = "build"


@click.group(name="Kedro-Viz")
Expand Down Expand Up @@ -270,3 +273,49 @@ def deploy(region, bucket_name):
)
finally:
viz_deploy_timer.terminate()


@viz.command(context_settings={"help_option_names": ["-h", "--help"]})
def build():
jitu5 marked this conversation as resolved.
Show resolved Hide resolved
"""Create build directory of local Kedro Viz instance with static data"""

if not _HTML_DIR.exists():
click.echo(
click.style(
"ERROR: Directory containing Kedro Viz static files not found.",
fg="red",
),
)
return

try:
# Create the build directory if not present
build_path = Path(_BUILD_PATH)
build_path.mkdir(parents=True, exist_ok=True)

# Copy static files from Kedro Viz app to the build directory
copy_static_files(build_path)

click.echo(
click.style(
f"Kedro-Viz build files have been successfully added to the "
f"{build_path} directory.",
fg="green",
)
)

except Exception as ex:
traceback.print_exc()
merelcht marked this conversation as resolved.
Show resolved Hide resolved
raise KedroCliError(str(ex)) from ex


def copy_static_files(build_path: Path):
"""Copy static files from Kedro-Viz app to the build directory."""

# Check if the destination directory already exists
if build_path.exists():
# Remove existing directory
shutil.rmtree(build_path)

# Copy static files directly to the build directory
shutil.copytree(_HTML_DIR, build_path)
67 changes: 66 additions & 1 deletion package/tests/test_launchers/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from unittest.mock import call

import pytest
Expand All @@ -21,6 +22,20 @@ def patched_start_browser(mocker):
mocker.patch("kedro_viz.launchers.cli._start_browser")


@pytest.fixture
def path_operation(tmp_path):
build_path = tmp_path / "build"
static_files = tmp_path / "static"
static_files.mkdir(parents=True, exist_ok=True)
(static_files / "file1.txt").touch()
(static_files / "file2.txt").touch()

return {
"build_path": build_path,
"static_files": static_files,
}


@pytest.mark.parametrize(
"command_options,run_server_args",
[
Expand Down Expand Up @@ -234,7 +249,9 @@ def test_viz_command_group(mocker):
"Usage: Kedro-Viz viz [OPTIONS] COMMAND [ARGS]...\n\n "
"Visualise a Kedro pipeline using Kedro viz.\n\n"
"Options:\n --help Show this message and exit.\n\n"
"Commands:\n deploy Deploy and host Kedro Viz on AWS S3\n "
"Commands:\n build Create build directory of local Kedro Viz "
"instance with static data\n "
"deploy Deploy and host Kedro Viz on AWS S3\n "
"run Launch local Kedro Viz instance\x1b[0m"
),
]
Expand Down Expand Up @@ -324,3 +341,51 @@ def test_viz_deploy_invalid_region(mocker):
)
]
mock_click_echo.assert_has_calls(mock_click_echo_calls)


def test_successful_build_with_existing_static_files(mocker, path_operation):
env = path_operation
mocker.patch("kedro_viz.launchers.cli._HTML_DIR", env["static_files"])
mocker.patch("kedro_viz.launchers.cli._BUILD_PATH", env["build_path"])

runner = CliRunner()
result = runner.invoke(cli.build)

assert result.exit_code == 0
assert "successfully added" in result.output


def test_build_failure_with_missing_static_files(mocker, tmp_path):
mocker.patch("kedro_viz.launchers.cli._HTML_DIR", tmp_path / "non_existing")
mocker.patch("kedro_viz.launchers.cli._BUILD_PATH", tmp_path / "build")

runner = CliRunner()
result = runner.invoke(cli.build)

assert "ERROR" in result.output
assert "not found" in result.output


def test_copy_static_files_with_existing_build_directory(mocker, path_operation):
env = path_operation
mocker.patch("kedro_viz.launchers.cli._HTML_DIR", env["static_files"])
mocker.patch("kedro_viz.launchers.cli._BUILD_PATH", env["build_path"])

cli.copy_static_files(env["build_path"])

assert len(os.listdir(env["build_path"])) == 2
assert not os.path.exists(env["build_path"] / "static")


def test_build_with_exception(mocker, path_operation):
env = path_operation
mocker.patch("kedro_viz.launchers.cli._HTML_DIR", env["static_files"])
mocker.patch("kedro_viz.launchers.cli._BUILD_PATH", env["build_path"])

mocker.patch("shutil.copytree", side_effect=Exception("Test exception"))

runner = CliRunner()
result = runner.invoke(cli.build)

assert result.exit_code != 0
assert "Test exception" in result.output