Skip to content

Commit 92d200d

Browse files
Mike ProsserMike Prosser
authored andcommitted
panel_id should be specified by the user. Also, lets provide readonly accessors for that and panel_uri.
1 parent 166774d commit 92d200d

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

src/nipanel/_panel.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import sys
4-
import uuid
54
from abc import ABC, abstractmethod
65
from types import TracebackType
76
from typing import Optional, Type, TYPE_CHECKING
@@ -24,10 +23,20 @@ class Panel(ABC):
2423

2524
__slots__ = ["_stub", "_panel_uri", "_panel_id", "__weakref__"]
2625

27-
def __init__(self, panel_uri: str) -> None:
26+
def __init__(self, panel_uri: str, panel_id: str) -> None:
2827
"""Initialize the panel."""
2928
self._panel_uri = panel_uri
30-
self._panel_id = str(uuid.uuid4())
29+
self._panel_id = panel_id
30+
31+
@property
32+
def panel_uri(self) -> str:
33+
"""Read-only accessor for the panel URI."""
34+
return self._panel_uri
35+
36+
@property
37+
def panel_id(self) -> str:
38+
"""Read-only accessor for the panel ID."""
39+
return self._panel_id
3140

3241
def __enter__(self) -> Self:
3342
"""Enter the runtime context related to this object."""

src/nipanel/_streamlit_panel.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@ class StreamlitPanel(Panel):
66

77
__slots__ = ()
88

9-
def __init__(self, streamlit_script_uri: str) -> None:
9+
def __init__(self, streamlit_script_uri: str, panel_id: str) -> None:
1010
"""Create a panel using a Streamlit script for the user interface.
1111
1212
Args:
1313
streamlit_script_uri: The file path of the Streamlit script.
14+
panel_id: A unique identifier for the panel.
1415
1516
Returns:
1617
A new StreamlitPanel instance.
1718
"""
18-
super().__init__(streamlit_script_uri)
19+
super().__init__(streamlit_script_uri, panel_id)
1920

2021
def _resolve_service_location(self) -> str:
2122
# TODO: AB#3095680 - resolve to the Streamlit PythonPanelService

tests/unit/test_panel.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,13 @@
22

33

44
def test___streamlit_panel___has_panel_id_and_panel_uri() -> None:
5-
panel = nipanel.StreamlitPanel("path/to/script")
6-
assert panel._panel_id is not None
7-
assert panel._panel_uri == "path/to/script"
8-
9-
10-
def test___two_panels___have_different_panel_ids() -> None:
11-
panel1 = nipanel.StreamlitPanel("path/to/script1")
12-
panel2 = nipanel.StreamlitPanel("path/to/script2")
13-
assert panel1._panel_id != panel2._panel_id
5+
panel = nipanel.StreamlitPanel("path/to/script", "my_panel")
6+
assert panel.panel_uri == "path/to/script"
7+
assert panel.panel_id == "my_panel"
148

159

1610
def test___connected_panel___set_value___gets_same_value() -> None:
17-
panel = nipanel.StreamlitPanel("path/to/script")
11+
panel = nipanel.StreamlitPanel("path/to/script", "my_panel")
1812
panel.connect()
1913

2014
panel.set_value("test_id", "test_value")
@@ -25,7 +19,7 @@ def test___connected_panel___set_value___gets_same_value() -> None:
2519

2620

2721
def test___with_panel___set_value___gets_same_value() -> None:
28-
with nipanel.StreamlitPanel("path/to/script") as panel:
22+
with nipanel.StreamlitPanel("path/to/script", "my_panel") as panel:
2923

3024
panel.set_value("test_id", "test_value")
3125

0 commit comments

Comments
 (0)