Skip to content

Commit b1c1605

Browse files
authored
Support non-default ports for the Discovery Service (#116)
* Resolve the discovery service and use it as the base URL for the refresh component * Use the resolved URL when initializing the refresh component
1 parent 49c97c1 commit b1c1605

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
STREAMLIT_PYTHON_PANEL_SERVICE = "ni.pythonpanel.v1.PythonPanelService"
2-
STREAMLIT_REFRESH_COMPONENT_URL = "http://localhost:42001/panel-service/refresh"
Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,41 @@
11
"""Initializes a refresh component for Streamlit."""
22

3-
from typing import Any
3+
from __future__ import annotations
44

5-
import streamlit.components.v1 as components
5+
import threading
66

7-
from nipanel._streamlit_constants import STREAMLIT_REFRESH_COMPONENT_URL
7+
from ni_measurement_plugin_sdk_service.discovery import DiscoveryClient
8+
from ni_measurement_plugin_sdk_service.grpc.channelpool import GrpcChannelPool
9+
from streamlit.components.v1 import declare_component
10+
from streamlit.components.v1.custom_component import CustomComponent
811

912

10-
def initialize_refresh_component(panel_id: str) -> Any:
13+
_grpc_client_lock = threading.RLock()
14+
_panel_service_proxy_location: str | None = None
15+
16+
17+
def initialize_refresh_component(panel_id: str) -> CustomComponent:
1118
"""Initialize a refresh component to the Streamlit app."""
12-
_refresh_component_func = components.declare_component(
19+
proxy_base_address = _get_or_resolve_proxy()
20+
component_url = f"http://{proxy_base_address}/panel-service/refresh/{panel_id}"
21+
_refresh_component_func = declare_component(
1322
"panelRefreshComponent",
14-
url=f"{STREAMLIT_REFRESH_COMPONENT_URL}/{panel_id}",
23+
url=component_url,
1524
)
1625

1726
return _refresh_component_func
27+
28+
29+
def _get_or_resolve_proxy() -> str:
30+
with _grpc_client_lock:
31+
global _panel_service_proxy_location
32+
if _panel_service_proxy_location is None:
33+
with GrpcChannelPool() as grpc_channel_pool:
34+
discovery_client = DiscoveryClient(grpc_channel_pool=grpc_channel_pool)
35+
service_location = discovery_client.resolve_service(
36+
provided_interface="ni.http1.proxy",
37+
service_class="",
38+
)
39+
_panel_service_proxy_location = service_location.insecure_address
40+
41+
return _panel_service_proxy_location

0 commit comments

Comments
 (0)