Skip to content

Commit c9e320e

Browse files
Mike ProsserMike Prosser
authored andcommitted
remove is_open and is_in_memory from public api, and make panel client thinner.
1 parent 6403ed6 commit c9e320e

File tree

6 files changed

+53
-105
lines changed

6 files changed

+53
-105
lines changed

protos/ni/pythonpanel/v1/python_panel_service.proto

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ service PythonPanelService {
5050
message EnumeratePanelsRequest {
5151
}
5252

53-
message PanelInformation
54-
{
53+
message PanelInformation {
5554
// Unique ID of the panel
5655
string panel_id = 1;
5756

src/nipanel/_panel.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,3 @@ def close_panel(self, reset: bool) -> None:
7676
reset: Whether to reset all storage associated with the panel.
7777
"""
7878
self._panel_client.close_panel(self._panel_id, reset=reset)
79-
80-
def is_open(self) -> bool:
81-
"""Check if the panel is open."""
82-
return self._panel_client.is_panel_open(self._panel_id)
83-
84-
def is_in_memory(self) -> bool:
85-
"""Check if the panel is in memory."""
86-
return self._panel_client.is_panel_in_memory(self._panel_id)

src/nipanel/_panel_client.py

Lines changed: 8 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
OpenPanelRequest,
1212
ClosePanelRequest,
1313
EnumeratePanelsRequest,
14-
PanelInformation,
1514
GetValueRequest,
1615
SetValueRequest,
1716
ClearValueRequest,
@@ -81,59 +80,18 @@ def close_panel(self, panel_id: str, reset: bool) -> None:
8180
close_panel_request = ClosePanelRequest(panel_id=panel_id, reset=reset)
8281
self._invoke_with_retry(self._get_stub().ClosePanel, close_panel_request)
8382

84-
def _enumerate_panels(self) -> list[PanelInformation]:
83+
def enumerate_panels(self) -> dict[str, tuple[bool, list[str]]]:
84+
"""Enumerate all open panels.
85+
86+
Returns:
87+
A dictionary mapping panel IDs to a tuple containing a boolean indicating if the panel
88+
is open and a list of value IDs associated with the panel.
89+
"""
8590
enumerate_panels_request = EnumeratePanelsRequest()
8691
response = self._invoke_with_retry(
8792
self._get_stub().EnumeratePanels, enumerate_panels_request
8893
)
89-
return list(response.panels)
90-
91-
def get_panel_ids(self) -> list[str]:
92-
"""Get the IDs of all panels in memory.
93-
94-
Returns:
95-
A list of panel IDs.
96-
"""
97-
panels = self._enumerate_panels()
98-
return [panel.panel_id for panel in panels]
99-
100-
def is_panel_in_memory(self, panel_id: str) -> bool:
101-
"""Check if the panel is in memory.
102-
103-
Args:
104-
panel_id: The ID of the panel.
105-
106-
Returns:
107-
True if the panel is in memory, False otherwise.
108-
"""
109-
panels = self._enumerate_panels()
110-
return any(panel.panel_id == panel_id for panel in panels)
111-
112-
def is_panel_open(self, panel_id: str) -> bool:
113-
"""Check if the panel is open.
114-
115-
Args:
116-
panel_id: The ID of the panel.
117-
118-
Returns:
119-
True if the panel is open, False otherwise.
120-
"""
121-
panels = self._enumerate_panels()
122-
panel = next((panel for panel in panels if panel.panel_id == panel_id), None)
123-
return panel.is_open if panel else False
124-
125-
def get_value_ids(self, panel_id: str) -> list[str]:
126-
"""Get the IDs of all the values that have been set for the panel.
127-
128-
Args:
129-
panel_id: The ID of the panel.
130-
131-
Returns:
132-
A list of value IDs for the panel.
133-
"""
134-
panels = self._enumerate_panels()
135-
panel = next((panel for panel in panels if panel.panel_id == panel_id), None)
136-
return list(panel.value_ids) if panel else []
94+
return {panel.panel_id: (panel.is_open, list(panel.value_ids)) for panel in response.panels}
13795

13896
def set_value(self, panel_id: str, value_id: str, value: object) -> None:
13997
"""Set the value for the control with value_id.

src/nipanel/_panel_value_accessor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,4 @@ def get_value_ids(self) -> list[str]:
8484
Returns:
8585
A list of value IDs for the panel.
8686
"""
87-
return self._panel_client.get_value_ids(self._panel_id)
87+
return self._panel_client.enumerate_panels()[self._panel_id][1]

tests/unit/test_panel_client.py

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
from nipanel._panel_client import PanelClient
55

66

7-
def test___panel_ids_is_empty(fake_panel_channel: grpc.Channel) -> None:
7+
def test___enumerate_panels_is_empty(fake_panel_channel: grpc.Channel) -> None:
88
client = create_panel_client(fake_panel_channel)
99

10-
assert client.get_panel_ids() == []
10+
assert client.enumerate_panels() == {}
1111

1212

1313
def test___open_panels___both_panels_open_and_in_memory(fake_panel_channel: grpc.Channel) -> None:
@@ -16,11 +16,10 @@ def test___open_panels___both_panels_open_and_in_memory(fake_panel_channel: grpc
1616
client.open_panel("panel1", "uri1")
1717
client.open_panel("panel2", "uri2")
1818

19-
assert client.get_panel_ids() == ["panel1", "panel2"]
20-
assert client.is_panel_in_memory("panel1")
21-
assert client.is_panel_in_memory("panel2")
22-
assert client.is_panel_open("panel1")
23-
assert client.is_panel_open("panel2")
19+
assert client.enumerate_panels() == {
20+
"panel1": (True, []),
21+
"panel2": (True, []),
22+
}
2423

2524

2625
def test___open_panels___close_panel_1_with_reset___panel_1_not_in_memory(
@@ -32,10 +31,9 @@ def test___open_panels___close_panel_1_with_reset___panel_1_not_in_memory(
3231

3332
client.close_panel("panel1", reset=True)
3433

35-
assert client.get_panel_ids() == ["panel2"]
36-
assert not client.is_panel_in_memory("panel1")
37-
assert client.is_panel_in_memory("panel2")
38-
assert client.is_panel_open("panel2")
34+
assert client.enumerate_panels() == {
35+
"panel2": (True, []),
36+
}
3937

4038

4139
def test___open_panels___close_panel_1_without_reset___both_panels_in_memory(
@@ -47,11 +45,10 @@ def test___open_panels___close_panel_1_without_reset___both_panels_in_memory(
4745

4846
client.close_panel("panel1", reset=False)
4947

50-
assert client.get_panel_ids() == ["panel1", "panel2"]
51-
assert client.is_panel_in_memory("panel1")
52-
assert client.is_panel_in_memory("panel2")
53-
assert not client.is_panel_open("panel1")
54-
assert client.is_panel_open("panel2")
48+
assert client.enumerate_panels() == {
49+
"panel1": (False, []),
50+
"panel2": (True, []),
51+
}
5552

5653

5754
def test___get_unset_value_raises_exception(fake_panel_channel: grpc.Channel) -> None:
@@ -61,28 +58,28 @@ def test___get_unset_value_raises_exception(fake_panel_channel: grpc.Channel) ->
6158
client.get_value("panel1", "unset_id")
6259

6360

64-
def test___set_value___get_value_ids_has_value(
61+
def test___set_value___enumerate_panels_shows_value(
6562
fake_panel_channel: grpc.Channel,
6663
) -> None:
6764
client = create_panel_client(fake_panel_channel)
6865

6966
client.set_value("panel1", "val1", "value1")
7067

71-
assert client.get_value_ids("panel1") == ["val1"]
68+
assert client.enumerate_panels() == {"panel1": (False, ["val1"])}
7269

7370

74-
def test___set_value___clear_value___get_value_ids_empty(
71+
def test___set_value___clear_value___enumerate_panels_shows_no_value(
7572
fake_panel_channel: grpc.Channel,
7673
) -> None:
7774
client = create_panel_client(fake_panel_channel)
7875
client.set_value("panel1", "val1", "value1")
7976

8077
client.clear_value("panel1", "val1")
8178

82-
assert client.get_value_ids("panel1") == []
79+
assert client.enumerate_panels() == {"panel1": (False, [])}
8380

8481

85-
def test___set_values___clear_value_2___get_value_ids_has_value_1(
82+
def test___set_values___clear_value_2___enumerate_panels_has_value_1(
8683
fake_panel_channel: grpc.Channel,
8784
) -> None:
8885
client = create_panel_client(fake_panel_channel)
@@ -91,7 +88,7 @@ def test___set_values___clear_value_2___get_value_ids_has_value_1(
9188

9289
client.clear_value("panel1", "val2")
9390

94-
assert client.get_value_ids("panel1") == ["val1"]
91+
assert client.enumerate_panels() == {"panel1": (False, ["val1"])}
9592

9693

9794
def test___set_value___gets_value(fake_panel_channel: grpc.Channel) -> None:

tests/unit/test_streamlit_panel.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,6 @@ def test___different_panels___have_different_panel_ids_and_uris() -> None:
2323
assert panel1._panel_client != panel2._panel_client
2424

2525

26-
def test___open_panel___panel_is_open_and_in_memory(
27-
fake_panel_channel: grpc.Channel,
28-
) -> None:
29-
panel = StreamlitPanel("my_panel", "path/to/script", grpc_channel=fake_panel_channel)
30-
assert not panel.is_open()
31-
assert not panel.is_in_memory()
32-
33-
panel.open_panel()
34-
35-
assert panel.is_open()
36-
assert panel.is_in_memory()
37-
38-
3926
def test___opened_panel___set_value___gets_same_value(
4027
fake_panel_channel: grpc.Channel,
4128
) -> None:
@@ -85,12 +72,9 @@ def test___opened_panel_with_value___close_without_reset___gets_value(
8572
value_id = "test_id"
8673
string_value = "test_value"
8774
panel.set_value(value_id, string_value)
88-
assert panel.is_open()
8975

9076
panel.close_panel(reset=False)
9177

92-
assert panel.is_in_memory()
93-
assert not panel.is_open()
9478
assert panel.get_value(value_id) == string_value
9579

9680

@@ -102,11 +86,9 @@ def test___opened_panel_with_value___close_with_reset___get_throws(
10286
value_id = "test_id"
10387
string_value = "test_value"
10488
panel.set_value(value_id, string_value)
105-
assert panel.is_open()
10689

10790
panel.close_panel(reset=True)
10891

109-
assert not panel.is_in_memory()
11092
with pytest.raises(grpc.RpcError):
11193
panel.get_value(value_id)
11294

@@ -227,18 +209,30 @@ def test___unsupported_type___set_value___raises(
227209
panel.set_value(value_id, value_payload)
228210

229211

212+
def test___open_panel___panel_is_open_and_in_memory(
213+
fake_panel_channel: grpc.Channel,
214+
) -> None:
215+
panel = StreamlitPanel("my_panel", "path/to/script", grpc_channel=fake_panel_channel)
216+
assert not is_panel_in_memory(panel)
217+
218+
panel.open_panel()
219+
220+
assert is_panel_in_memory(panel)
221+
assert is_panel_open(panel)
222+
223+
230224
def test___with_panel___opens_and_closes_panel(
231225
fake_panel_channel: grpc.Channel,
232226
) -> None:
233227
"""Test that using the panel in a with statement opens and closes it."""
234228
panel = StreamlitPanel("my_panel", "path/to/script", grpc_channel=fake_panel_channel)
235229

236230
with panel:
237-
assert panel.is_open()
238-
assert panel.is_in_memory()
231+
assert is_panel_in_memory(panel)
232+
assert is_panel_open(panel)
239233

240-
assert not panel.is_open()
241-
assert panel.is_in_memory()
234+
assert is_panel_in_memory(panel)
235+
assert not is_panel_open(panel)
242236

243237

244238
def test___with_panel___set_value___gets_same_value(
@@ -251,3 +245,11 @@ def test___with_panel___set_value___gets_same_value(
251245
panel.set_value(value_id, string_value)
252246

253247
assert panel.get_value(value_id) == string_value
248+
249+
250+
def is_panel_in_memory(panel: StreamlitPanel) -> bool:
251+
return panel.panel_id in panel._panel_client.enumerate_panels().keys()
252+
253+
254+
def is_panel_open(panel: StreamlitPanel) -> bool:
255+
return panel._panel_client.enumerate_panels()[panel.panel_id][0]

0 commit comments

Comments
 (0)