Skip to content
1 change: 0 additions & 1 deletion src/nipanel/_streamlit_constants.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
STREAMLIT_PYTHON_PANEL_SERVICE = "ni.pythonpanel.v1.PythonPanelService"
STREAMLIT_REFRESH_COMPONENT_URL = "http://localhost:42001/panel-service/refresh"
37 changes: 31 additions & 6 deletions src/nipanel/streamlit_refresh/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,42 @@
"""Initializes a refresh component for Streamlit."""

from typing import Any
from __future__ import annotations

import streamlit.components.v1 as components
import threading

from nipanel._streamlit_constants import STREAMLIT_REFRESH_COMPONENT_URL
from ni_measurement_plugin_sdk_service.discovery import DiscoveryClient
from ni_measurement_plugin_sdk_service.grpc.channelpool import GrpcChannelPool
from streamlit.components.v1 import declare_component
from streamlit.components.v1.custom_component import CustomComponent


def initialize_refresh_component(panel_id: str) -> Any:
_grpc_client_lock = threading.RLock()
_panel_service_proxy_location: str | None = None


def initialize_refresh_component(panel_id: str) -> CustomComponent:
"""Initialize a refresh component to the Streamlit app."""
_refresh_component_func = components.declare_component(
proxy_base_address = _get_or_resolve_proxy()
component_url = f"http://{proxy_base_address}/panel-service/refresh/{panel_id}"
_refresh_component_func = declare_component(
"panelRefreshComponent",
url=f"{STREAMLIT_REFRESH_COMPONENT_URL}/{panel_id}",
url=component_url,
)

return _refresh_component_func


def _get_or_resolve_proxy() -> str:
global _grpc_client_lock
with _grpc_client_lock:
global _panel_service_proxy_location
if _panel_service_proxy_location is None:
with GrpcChannelPool() as grpc_channel_pool:
discovery_client = DiscoveryClient(grpc_channel_pool=grpc_channel_pool)
service_location = discovery_client.resolve_service(
provided_interface="ni.http1.proxy",
service_class="",
)
_panel_service_proxy_location = service_location.insecure_address

return _panel_service_proxy_location