Skip to content

Commit 658f25c

Browse files
committed
Add option for bokeh resources
1 parent f0527ab commit 658f25c

File tree

4 files changed

+39
-18
lines changed

4 files changed

+39
-18
lines changed

stpyvista/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## [v 0.0.12] - 2023-12-26
4+
- Add option to pass to `bokeh.resources` to load either `CDN` or `INLINE`. Defaults to `INLINE`.
5+
- Update docstring for stpyvista
6+
37
## [v 0.0.11] - 2023-11-20
48
- Remove unnecessary pyvista call for a jupyter backend. Ipython dependency can be totally drop now.
59

stpyvista/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "stpyvista"
7-
version = "0.0.11"
7+
version = "0.0.12"
88
authors = [
99
{ name="Edwin Saavedra C.", email="esaavedrac@u.northwestern.edu" },
1010
]

stpyvista/src/stpyvista/__init__.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
import streamlit.components.v1 as components
77
import pyvista as pv
88
import panel as pn
9-
from bokeh.resources import CDN
109

11-
# pv.set_jupyter_backend("none")
10+
from bokeh.resources import CDN, INLINE
11+
BOKEH_RESOURCES = {"CDN": CDN, "INLINE": INLINE}
12+
1213
pn.extension("vtk", sizing_mode="stretch_width")
1314

1415
# Tell streamlit that there is a component called stpyvista,
@@ -25,40 +26,45 @@ class stpyvistaTypeError(TypeError):
2526

2627

2728
# Create the python function that will be called from the front end
28-
HA_MODES = Literal["left", "center", "right"]
29-
3029

3130
def stpyvista(
3231
plotter: pv.Plotter,
3332
use_container_width: bool = True,
34-
horizontal_align: HA_MODES = "center",
35-
panel_kwargs=None,
33+
horizontal_align: Literal["center", "left", "right"] = "center",
34+
panel_kwargs: dict|None = None,
35+
bokeh_resources: Literal["CDN", "INLINE"] = "INLINE",
3636
key: Optional[str] = None,
3737
) -> None:
3838
"""
3939
Renders an interactive pyvisya Plotter in streamlit.
4040
4141
Parameters
4242
----------
43-
input: Union[pv.Plotter, HTML_stpyvista]
44-
Plotter to render
43+
input: pv.Plotter
44+
Pyvista plotter object to render.
4545
4646
use_container_width : bool = True
4747
If True, set the dataframe width to the width of the parent container. \
4848
This takes precedence over the `horizontal_align` argument. \
49-
Defaults to True
49+
Defaults to `True`.
5050
51-
horizontal_align: str = "center"
52-
Either "center", "left" or "right". Defaults to "center".
51+
horizontal_align : Literal["center", "left", "right"] = "center"
52+
Horizontal alignment of the stpyvista component. This parameter is ignored if
53+
`use_container_width = True`. Defaluts to `"center"`.
5354
54-
panel_kwargs: dict | None
55+
panel_kwargs : dict | None = None
5556
Optional keyword parameters to pass to pn.panel() Check:
5657
https://panel.holoviz.org/api/panel.pane.vtk.html for details. Here is
5758
a useful one:
5859
59-
orientation_widget: bool
60+
orientation_widget : bool
6061
Show the xyz axis indicator
6162
63+
bokeh_resources: Literal["CDN", "INLINE"] = "Inline"
64+
Source of the BokehJS configuration. Check:
65+
https://docs.bokeh.org/en/latest/docs/reference/resources.html for details. \
66+
Defaults to "INLINE"
67+
6268
key: str|None
6369
An optional key that uniquely identifies this component. If this is
6470
None, and the component's arguments are changed, the component will
@@ -67,6 +73,7 @@ def stpyvista(
6773
Returns
6874
-------
6975
None
76+
7077
"""
7178

7279
if isinstance(plotter, pv.Plotter):
@@ -81,10 +88,18 @@ def stpyvista(
8188
geo_pan_pv = pn.panel(
8289
plotter.ren_win, height=height, width=width, **panel_kwargs
8390
)
84-
91+
92+
# Check bokeh_resources
93+
if not bokeh_resources in ("CDN", "INLINE"):
94+
raise stpyvistaTypeError(
95+
f'"{bokeh_resources}" is not a valid bokeh resource. '
96+
'Valid options are "CDN" or "INLINE".'
97+
)
98+
99+
85100
# Create HTML file
86101
model_bytes = BytesIO()
87-
geo_pan_pv.save(model_bytes, resources=CDN)
102+
geo_pan_pv.save(model_bytes, resources=BOKEH_RESOURCES[bokeh_resources])
88103
panel_html = model_bytes.getvalue().decode("utf-8")
89104
model_bytes.close()
90105

@@ -101,7 +116,9 @@ def stpyvista(
101116
return component_value
102117

103118
else:
104-
raise (stpyvistaTypeError)
119+
raise stpyvistaTypeError(
120+
f'{plotter} is not a `pv.Plotter` instance. '
121+
)
105122

106123

107124
def main():

stpyvista/test/cube.py

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

4545
for col in cols:
4646
with col:
47-
stpyvista(plotter, use_container_width=chk)
47+
stpyvista(plotter, use_container_width=chk, bokeh_resources="CDN")

0 commit comments

Comments
 (0)