|
1 | 1 | """Initializes a refresh component for Streamlit.""" |
2 | 2 |
|
3 | | -from typing import Any |
| 3 | +from __future__ import annotations |
4 | 4 |
|
5 | | -import streamlit.components.v1 as components |
| 5 | +import threading |
6 | 6 |
|
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 |
8 | 11 |
|
9 | 12 |
|
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: |
11 | 18 | """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( |
13 | 22 | "panelRefreshComponent", |
14 | | - url=f"{STREAMLIT_REFRESH_COMPONENT_URL}/{panel_id}", |
| 23 | + url=component_url, |
15 | 24 | ) |
16 | 25 |
|
17 | 26 | 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