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

Replace watchgod library with watchfiles #2134

Merged
merged 29 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
29 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
1 change: 1 addition & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Please follow the established format:

- Improve `kedro viz build` usage documentation (#2126)
- Fix unserializable parameters value (#2122)
- Replace `watchgod` library with `watchfiles` (#2134)


# Release 10.0.0
Expand Down
2 changes: 1 addition & 1 deletion package/features/steps/lower_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ fastapi==0.100.0
fsspec==2021.4
aiofiles==22.1.0
uvicorn[standard]==0.22.0
watchgod==0.8.2
watchfiles==0.24.0
plotly==4.8
packaging==23.0
pandas==1.3; python_version < '3.10'
Expand Down
13 changes: 8 additions & 5 deletions package/kedro_viz/launchers/cli/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from kedro_viz.constants import DEFAULT_HOST, DEFAULT_PORT
from kedro_viz.launchers.cli.main import viz
from kedro_viz.utils import file_extension_filter

_VIZ_PROCESSES: Dict[str, int] = {}

Expand Down Expand Up @@ -164,17 +165,19 @@ def run(
"is_lite": lite,
}
if autoreload:
from watchgod import RegExpWatcher, run_process
from watchfiles import run_process

run_process_args = [str(kedro_project_path)]
ravi-kumar-pilla marked this conversation as resolved.
Show resolved Hide resolved
run_process_kwargs = {
"path": kedro_project_path,
"target": run_server,
"kwargs": run_server_kwargs,
"watcher_cls": RegExpWatcher,
"watcher_kwargs": {"re_files": r"^.*(\.yml|\.yaml|\.py|\.json)$"},
"watch_filter": file_extension_filter,
}
viz_process = multiprocessing.Process(
target=run_process, daemon=False, kwargs={**run_process_kwargs}
target=run_process,
daemon=False,
args=run_process_args,
kwargs={**run_process_kwargs},
)
else:
viz_process = multiprocessing.Process(
Expand Down
13 changes: 8 additions & 5 deletions package/kedro_viz/launchers/jupyter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
import IPython
from IPython.display import HTML, display
from kedro.framework.project import PACKAGE_NAME
from watchgod import RegExpWatcher, run_process
from watchfiles import run_process

from kedro_viz.launchers.utils import _check_viz_up, _wait_for
from kedro_viz.server import DEFAULT_HOST, DEFAULT_PORT, run_server
from kedro_viz.utils import file_extension_filter

_VIZ_PROCESSES: Dict[str, int] = {}
_DATABRICKS_HOST = "0.0.0.0"
Expand Down Expand Up @@ -148,15 +149,17 @@ def run_viz( # pylint: disable=too-many-locals
}
process_context = multiprocessing.get_context("spawn")
if autoreload:
run_process_args = [str(project_path)]
run_process_kwargs = {
"path": project_path,
"target": run_server,
"kwargs": run_server_kwargs,
"watcher_cls": RegExpWatcher,
"watcher_kwargs": {"re_files": r"^.*(\.yml|\.yaml|\.py|\.json)$"},
"watch_filter": file_extension_filter,
}
viz_process = process_context.Process(
target=run_process, daemon=False, kwargs={**run_process_kwargs}
target=run_process,
daemon=False,
args=run_process_args,
kwargs={**run_process_kwargs},
)
else:
viz_process = process_context.Process(
Expand Down
13 changes: 8 additions & 5 deletions package/kedro_viz/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from kedro_viz.integrations.kedro import data_loader as kedro_data_loader
from kedro_viz.integrations.kedro.sqlite_store import SQLiteStore
from kedro_viz.launchers.utils import _check_viz_up, _wait_for
from kedro_viz.utils import file_extension_filter

DEV_PORT = 4142

Expand Down Expand Up @@ -142,7 +143,7 @@ def run_server(
import argparse
import multiprocessing

from watchgod import RegExpWatcher, run_process
from watchfiles import run_process

parser = argparse.ArgumentParser(description="Launch a development viz server")
parser.add_argument("project_path", help="Path to a Kedro project")
Expand All @@ -156,20 +157,22 @@ def run_server(

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

run_process_args = [str(project_path)]
run_process_kwargs = {
"path": project_path,
"target": run_server,
"kwargs": {
"host": args.host,
"port": args.port,
"project_path": str(project_path),
},
"watcher_cls": RegExpWatcher,
"watcher_kwargs": {"re_files": r"^.*(\.yml|\.yaml|\.py|\.json)$"},
"watch_filter": file_extension_filter,
}

viz_process = multiprocessing.Process(
target=run_process, daemon=False, kwargs={**run_process_kwargs}
target=run_process,
daemon=False,
args=run_process_args,
kwargs={**run_process_kwargs},
)

print("Starting Kedro Viz ...")
Expand Down
5 changes: 5 additions & 0 deletions package/kedro_viz/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,8 @@ def _strip_transcoding(element: str) -> str:
def is_dataset_param(dataset_name: str) -> bool:
"""Return whether a dataset is a parameter"""
return dataset_name.lower().startswith("params:") or dataset_name == "parameters"


def file_extension_filter(_, file_path: str) -> bool:
"""Determine if a given file path ends with one of the specified extensions."""
return file_path.endswith((".yml", ".yaml", ".py", ".json"))
2 changes: 1 addition & 1 deletion package/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ secure>=0.3.0
sqlalchemy>=1.4, <3
strawberry-graphql>=0.192.0, <1.0
uvicorn[standard]>=0.30.0, <1.0
watchgod>=0.8.2, <1.0
watchfiles>=0.24.0
13 changes: 8 additions & 5 deletions package/tests/test_launchers/test_cli/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import requests
from click.testing import CliRunner
from packaging.version import parse
from watchgod import RegExpWatcher, run_process
from watchfiles import run_process

from kedro_viz import __version__
from kedro_viz.launchers.cli import main
from kedro_viz.launchers.cli.run import _VIZ_PROCESSES
from kedro_viz.launchers.utils import _PYPROJECT
from kedro_viz.server import run_server
from kedro_viz.utils import file_extension_filter


@pytest.fixture
Expand Down Expand Up @@ -357,8 +358,8 @@ def test_kedro_viz_command_with_autoreload(
with runner.isolated_filesystem():
runner.invoke(main.viz_cli, ["viz", "run", "--autoreload"])

run_process_args = [str(mock_project_path)]
run_process_kwargs = {
"path": mock_project_path,
"target": run_server,
"kwargs": {
"host": "127.0.0.1",
Expand All @@ -374,11 +375,13 @@ def test_kedro_viz_command_with_autoreload(
"extra_params": {},
"is_lite": False,
},
"watcher_cls": RegExpWatcher,
"watcher_kwargs": {"re_files": "^.*(\\.yml|\\.yaml|\\.py|\\.json)$"},
"watch_filter": file_extension_filter,
}

process_init.assert_called_once_with(
target=run_process, daemon=False, kwargs={**run_process_kwargs}
target=run_process,
daemon=False,
args=run_process_args,
kwargs={**run_process_kwargs},
)
assert run_process_kwargs["kwargs"]["port"] in _VIZ_PROCESSES
1 change: 1 addition & 0 deletions package/tests/test_launchers/test_jupyter.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def test_run_viz_with_autoreload(self, mocker, patched_check_viz_up):
mock_process.assert_called_once_with(
target=mocker.ANY,
daemon=False, # No daemon for autoreload
args=mocker.ANY,
kwargs=mocker.ANY,
)

Expand Down
19 changes: 19 additions & 0 deletions package/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pytest

from kedro_viz.utils import file_extension_filter


@pytest.mark.parametrize(
"file_path, expected",
[
("config.yml", True),
("config.yaml", True),
("script.py", True),
("data.json", True),
("image.png", False),
("document.txt", False),
("archive.zip", False),
],
)
def test_file_extension_filter(file_path, expected):
assert file_extension_filter(None, file_path) == expected
Loading