Skip to content

Commit 543844c

Browse files
fix: Improve kwargs processing in show
1 parent 3399a1f commit 543844c

File tree

3 files changed

+48
-11
lines changed

3 files changed

+48
-11
lines changed

src/ansys/tools/visualization_interface/backends/pyvista/pyvista.py

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
# SOFTWARE.
2222
"""Provides a wrapper to aid in plotting."""
23-
from abc import abstractmethod
23+
from abc import Callable, abstractmethod
2424
import importlib.util
2525
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
2626

@@ -405,15 +405,43 @@ def disable_hover(self):
405405
"""Disable hover capabilities in the plotter."""
406406
self._hover_widget.EnabledOff()
407407

408+
def __extract_kwargs(self, func_name: Callable, input_kwargs: Dict[str, Any]) -> Dict[str, Any]:
409+
"""Extracts the keyword arguments from a function signature and returns it as dict.
410+
411+
Parameters
412+
----------
413+
func_name : Callable
414+
Function to extract the keyword arguments from. It should be a callable function
415+
input_kwargs : Dict[str, Any]
416+
Dictionary with the keyword arguments to update the extracted ones.
417+
418+
Returns
419+
-------
420+
Dict[str, Any]
421+
Dictionary with the keyword arguments extracted from the function signature and
422+
updated with the input kwargs.
423+
"""
424+
import inspect
425+
signature = inspect.signature(func_name)
426+
kwargs = {}
427+
for k, v in signature.parameters.items():
428+
# We are ignoring positional arguments, and passing everything as kwarg
429+
if v.default is not inspect.Parameter.empty:
430+
kwargs[k] = v.default
431+
for k, v in kwargs.items():
432+
# my_args is the arguments passed to the ansys-visualization API
433+
if k in input_kwargs:
434+
kwargs[k] = input_kwargs[k]
435+
return kwargs
436+
408437
def show(
409438
self,
410439
plottable_object: Any = None,
411440
screenshot: Optional[str] = None,
412441
view_2d: Dict = None,
413442
name_filter: str = None,
414443
dark_mode: bool = False,
415-
plotting_options: Optional[Dict[str, Any]] = {},
416-
**show_options: Dict[str, Any],
444+
**kwargs: Dict[str, Any],
417445
) -> List[Any]:
418446
"""Plot and show any PyAnsys object.
419447
@@ -432,10 +460,7 @@ def show(
432460
Regular expression with the desired name or names to include in the plotter.
433461
dark_mode : bool, default: False
434462
Whether to use dark mode for the widgets.
435-
plotting_options : dict, default: None
436-
Keyword arguments. For allowable keyword arguments, see the
437-
:meth:`Plotter.add_mesh <pyvista.Plotter.add_mesh>` method.
438-
**show_options : Any
463+
**kwargs : Any
439464
Additional keyword arguments for the show method.
440465
441466
Returns
@@ -444,6 +469,14 @@ def show(
444469
List with the picked bodies in the picked order.
445470
446471
"""
472+
plotting_options = self.__extract_kwargs(
473+
self._pl._scene.add_mesh,
474+
kwargs,
475+
)
476+
show_options = self.__extract_kwargs(
477+
self._pl.scene.show,
478+
kwargs,
479+
)
447480
self.plot(plottable_object, name_filter, **plotting_options)
448481
if self._pl.object_to_actors_map:
449482
self._object_to_actors_map = self._pl.object_to_actors_map
@@ -479,6 +512,8 @@ def show(
479512
# Update all buttons/widgets
480513
[widget.update() for widget in self._widgets]
481514

515+
# Remove screenshot from show options since we pass it manually
516+
show_options.pop("screenshot", None)
482517
self.show_plotter(screenshot, **show_options)
483518

484519
picked_objects_list = []

src/ansys/tools/visualization_interface/backends/pyvista/pyvista_interface.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,9 @@ def show(
328328
jupyter_backend : str, default: None
329329
PyVista Jupyter backend.
330330
**kwargs : dict, default: None
331-
Plotting keyword arguments. For allowable keyword arguments, see the
332-
:meth:`Plotter.show <pyvista.Plotter.show>` method.
331+
Plotting and show keyword arguments. For allowable keyword arguments, see the
332+
:meth:`Plotter.show <pyvista.Plotter.show>` and
333+
:meth:`Plotter.show <pyvista.Plotter.add_mesh>` methods.
333334
334335
Notes
335336
-----
@@ -360,6 +361,7 @@ def show(
360361
if kwargs.get("screenshot") is not None:
361362
self.scene.off_screen = True
362363
if jupyter_backend:
364+
kwargs.pop("jupyter_backend", None)
363365
self.scene.show(jupyter_backend=jupyter_backend, **kwargs)
364366
else:
365367
if self._use_qt:

tests/test_generic_plotter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,5 +188,5 @@ def test_plotter_show_mix():
188188
# Plot
189189
pl.plot(sphere1, opacity=0.5, color="blue")
190190

191-
# Mix plot and show
192-
pl.show(sphere, plotting_options={"show_edges": True}, cpos="xy")
191+
# Mix plot and show kwargs
192+
pl.show(sphere, show_edges=True, cpos="xy")

0 commit comments

Comments
 (0)