Webwidget Update on Slicer Events

Hello,

I’m using a qSlicerWebWidget to display some interactive plots in Slicer. I’m wondering if there’s a way to call a method within my javascript/html when something changes in the Slicer scene (add a listener within the webwidget). For example, I would like to update what is displayed in my qSlicerWebWidget if a sequence in my Slicer scene changes frame, or if the crosshair moves.

Is there a way to do this without reloading the entire html and assigning it to the WebWidget? Or is the only available interaction between webwidget and Slicer window.slicerPython.evalPython?

I know I could achieve this my adding a listener within python and then re-loading the full HTML each time, I’m just trying to keep it fast.

Yes, you can communicate both ways. window.slicerPython.evalPython lets the JavaScript execute Python code in Slicer’s environment, and qSlicerWebWidget.evalJS lets Python or C++ code execute JS code.

You can connect to the qSlicerWebWidget.evalResult signal to get the result of your computation. Since the JS code runs asynchronously, the signal returns both the evaluated JS and the result so you can keep track of which code generated the result.

It’s all event driven and runs very fast.