66import streamlit .components .v1 as components
77import pyvista as pv
88import 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+
1213pn .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
3130def 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
107124def main ():
0 commit comments