Skip to content

Commit 9b5c99f

Browse files
Mike ProsserMike Prosser
authored andcommitted
refactor to use _StreamlitPanel subclass
1 parent eaf0bc3 commit 9b5c99f

File tree

2 files changed

+48
-40
lines changed

2 files changed

+48
-40
lines changed

src/nipanel/_panel.py

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import sys
44
import uuid
5+
from abc import ABC, abstractmethod
56
from types import TracebackType
67
from typing import Optional, Type, TYPE_CHECKING
78

@@ -14,15 +15,9 @@
1415
from typing_extensions import Self
1516

1617

17-
class Panel:
18+
class Panel(ABC):
1819
"""This class allows you to connect to a panel and specify values for its controls."""
1920

20-
_stub: PythonPanelServiceStub | None
21-
_panel_uri: str
22-
_panel_id: str
23-
24-
__slots__ = ["_stub", "_panel_uri", "_panel_id", "__weakref__"]
25-
2621
def __enter__(self) -> Self:
2722
"""Enter the runtime context related to this object."""
2823
self.connect()
@@ -38,31 +33,17 @@ def __exit__(
3833
self.disconnect()
3934
return None
4035

41-
@classmethod
42-
def streamlit_panel(cls, streamlit_script_path: str) -> Self:
43-
"""Create a panel using a streamlit script for the user interface.
44-
45-
Args:
46-
streamlit_script_path: The file path of the streamlit script
47-
48-
Returns:
49-
A new panel associated with the streamlit script
50-
"""
51-
panel = cls()
52-
panel._panel_uri = streamlit_script_path
53-
panel._panel_id = str(uuid.uuid4())
54-
return panel
55-
36+
@abstractmethod
5637
def connect(self) -> None:
5738
"""Connect to the panel and open it."""
58-
# TODO: AB#3095680 - Use gRPC pool management, create the _stub, and call _stub.Connect
5939
pass
6040

41+
@abstractmethod
6142
def disconnect(self) -> None:
6243
"""Disconnect from the panel (does not close the panel)."""
63-
# TODO: AB#3095680 - Use gRPC pool management, call _stub.Disconnect
6444
pass
6545

46+
@abstractmethod
6647
def get_value(self, value_id: str) -> object:
6748
"""Get the value for a control on the panel.
6849
@@ -72,15 +53,56 @@ def get_value(self, value_id: str) -> object:
7253
Returns:
7354
The value
7455
"""
75-
# TODO: AB#3095681 - get the Any from _stub.GetValue and convert it to the correct type
76-
return "placeholder value"
56+
pass
7757

58+
@abstractmethod
7859
def set_value(self, value_id: str, value: object) -> None:
7960
"""Set the value for a control on the panel.
8061
8162
Args:
8263
value_id: The id of the value
8364
value: The value
8465
"""
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:
85107
# TODO: AB#3095681 - Convert the value to an Any and pass it to _stub.SetValue
86108
pass

tests/unit/test_panel.py

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

33

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-
184
def test___connected_panel___set_value___gets_same_value() -> None:
195
panel = nipanel.Panel.streamlit_panel("path/to/script")
206
panel.connect()

0 commit comments

Comments
 (0)