PDA

View Full Version : Getting Data from a javascript object OR embedding pyvis.network object in QT widget



octohat
12th December 2018, 18:56
Hi! My high level problem is this:

I can generate a snazzy interactive network graph with pyvis.network (a python package)
I can generate fancy controls and buttons with QT

So far, I can't get them to communicate with each other.

Solution Attempt 1:

I generate the pyviz object and write to html.
then load the html in my widget:
(into a QtWebEngineWidgets.QWebEngineView)
self.load(QtCore.QUrl().fromLocalFile(myfile.html) )

that works great... but swimming through all the methods I couldn't find how to access the data (it's embedded in a javascript script object)

Solution Attempt 2:

I can't find a way to load the python object pyvis.network itself into a widget (bypassing write to html)

Any thoughts ideas? Let me know if you need more info.

d_stranz
13th December 2018, 17:24
So far, I can't get them to communicate with each other.

I presume you are using PyQt for the GUI, right? Almost all Qt GUI widgets will generate one or more signals when you interact with them (like clicking with the mouse, typing, etc.). In order to do anything with those signals you have to write slots in your own GUI containers (composite QWidgets like QDialog, QFrame, etc.) and connect those to the objects and their signals.

I don't know much about pyvis.network, but when you say it is "interactive" I assume you mean that you can click on nodes or edges and can cause something to happen. Internally, there should be methods you can call to simulate these kinds of interactions programmatically. I would guess there are also Python events that get generated through a user interaction which you can trap programmatically.

So for the Qt GUI to communicate to the Python object, your slots will need to call the pyviz.network methods that result in the change you want. To go the other way, the pyviz.network events that result from a user interaction need to be trapped and relayed to the Qt side by turning them into Qt signals emitted by your GUI.

Here's a link to a stackoverflow discussion of how to connect matplotlib and Qt. (https://stackoverflow.com/questions/12459811/how-to-embed-matplotlib-in-pyqt-for-dummies) as well as an example on the matplotlib site (https://matplotlib.org/examples/user_interfaces/embedding_in_qt4.html).