Skip to content

Commit

Permalink
Add an option to control when the serialization to vtkjs happened
Browse files Browse the repository at this point in the history
  • Loading branch information
ARTUSI committed Sep 30, 2019
1 parent 08e5e6d commit 536327f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
4 changes: 2 additions & 2 deletions examples/reference/panes/VTK.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
" <br>**Warning**: These keybindings may not work as expected in a notebook context, if they interact with already bound keys\n",
"* **``orientation_widget``** (bool): A boolean to activate/deactivate the orientation widget in the 3D pane. This widget is clickable and allows to rotate the scene in one of the orthographic projection\n",
"* **``object``** (str or object): Can be a string pointing to a local or remote file with a `.vtkjs` extension, or a `vtkRenderWindow` object \n",
"\n",
"* **``serialize_policy``** (str): String selected between `'instantiation'` and `'display'`. It defines when the serialization of the VTK panel occurs. If `display` (default) is selected, the object is serialized only when the panel is displayed to the screen else if `instantiation` is activated, the serialization happened when the panel is created. This parameter is constant, once set it can't be modified\n",
"___"
]
},
Expand All @@ -49,7 +49,7 @@
"outputs": [],
"source": [
"dragon = pn.pane.VTK('https://raw.githubusercontent.com/Kitware/vtk-js/master/Data/StanfordDragon.vtkjs',\n",
" sizing_mode='stretch_width', height=400, enable_keybindings=True, orientation_widget=True)\n",
" sizing_mode='stretch_width', height=400, enable_keybindings=True, orientation_widget=True, serialize_policy='instantiation')\n",
"dragon"
]
},
Expand Down
58 changes: 35 additions & 23 deletions panel/pane/vtk/vtk.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,21 @@ class VTK(PaneBase):
VTK panes allow rendering VTK objects.
"""

serialize_policy = param.ObjectSelector(default='display',
objects=['instantiation', 'display'],
constant=True,
doc="""
Define if the object serialization occurs
at panel instanciation or when the panel is displayed.
""")

camera = param.Dict(doc="""State of the rendered VTK camera.""")

enable_keybindings = param.Boolean(default=False, doc="""
Activate/Deactivate keys binding.
Warning: These keys bind may not work as expected in a notebook
context if they interact with already binded keys
context if they interact with already binded keys.
""")

orientation_widget = param.Boolean(default=False, doc="""
Expand All @@ -174,6 +182,9 @@ class VTK(PaneBase):
def __init__(self, obj=None, **params):
super(VTK, self).__init__(obj, **params)
self._legend = None
self._vtkjs = None
if self.serialize_policy == 'instantiation':
self._vtkjs = self._get_vtkjs()

@classmethod
def applies(cls, obj):
Expand Down Expand Up @@ -247,7 +258,7 @@ def construct_colorbars(self, orientation='horizontal'):

def _init_properties(self):
return {k: v for k, v in self.param.get_param_values()
if v is not None and k not in ['default_layout', 'object', 'infer_legend']}
if v is not None and k not in ['default_layout', 'object', 'infer_legend', 'serialize_policy']}

@classmethod
def register_serializer(cls, class_type, serializer):
Expand All @@ -260,32 +271,33 @@ def register_serializer(cls, class_type, serializer):
cls._serializers.update({class_type:serializer})

def _get_vtkjs(self):
if self.object is None:
vtkjs = None
elif isinstance(self.object, string_types) and self.object.endswith('.vtkjs'):
if os.path.isfile(self.object):
with open(self.object, 'rb') as f:
vtkjs = f.read()
if self._vtkjs is None and self.object is not None:
if isinstance(self.object, string_types) and self.object.endswith('.vtkjs'):
if os.path.isfile(self.object):
with open(self.object, 'rb') as f:
vtkjs = f.read()
else:
data_url = urlopen(self.object)
vtkjs = data_url.read()
elif hasattr(self.object, 'read'):
vtkjs = self.object.read()
else:
data_url = urlopen(self.object)
vtkjs = data_url.read()
elif hasattr(self.object, 'read'):
vtkjs = self.object.read()
else:
available_serializer = [v for k, v in VTK._serializers.items() if isinstance(self.object, k)]
if len(available_serializer) == 0:
import vtk
from .vtkjs_serializer import render_window_serializer
available_serializer = [v for k, v in VTK._serializers.items() if isinstance(self.object, k)]
if len(available_serializer) == 0:
import vtk
from .vtkjs_serializer import render_window_serializer

VTK.register_serializer(vtk.vtkRenderWindow, render_window_serializer)
serializer = render_window_serializer
else:
serializer = available_serializer[0]
vtkjs = serializer(self.object)
VTK.register_serializer(vtk.vtkRenderWindow, render_window_serializer)
serializer = render_window_serializer
else:
serializer = available_serializer[0]
vtkjs = serializer(self.object)
self._vtkjs = vtkjs

return vtkjs
return self._vtkjs

def _update(self, model):
self._vtkjs = None
vtkjs = self._get_vtkjs()
model.data = base64encode(vtkjs) if vtkjs is not None else vtkjs

Expand Down

0 comments on commit 536327f

Please sign in to comment.