Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/nipanel/_panel.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import sys
import uuid
from abc import ABC, abstractmethod
from types import TracebackType
from typing import Optional, Type, TYPE_CHECKING
Expand All @@ -24,10 +23,20 @@ class Panel(ABC):

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

def __init__(self, panel_uri: str) -> None:
def __init__(self, panel_uri: str, panel_id: str) -> None:
"""Initialize the panel."""
self._panel_uri = panel_uri
self._panel_id = str(uuid.uuid4())
self._panel_id = panel_id

@property
def panel_uri(self) -> str:
"""Read-only accessor for the panel URI."""
return self._panel_uri

@property
def panel_id(self) -> str:
"""Read-only accessor for the panel ID."""
return self._panel_id

def __enter__(self) -> Self:
"""Enter the runtime context related to this object."""
Expand Down
5 changes: 3 additions & 2 deletions src/nipanel/_streamlit_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ class StreamlitPanel(Panel):

__slots__ = ()

def __init__(self, streamlit_script_uri: str) -> None:
def __init__(self, streamlit_script_uri: str, panel_id: str) -> None:
"""Create a panel using a Streamlit script for the user interface.

Args:
streamlit_script_uri: The file path of the Streamlit script.
panel_id: A unique identifier for the panel.

Returns:
A new StreamlitPanel instance.
"""
super().__init__(streamlit_script_uri)
super().__init__(streamlit_script_uri, panel_id)

def _resolve_service_location(self) -> str:
# TODO: AB#3095680 - resolve to the Streamlit PythonPanelService
Expand Down
16 changes: 5 additions & 11 deletions tests/unit/test_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@


def test___streamlit_panel___has_panel_id_and_panel_uri() -> None:
panel = nipanel.StreamlitPanel("path/to/script")
assert panel._panel_id is not None
assert panel._panel_uri == "path/to/script"


def test___two_panels___have_different_panel_ids() -> None:
panel1 = nipanel.StreamlitPanel("path/to/script1")
panel2 = nipanel.StreamlitPanel("path/to/script2")
assert panel1._panel_id != panel2._panel_id
panel = nipanel.StreamlitPanel("path/to/script", "my_panel")
assert panel.panel_uri == "path/to/script"
assert panel.panel_id == "my_panel"


def test___connected_panel___set_value___gets_same_value() -> None:
panel = nipanel.StreamlitPanel("path/to/script")
panel = nipanel.StreamlitPanel("path/to/script", "my_panel")
panel.connect()

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


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

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

Expand Down