Skip to content

Commit 5034907

Browse files
Mike ProsserMike Prosser
authored andcommitted
refactor to make StreamlitPanel public instead of Panel
1 parent 9b5c99f commit 5034907

File tree

6 files changed

+65
-53
lines changed

6 files changed

+65
-53
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"python.testing.pytestArgs": [
3-
"tests"
3+
"."
44
],
55
"python.testing.unittestEnabled": false,
66
"python.testing.pytestEnabled": true

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,7 @@ ignore_missing_imports = true
5252
skips = [
5353
"B101", # assert_used
5454
]
55+
56+
[tool.pytest.ini_options]
57+
addopts = "--doctest-modules --strict-markers"
58+
testpaths = ["src/nipanel", "tests"]

src/nipanel/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""The NI Panel."""
22

3-
from nipanel._panel import Panel
3+
from nipanel._streamlit_panel import StreamlitPanel
44

5-
__all__ = ["Panel"]
5+
__all__ = ["StreamlitPanel"]
66

77
# Hide that it was defined in a helper file
8-
Panel.__module__ = __name__
8+
StreamlitPanel.__module__ = __name__

src/nipanel/_panel.py

Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@
1818
class Panel(ABC):
1919
"""This class allows you to connect to a panel and specify values for its controls."""
2020

21+
_stub: PythonPanelServiceStub | None
22+
_panel_uri: str
23+
_panel_id: str
24+
25+
__slots__ = ["_stub", "_panel_uri", "_panel_id", "__weakref__"]
26+
27+
def __init__(self, panel_uri: str) -> None:
28+
"""Initialize the panel."""
29+
self._panel_uri = panel_uri
30+
self._panel_id = str(uuid.uuid4())
31+
2132
def __enter__(self) -> Self:
2233
"""Enter the runtime context related to this object."""
2334
self.connect()
@@ -33,17 +44,16 @@ def __exit__(
3344
self.disconnect()
3445
return None
3546

36-
@abstractmethod
3747
def connect(self) -> None:
3848
"""Connect to the panel and open it."""
39-
pass
49+
# TODO: AB#3095680 - Use gRPC pool management, create the _stub, and call _stub.Connect
50+
self._resolve_service_location()
4051

41-
@abstractmethod
4252
def disconnect(self) -> None:
4353
"""Disconnect from the panel (does not close the panel)."""
54+
# TODO: AB#3095680 - Use gRPC pool management, call _stub.Disconnect
4455
pass
4556

46-
@abstractmethod
4757
def get_value(self, value_id: str) -> object:
4858
"""Get the value for a control on the panel.
4959
@@ -53,56 +63,20 @@ def get_value(self, value_id: str) -> object:
5363
Returns:
5464
The value
5565
"""
56-
pass
66+
# TODO: AB#3095681 - get the Any from _stub.GetValue and convert it to the correct type
67+
return "placeholder value"
5768

58-
@abstractmethod
5969
def set_value(self, value_id: str, value: object) -> None:
6070
"""Set the value for a control on the panel.
6171
6272
Args:
6373
value_id: The id of the value
6474
value: The value
6575
"""
66-
pass
67-
68-
@classmethod
69-
def streamlit_panel(cls, streamlit_script_path: str) -> Panel:
70-
"""Create a panel using a streamlit script for the user interface.
71-
72-
Args:
73-
streamlit_script_path: The file path of the Streamlit script.
74-
75-
Returns:
76-
A new StreamlitPanel instance.
77-
"""
78-
return _StreamlitPanel(streamlit_script_path)
79-
80-
81-
class _StreamlitPanel(Panel):
82-
83-
_stub: PythonPanelServiceStub | None
84-
_streamlit_script_uri: str
85-
_panel_id: str
86-
87-
__slots__ = ["_stub", "_streamlit_script_uri", "_panel_id"]
88-
89-
def __init__(self, streamlit_script_uri: str):
90-
self._streamlit_script_uri = streamlit_script_uri
91-
self._panel_id = str(uuid.uuid4())
92-
self._stub = None # Initialize the gRPC stub
93-
94-
def connect(self) -> None:
95-
# TODO: AB#3095680 - Use gRPC pool management, create the _stub, and call _stub.Connect
96-
pass
97-
98-
def disconnect(self) -> None:
99-
# TODO: AB#3095680 - Use gRPC pool management, call _stub.Disconnect
100-
pass
101-
102-
def get_value(self, value_id: str) -> object:
103-
# TODO: AB#3095681 - get the Any from _stub.GetValue and convert it to the correct type
104-
return "placeholder value"
105-
106-
def set_value(self, value_id: str, value: object) -> None:
10776
# TODO: AB#3095681 - Convert the value to an Any and pass it to _stub.SetValue
10877
pass
78+
79+
@abstractmethod
80+
def _resolve_service_location(self) -> str:
81+
"""Resolve the service location for the panel."""
82+
raise NotImplementedError

src/nipanel/_streamlit_panel.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from nipanel._panel import Panel
2+
3+
4+
class StreamlitPanel(Panel):
5+
"""This class allows you to connect to a Streamlit panel and specify values for its controls."""
6+
7+
__slots__ = ()
8+
9+
def __init__(self, streamlit_script_uri: str) -> None:
10+
"""Create a panel using a Streamlit script for the user interface.
11+
12+
Args:
13+
streamlit_script_uri: The file path of the Streamlit script.
14+
15+
Returns:
16+
A new StreamlitPanel instance.
17+
"""
18+
super().__init__(streamlit_script_uri)
19+
20+
def _resolve_service_location(self) -> str:
21+
# TODO: AB#3095680 - resolve to the Streamlit PythonPanelService
22+
return ""

tests/unit/test_panel.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
import nipanel
22

33

4+
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
14+
15+
416
def test___connected_panel___set_value___gets_same_value() -> None:
5-
panel = nipanel.Panel.streamlit_panel("path/to/script")
17+
panel = nipanel.StreamlitPanel("path/to/script")
618
panel.connect()
719

820
panel.set_value("test_id", "test_value")
@@ -13,7 +25,7 @@ def test___connected_panel___set_value___gets_same_value() -> None:
1325

1426

1527
def test___with_panel___set_value___gets_same_value() -> None:
16-
with nipanel.Panel.streamlit_panel("path/to/script") as panel:
28+
with nipanel.StreamlitPanel("path/to/script") as panel:
1729

1830
panel.set_value("test_id", "test_value")
1931

0 commit comments

Comments
 (0)