Skip to content

Commit 163735b

Browse files
Mike ProsserMike Prosser
authored andcommitted
_sync_session_state()
1 parent 585948c commit 163735b

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

examples/simple_graph/simple_graph_panel.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
amplitude_enum = AmplitudeEnum[amplitude_tuple[0]]
2424
panel.set_value("amplitude_enum", amplitude_enum)
2525
with col2:
26-
base_frequency = st.number_input("Base Frequency", value=1.0, step=0.1)
27-
panel.set_value("base_frequency", base_frequency)
26+
base_frequency = st.number_input("Base Frequency", value=1.0, step=0.1, key="base_frequency")
2827

2928
with col3:
3029
frequency = panel.get_value("frequency", 0.0)

src/nipanel/_convert.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,12 @@ def from_any(protobuf_any: any_pb2.Any) -> object:
129129

130130
converter = _CONVERTER_FOR_GRPC_TYPE[underlying_typename]
131131
return converter.to_python(protobuf_any)
132+
133+
134+
def is_supported_type(value: object) -> bool:
135+
"""Check if a given Python value can be converted to protobuf Any."""
136+
try:
137+
_get_best_matching_type(value)
138+
return True
139+
except TypeError:
140+
return False

src/nipanel/_streamlit_panel_initializer.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import streamlit as st
55

6+
from nipanel._convert import is_supported_type
67
from nipanel._streamlit_panel import StreamlitPanel
78
from nipanel._streamlit_panel_value_accessor import StreamlitPanelValueAccessor
89
from nipanel.streamlit_refresh import initialize_refresh_component
@@ -61,6 +62,7 @@ def get_panel_accessor() -> StreamlitPanelValueAccessor:
6162
st.session_state[PANEL_ACCESSOR_KEY] = _initialize_panel_from_base_path()
6263

6364
panel = cast(StreamlitPanelValueAccessor, st.session_state[PANEL_ACCESSOR_KEY])
65+
_sync_session_state(panel)
6466
refresh_component = initialize_refresh_component(panel.panel_id)
6567
refresh_component()
6668
return panel
@@ -75,3 +77,11 @@ def _initialize_panel_from_base_path() -> StreamlitPanelValueAccessor:
7577
if not panel_id:
7678
raise ValueError(f"Panel ID is empty in baseUrlPath: '{base_url_path}'")
7779
return StreamlitPanelValueAccessor(panel_id)
80+
81+
82+
def _sync_session_state(panel):
83+
"""Automatically read keyed control values from the session state."""
84+
for key in st.session_state.keys():
85+
value = st.session_state[key]
86+
if is_supported_type(value):
87+
panel.set_value(str(key), value)

0 commit comments

Comments
 (0)