Embed html file into the scripted module

Operating system: Ubuntu18.04
Slicer version: 4.13.0-2021-07-21 r3042 / 2a223c0

Hello,
I am trying to generate a scripted module with embed html. Then I use webEngineView and set the webEngineView url to my html file. Since I want html js to interact with slicer, I use websockets to allow the two to interact, but whether I use multi-process or Cli Module to run websockets server, I can’t operate view node in Slicer. So, is there any good solution to implement the interaction between the embedded HTML components and the Slicer scene?

Thank you

1 Like

Hi -

No need for a websocket, you can communicate directly from python to javascript or javascript to python using the qSlicerWebWidget. See the test code linked below for examples. It’s all asynchronous and smooth.

Here’s an example of some demo code that mixes a web graph with the segment editor.

If you communicate via the web widget data is passed in ascii, which is usually fast enough but if it’s not you can communicate asynchronously by integrating the sockets with the Qt event loop using tools like the QSocketNotifier, which is used in the SlicerWeb code.

1 Like

Thanks for the reply pieper.

It works for me. I use qSlicerWebWidget and set html like this:

and in html, import the js to enable window.slicerPython,
then I can use window.slicerPython.evalPython and self.webWidget.evalJS to communicate from python to javascript and javascript to python.

Thanks a lot :grinning:

1 Like

Note that you can use JavaScript widgets via Jupyter notebooks. There are tons of reusable widgets for interactive plotting and many other purposes.

2 Likes

So sorry, it’s no need to import the js file :sweat_smile: