Skip to content

Commit 0d3aa3c

Browse files
Mike ProsserMike Prosser
authored andcommitted
rename initialize_panel to get_panel_accessor and throw exceptions when called improperly
1 parent 3d60ea7 commit 0d3aa3c

File tree

5 files changed

+20
-11
lines changed

5 files changed

+20
-11
lines changed

examples/all_types/all_types_panel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import nipanel
77

8-
panel = nipanel.initialize_panel()
8+
panel = nipanel.get_panel_accessor()
99

1010
st.set_page_config(page_title="All Types Example", page_icon="📊", layout="wide")
1111
st.title("All Types Example")

examples/sample/sample_panel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import nipanel
66

7-
panel = nipanel.initialize_panel()
7+
panel = nipanel.get_panel_accessor()
88

99

1010
st.set_page_config(page_title="Sample Panel Example", page_icon="📊", layout="wide")

examples/simple_graph/simple_graph_panel.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import streamlit as st
44
from streamlit_echarts import st_echarts # type: ignore
55

6-
from nipanel import initialize_panel
6+
import nipanel
77

88

9-
panel = initialize_panel()
9+
panel = nipanel.get_panel_accessor()
1010

1111
st.set_page_config(page_title="Simple Graph Example", page_icon="📈", layout="wide")
1212
st.title("Simple Graph Example")

src/nipanel/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
from nipanel._panel import Panel
66
from nipanel._streamlit_panel import StreamlitPanel
7-
from nipanel._streamlit_panel_initializer import create_panel, initialize_panel
7+
from nipanel._streamlit_panel_initializer import create_panel, get_panel_accessor
88
from nipanel._streamlit_panel_value_accessor import StreamlitPanelValueAccessor
99

1010
__all__ = [
1111
"create_panel",
12-
"initialize_panel",
12+
"get_panel_accessor",
1313
"Panel",
1414
"StreamlitPanel",
1515
"StreamlitPanelValueAccessor",

src/nipanel/_streamlit_panel_initializer.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,19 @@ def create_panel(streamlit_script_path: Path) -> StreamlitPanel:
1919
"some_example".
2020
2121
Use this function when you want to create a new panel instance to use in a Streamlit
22-
application.
22+
application. Do not call this function from within a Streamlit script.
2323
2424
Args:
2525
streamlit_script_path: The file path of the Streamlit script to be used for the panel.
2626
2727
Returns:
2828
A StreamlitPanel instance initialized with the given panel ID.
2929
"""
30+
if st.get_option("server.baseUrlPath") != "":
31+
raise RuntimeError(
32+
"nipanel.create_panel() should not be called from a Streamlit script. Call nipanel.get_panel_accessor() instead."
33+
)
34+
3035
path_str = str(streamlit_script_path)
3136
if not path_str.endswith(".py"):
3237
raise ValueError(
@@ -37,17 +42,21 @@ def create_panel(streamlit_script_path: Path) -> StreamlitPanel:
3742
return StreamlitPanel(panel_id, path_str)
3843

3944

40-
def initialize_panel() -> StreamlitPanelValueAccessor:
45+
def get_panel_accessor() -> StreamlitPanelValueAccessor:
4146
"""Initialize and return the Streamlit panel value accessor.
4247
4348
This function retrieves the Streamlit panel value accessor for the current Streamlit script.
44-
It is typically used within a Streamlit script to access and manipulate panel values.
45-
The accessor will be cached in the Streamlit session state to ensure that it is reused across
46-
reruns of the script.
49+
This function should only be called from within a Streamlit script. The accessor will be cached
50+
in the Streamlit session state to ensure that it is reused across reruns of the script.
4751
4852
Returns:
4953
A StreamlitPanelValueAccessor instance for the current panel.
5054
"""
55+
if st.get_option("server.baseUrlPath") == "":
56+
raise RuntimeError(
57+
"nipanel.get_panel_accessor() should only be called from a Streamlit script. Call nipanel.create_panel() instead."
58+
)
59+
5160
if PANEL_ACCESSOR_KEY not in st.session_state:
5261
st.session_state[PANEL_ACCESSOR_KEY] = _initialize_panel_from_base_path()
5362

0 commit comments

Comments
 (0)