Skip to content

Commit eaf0bc3

Browse files
Mike ProsserMike Prosser
authored andcommitted
address feedback
1 parent 5511c58 commit eaf0bc3

File tree

4 files changed

+64
-36
lines changed

4 files changed

+64
-36
lines changed

src/nipanel/__init__.py

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

3-
from nipanel.nipanel import NiPanel
3+
from nipanel._panel import Panel
44

5-
__all__ = ["NiPanel"]
5+
__all__ = ["Panel"]
6+
7+
# Hide that it was defined in a helper file
8+
Panel.__module__ = __name__
Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
1-
"""NI Panel."""
1+
from __future__ import annotations
22

3+
import sys
34
import uuid
45
from types import TracebackType
5-
from typing import Optional, Type
6+
from typing import Optional, Type, TYPE_CHECKING
67

8+
from ni.pythonpanel.v1.python_panel_service_pb2_grpc import PythonPanelServiceStub
79

8-
class NiPanel:
9-
"""This class allows you to access controls on the panel."""
10+
if TYPE_CHECKING:
11+
if sys.version_info >= (3, 11):
12+
from typing import Self
13+
else:
14+
from typing_extensions import Self
1015

11-
def __init__(self) -> None:
12-
"""Initialize the NiPanel instance."""
13-
self._stub = None # will be a PythonPanelServiceStub
14-
self.panel_uri = ""
15-
self.panel_id = ""
1616

17-
def __enter__(self) -> "NiPanel":
17+
class Panel:
18+
"""This class allows you to connect to a panel and specify values for its controls."""
19+
20+
_stub: PythonPanelServiceStub | None
21+
_panel_uri: str
22+
_panel_id: str
23+
24+
__slots__ = ["_stub", "_panel_uri", "_panel_id", "__weakref__"]
25+
26+
def __enter__(self) -> Self:
1827
"""Enter the runtime context related to this object."""
1928
self.connect()
2029
return self
@@ -30,18 +39,18 @@ def __exit__(
3039
return None
3140

3241
@classmethod
33-
def streamlit_panel(cls, streamlit_script_path: str) -> "NiPanel":
42+
def streamlit_panel(cls, streamlit_script_path: str) -> Self:
3443
"""Create a panel using a streamlit script for the user interface.
3544
3645
Args:
3746
streamlit_script_path: The file path of the streamlit script
3847
3948
Returns:
40-
NiPanel: A new panel associated with the streamlit script
49+
A new panel associated with the streamlit script
4150
"""
4251
panel = cls()
43-
panel.panel_uri = streamlit_script_path
44-
panel.panel_id = str(uuid.uuid4())
52+
panel._panel_uri = streamlit_script_path
53+
panel._panel_id = str(uuid.uuid4())
4554
return panel
4655

4756
def connect(self) -> None:
@@ -61,7 +70,7 @@ def get_value(self, value_id: str) -> object:
6170
value_id: The id of the value
6271
6372
Returns:
64-
object: The value
73+
The value
6574
"""
6675
# TODO: AB#3095681 - get the Any from _stub.GetValue and convert it to the correct type
6776
return "placeholder value"

tests/unit/test_nipanel.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

tests/unit/test_panel.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import nipanel
2+
3+
4+
def test___streamlit_panel___uri_and_id_are_set() -> None:
5+
panel = nipanel.Panel.streamlit_panel("path/to/script")
6+
7+
assert panel._panel_uri == "path/to/script"
8+
assert panel._panel_id is not None
9+
10+
11+
def test___two_panels___have_different_ids() -> None:
12+
panel1 = nipanel.Panel.streamlit_panel("path/to/script1")
13+
panel2 = nipanel.Panel.streamlit_panel("path/to/script2")
14+
15+
assert panel1._panel_id != panel2._panel_id
16+
17+
18+
def test___connected_panel___set_value___gets_same_value() -> None:
19+
panel = nipanel.Panel.streamlit_panel("path/to/script")
20+
panel.connect()
21+
22+
panel.set_value("test_id", "test_value")
23+
24+
# TODO: AB#3095681 - change asserted value to test_value
25+
assert panel.get_value("test_id") == "placeholder value"
26+
panel.disconnect()
27+
28+
29+
def test___with_panel___set_value___gets_same_value() -> None:
30+
with nipanel.Panel.streamlit_panel("path/to/script") as panel:
31+
32+
panel.set_value("test_id", "test_value")
33+
34+
# TODO: AB#3095681 - change asserted value to test_value
35+
assert panel.get_value("test_id") == "placeholder value"

0 commit comments

Comments
 (0)