Skip to content

Commit

Permalink
Fix %run_viz using old process in jupyter notebook (#2267)
Browse files Browse the repository at this point in the history
* fix for notebook common process

Signed-off-by: ravi_kumar_pilla <[email protected]>

* add sanity check to allocate port

Signed-off-by: ravi_kumar_pilla <[email protected]>

* update release note

Signed-off-by: ravi_kumar_pilla <[email protected]>

* fix docs

Signed-off-by: ravi_kumar_pilla <[email protected]>

---------

Signed-off-by: ravi_kumar_pilla <[email protected]>
  • Loading branch information
ravi-kumar-pilla authored Feb 10, 2025
1 parent 60b6a81 commit 0690aaf
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 24 deletions.
9 changes: 9 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ 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
-->
# Upcoming Release

## Major features and improvements

## Bug fixes and other changes

- Fix `%run_viz` using old process in jupyter notebook. (#2267)

## Community contributions

# Release 10.2.0

Expand Down
2 changes: 1 addition & 1 deletion docs/source/publish_and_share_kedro_viz_on_gcp.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,4 @@ You can control who can view your visualisation using [IAM permissions and ACLs]

Kedro-Viz does not handle billing. You pay for storing objects on your Google Cloud Storage. The amount you pay depends on the amount of data stored, data processing and network usage. Additionally you may be charged for using cloud load balancing.

See the official [Google Cloud Storage Billing](https://cloud.google.com/storage/pricing) and [Google Cloud Load Balancer Billing](https://cloud.google.com/vpc/network-pricing#lb) for more information.
See the official [Google Cloud Storage Billing](https://cloud.google.com/storage/pricing) and [Google Cloud Load Balancer Billing](https://cloud.google.com/vpc/network-pricing) for more information.
25 changes: 2 additions & 23 deletions package/kedro_viz/launchers/jupyter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import multiprocessing
import os
import shlex
import socket
from contextlib import closing
from typing import Any, Dict

import IPython
Expand All @@ -17,7 +15,7 @@
from watchfiles import run_process

from kedro_viz.autoreload_file_filter import AutoreloadFileFilter
from kedro_viz.launchers.utils import _check_viz_up, _wait_for
from kedro_viz.launchers.utils import _check_viz_up, _find_available_port, _wait_for
from kedro_viz.server import DEFAULT_HOST, DEFAULT_PORT, run_server

_VIZ_PROCESSES: Dict[str, int] = {}
Expand All @@ -26,25 +24,6 @@
logger = logging.getLogger(__name__)


def _allocate_port(host: str, start_at: int, end_at: int = 65535) -> int:
acceptable_ports = range(start_at, end_at + 1)

viz_ports = _VIZ_PROCESSES.keys() & set(acceptable_ports)
if viz_ports: # reuse one of already allocated ports
return sorted(viz_ports)[0]

socket.setdefaulttimeout(2.0) # seconds
for port in acceptable_ports: # iterate through all acceptable ports
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
if sock.connect_ex((host, port)) != 0: # port is available
return port

raise ValueError(
"Cannot allocate an open TCP port for Kedro-Viz in a range "
f"from {start_at} to {end_at}"
)


def _is_databricks() -> bool:
return "DATABRICKS_RUNTIME_VERSION" in os.environ

Expand Down Expand Up @@ -120,7 +99,7 @@ def run_viz(args: str = "", local_ns: Dict[str, Any] = None) -> None:
params = arg_dict.get("params", "")

# Allocate port
port = _allocate_port(host, start_at=port)
port = _find_available_port(host, port, max_attempts=10)

# Terminate existing process if needed
if port in _VIZ_PROCESSES and _VIZ_PROCESSES[port].is_alive():
Expand Down
8 changes: 8 additions & 0 deletions package/kedro_viz/launchers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ def _is_port_in_use(host: str, port: int):
def _find_available_port(host: str, start_port: int, max_attempts: int = 5) -> int:
max_port = start_port + max_attempts - 1
port = start_port

# Sanity check to not collide with system ports
if port > 49151:
raise ValueError(
"Cannot allocate an open TCP port for Kedro-Viz in a range "
f"from {start_port} to {max_port}"
)

while port <= max_port:
if not _is_port_in_use(host, port):
return port
Expand Down

0 comments on commit 0690aaf

Please sign in to comment.