Arthur Waeber
Arthur Waeber

Reputation: 21

click event on network's node using pyvis on jupyter lab

I am working on a project in applied data analysis and was trying to add a click event to some precise nodes in the network G. I found some poor documentation on this topic and tried to implement this in my code. Here below is a simplified example, trying to make node 1 open a wikipedia page when eft-clicking on it through a json file read as options or interaction. The url is automatically opened without even having to click on the node and clicking n the node doesn't do anything.. It seems that Jupyter just ignores my "interaction" and "click" hierarchy. I am stucked and ask for your help! thanks a lot !

import webbrowser
import pyvis
from pyvis import network
from pyvis.network import Network

name = 'Freddie Mercury'
url = "https://en.wikipedia.org/wiki/"+name

def display_page(url):
    webbrowser.open_new(url)

G = Network(height='400px', width='80%', bgcolor='white', notebook=True, font_color ='black')
G.add_node(1)
G.add_node(2)
G.add_edges([(1,2,4)])

options = {
          "nodes":{
              "font":{
                  "size": 50,
                  "bold":True
              }
          },
          "edges":{
              "color":'red',
              "smooth":False
          },
          "physics":{
              "barnesHut":{
                  "gravitationalConstant":-500000,
                  "centralGravity":12,
                  "springLength": 50,
                  "springConstant": 0.7,
                  "damping": 3,
                  "avoidOverlap": 10
              }
          },
          "intercation":{   
               "click":{
                   "nodes": ["1"],
                   "event":[display_page(url)]
}}}

G.options=options

network.Network.show(G,'networkx_click_event.html')

Upvotes: 2

Views: 4069

Answers (2)

mis7er.n9o
mis7er.n9o

Reputation: 17

hello to fire event you do :

net = Network("1000px", "1000px")
net.add_node(....)
net.add_event_listener("selectNode", callback_function)

Upvotes: -1

Carl F. Corneil
Carl F. Corneil

Reputation: 192

You've misspelled "interaction", but are you sure the package pyvis actually has "click" as a configuration option under interaction?

From what I can gather from the documentation pyvis is a wrapper around visjs, and these are the interaction-configurations available under the network: https://visjs.github.io/vis-network/docs/network/interaction.html# I dont see any available "click" option, but maybe you could explore the "navigationButtons"?

Upvotes: 2

Related Questions