11from multiprocessing import Process , Queue
22from warnings import warn
3-
3+ from pathlib import Path
44import streamlit .components .v1 as components
55
66from pyvista .plotting import Plotter
@@ -10,7 +10,47 @@ def _export_html(queue: Queue, plotter: Plotter):
1010 queue .put (plotter .export_html (filename = None ))
1111
1212
13- def stpyvista (plotter : Plotter , ** kwargs ) -> None :
13+ def _as_html (plotter : Plotter , ** kwargs ) -> None :
14+ """Plan to remove this function in the future"""
15+ if not isinstance (plotter , Plotter ):
16+ raise TypeError (f"{ plotter } is not a `pyvista.Plotter` instance." )
17+
18+ if "panel_kwargs" in kwargs :
19+ warn (
20+ "panel_kwargs is not supported by the trame backend.\n "
21+ "They will be ignored"
22+ )
23+
24+ if "horizontal_align" in kwargs :
25+ warn (
26+ "horizontal_align is not supported by the trame backend.\n "
27+ "It will be ignored"
28+ )
29+
30+ queue = Queue (maxsize = 1 )
31+ process = Process (target = _export_html , args = (queue , plotter ))
32+
33+ process .start ()
34+ html_plotter = queue .get ().read ()
35+ process .join ()
36+
37+ if kwargs .get ("use_container_width" , True ):
38+ width = None
39+ else :
40+ width = plotter .window_size [0 ]
41+
42+ components .html (html_plotter , height = plotter .window_size [1 ], width = width )
43+
44+
45+ ## Using component declaration
46+
47+ frontend_dir = (Path (__file__ ).parent / "trame_based" ).absolute ()
48+ _component_func = components .declare_component (
49+ "stpyvista_trame" , path = str (frontend_dir )
50+ )
51+
52+
53+ def stpyvista (plotter : Plotter , use_container_width = True , key = None , ** kwargs ) -> None :
1454 """
1555 Renders an interactive Pyvista Plotter in streamlit using the
1656 trame backend.
@@ -19,7 +59,17 @@ def stpyvista(plotter: Plotter, **kwargs) -> None:
1959 ----------
2060 plotter: pv.Plotter
2161 Pyvista plotter object to render.
62+ use_container_width: bool = True
63+ If True, set the 3D view width to the width of the parent container. \
64+ If False, the width is taken from the plotter window size.
65+ key: Optional[str] = None
66+ An optional key that uniquely identifies this component. If this is
67+ None, and the component's arguments are changed, the component will
68+ be re-mounted in the Streamlit frontend and lose its current state.
69+
2270 """
71+
72+ ## Checks
2373 if not isinstance (plotter , Plotter ):
2474 raise TypeError (f"{ plotter } is not a `pyvista.Plotter` instance." )
2575
@@ -35,16 +85,22 @@ def stpyvista(plotter: Plotter, **kwargs) -> None:
3585 "It will be ignored"
3686 )
3787
88+ ## Get HTML of plotter
3889 queue = Queue (maxsize = 1 )
3990 process = Process (target = _export_html , args = (queue , plotter ))
4091
4192 process .start ()
4293 html_plotter = queue .get ().read ()
4394 process .join ()
4495
45- if kwargs .get ("use_container_width" , True ):
46- width = None
47- else :
48- width = plotter .window_size [0 ]
96+ ## Set dimensions
97+ width = None if use_container_width else plotter .window_size [0 ]
98+ height = plotter .window_size [1 ]
4999
50- components .html (html_plotter , height = plotter .window_size [1 ], width = width )
100+ _component_func (
101+ trame_html = html_plotter ,
102+ height = height ,
103+ width = width ,
104+ use_container_width = use_container_width ,
105+ key = key ,
106+ )
0 commit comments