Creating screenshots with pyvista or vtk extension


I’ve been stuck with a seemingly simple task and I’m not sure if I’m overlooking something obvious. I’m using panel with vtk/pyvista extension and my simple goal is to get a screenshot out from the current plotter in the dashboard.
I attached simple example with a button to take a screenshot. However, the saved screenshot is not from the active plotter but it seems from one in the background.

Left is my panel dashboard and right is the screenshot:

Can I anyone tell me how can I save the screenshot from the current viewpoint of the pyvista plotter? Or any other way to grab the screenshot from the dashboard would work for me. Happy to switch to another package, as long as I can do volume / iso surface rendering.


Here is the code:

import panel as pn
import pyvista as pv

class Test():

    def __init__(self) -> None:
        self.plotter = pv.Plotter(off_screen=True)
        arrow = pv.Arrow()
        self.plotter.add_mesh(arrow, color='blue')
        self.vtk_pane = pn.pane.VTK(self.plotter.ren_win, width=400, height=400)

        self.button = pn.widgets.Button(name='Screenshot', button_type='primary')

        self.panel = pn.Column(self.button, self.vtk_pane)

    def on_click(self,event):
        screenshot_path = "screenshot.png"
        print(f"Screenshot saved to {screenshot_path}")

if __name__ == "__main__":
    test = Test()