Skip to content

Commit

Permalink
Add option for bokeh resources
Browse files Browse the repository at this point in the history
  • Loading branch information
edsaac committed Dec 26, 2023
1 parent f0527ab commit 658f25c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 18 deletions.
4 changes: 4 additions & 0 deletions stpyvista/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [v 0.0.12] - 2023-12-26
- Add option to pass to `bokeh.resources` to load either `CDN` or `INLINE`. Defaults to `INLINE`.
- Update docstring for stpyvista

## [v 0.0.11] - 2023-11-20
- Remove unnecessary pyvista call for a jupyter backend. Ipython dependency can be totally drop now.

Expand Down
2 changes: 1 addition & 1 deletion stpyvista/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "stpyvista"
version = "0.0.11"
version = "0.0.12"
authors = [
{ name="Edwin Saavedra C.", email="[email protected]" },
]
Expand Down
49 changes: 33 additions & 16 deletions stpyvista/src/stpyvista/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import streamlit.components.v1 as components
import pyvista as pv
import panel as pn
from bokeh.resources import CDN

# pv.set_jupyter_backend("none")
from bokeh.resources import CDN, INLINE
BOKEH_RESOURCES = {"CDN": CDN, "INLINE": INLINE}

pn.extension("vtk", sizing_mode="stretch_width")

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


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


def stpyvista(
plotter: pv.Plotter,
use_container_width: bool = True,
horizontal_align: HA_MODES = "center",
panel_kwargs=None,
horizontal_align: Literal["center", "left", "right"] = "center",
panel_kwargs: dict|None = None,
bokeh_resources: Literal["CDN", "INLINE"] = "INLINE",
key: Optional[str] = None,
) -> None:
"""
Renders an interactive pyvisya Plotter in streamlit.
Parameters
----------
input: Union[pv.Plotter, HTML_stpyvista]
Plotter to render
input: pv.Plotter
Pyvista plotter object to render.
use_container_width : bool = True
If True, set the dataframe width to the width of the parent container. \
This takes precedence over the `horizontal_align` argument. \
Defaults to True
Defaults to `True`.
horizontal_align: str = "center"
Either "center", "left" or "right". Defaults to "center".
horizontal_align : Literal["center", "left", "right"] = "center"
Horizontal alignment of the stpyvista component. This parameter is ignored if
`use_container_width = True`. Defaluts to `"center"`.
panel_kwargs: dict | None
panel_kwargs : dict | None = None
Optional keyword parameters to pass to pn.panel() Check:
https://panel.holoviz.org/api/panel.pane.vtk.html for details. Here is
a useful one:
orientation_widget: bool
orientation_widget : bool
Show the xyz axis indicator
bokeh_resources: Literal["CDN", "INLINE"] = "Inline"
Source of the BokehJS configuration. Check:
https://docs.bokeh.org/en/latest/docs/reference/resources.html for details. \
Defaults to "INLINE"
key: str|None
An optional key that uniquely identifies this component. If this is
None, and the component's arguments are changed, the component will
Expand All @@ -67,6 +73,7 @@ def stpyvista(
Returns
-------
None
"""

if isinstance(plotter, pv.Plotter):
Expand All @@ -81,10 +88,18 @@ def stpyvista(
geo_pan_pv = pn.panel(
plotter.ren_win, height=height, width=width, **panel_kwargs
)


# Check bokeh_resources
if not bokeh_resources in ("CDN", "INLINE"):
raise stpyvistaTypeError(
f'"{bokeh_resources}" is not a valid bokeh resource. '
'Valid options are "CDN" or "INLINE".'
)


# Create HTML file
model_bytes = BytesIO()
geo_pan_pv.save(model_bytes, resources=CDN)
geo_pan_pv.save(model_bytes, resources=BOKEH_RESOURCES[bokeh_resources])
panel_html = model_bytes.getvalue().decode("utf-8")
model_bytes.close()

Expand All @@ -101,7 +116,9 @@ def stpyvista(
return component_value

else:
raise (stpyvistaTypeError)
raise stpyvistaTypeError(
f'{plotter} is not a `pv.Plotter` instance. '
)


def main():
Expand Down
2 changes: 1 addition & 1 deletion stpyvista/test/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@

for col in cols:
with col:
stpyvista(plotter, use_container_width=chk)
stpyvista(plotter, use_container_width=chk, bokeh_resources="CDN")

0 comments on commit 658f25c

Please sign in to comment.