Skip to content

Commit b3c955e

Browse files
Mike ProsserMike Prosser
authored andcommitted
add checks to _get_python_path()
1 parent b5ac513 commit b3c955e

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

src/nipanel/_panel.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def __init__(
3737
grpc_channel=grpc_channel,
3838
)
3939
self._panel_script_path = panel_script_path
40-
python_path = str(Path(sys.executable).resolve())
40+
python_path = self._get_python_path()
4141
self._panel_url = self._panel_client.start_panel(panel_id, panel_script_path, python_path)
4242

4343
@property
@@ -49,3 +49,12 @@ def panel_script_path(self) -> str:
4949
def panel_url(self) -> str:
5050
"""Read-only accessor for the panel URL."""
5151
return self._panel_url
52+
53+
def _get_python_path(self) -> str:
54+
"""Get the Python path for the panel."""
55+
if getattr(sys, "frozen", False):
56+
raise RuntimeError("Panel cannot be used in a frozen application (e.g., PyInstaller).")
57+
python_path = str(Path(sys.executable).resolve())
58+
if python_path is None or python_path == "":
59+
raise RuntimeError("Python environment not found")
60+
return python_path

tests/unit/test_streamlit_panel.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,15 @@ def test___panel___panel_is_running_and_in_memory(
350350
assert is_panel_running(panel)
351351

352352

353+
def test___panel___python_path_is_in_venv(
354+
fake_python_panel_service: FakePythonPanelService,
355+
fake_panel_channel: grpc.Channel,
356+
) -> None:
357+
StreamlitPanel("my_panel", "path/to/script", grpc_channel=fake_panel_channel)
358+
359+
assert ".venv" in fake_python_panel_service.servicer.python_path
360+
361+
353362
def is_panel_in_memory(panel: StreamlitPanel) -> bool:
354363
return panel.panel_id in panel._panel_client.enumerate_panels().keys()
355364

tests/utils/_fake_python_panel_servicer.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ def __init__(self) -> None:
2727
self._panel_value_ids: dict[str, dict[str, Any]] = {}
2828
self._fail_next_start_panel = False
2929
self._notification_count: int = 0
30+
self._python_path: str = ""
3031

3132
def StartPanel( # noqa: N802
3233
self, request: StartPanelRequest, context: Any
3334
) -> StartPanelResponse:
3435
"""Trivial implementation for testing."""
36+
self._python_path = request.python_path
3537
if self._fail_next_start_panel:
3638
self._fail_next_start_panel = False
3739
context.abort(grpc.StatusCode.UNAVAILABLE, "Simulated failure")
@@ -81,6 +83,11 @@ def notification_count(self) -> int:
8183
"""Get the number of notifications sent from SetValue."""
8284
return self._notification_count
8385

86+
@property
87+
def python_path(self) -> str:
88+
"""Get the Python path used to start the panel."""
89+
return self._python_path
90+
8491
def _init_panel(self, panel_id: str) -> None:
8592
if panel_id not in self._panel_ids:
8693
self._panel_ids.append(panel_id)

0 commit comments

Comments
 (0)