How can I set other edges invisible when I click on a node in pyvis.network // networkx?

1.5k views Asked by At

Data: Currently I am working on a quite big network. Therefore I am sending a picture of a part of nodes and edges. enter image description here

pos = nx.nx_agraph.graphviz_layout(G, prog="twopi")
Xs=[a[0] for a in pos.values()]
Ys=[a[1] for a in pos.values()]

from pyvis.network import Network
g=Network(height=2000,width=2000,directed=True)
g.from_nx(G)
g.add_nodes(Nodes,x=Xs,y=Ys)
g.barnes_hut(gravity=-80000,central_gravity=0.5,spring_length=150,spring_strength=0.001,damping=0.09,overlap=0)
g.show('nx.html')
 
  • What I am trying to do is setting other edges invisible when I click on a node. For example, if I click on 'node A', I just want to see just the node A's edges.*

enter image description here

What can I do to create this?

1

There are 1 answers

2
Paritosh Kulkarni On BEST ANSWER

If I can understand correctly, you want to hide certain edges after selection of particular node. I do not quite understand the network image, however I am assuming there is a certain node A that you need to add separately or a list of nodes with the functionality:

Since Pyvis is build on top of Visjs these are the interaction-configurations available under the network: https://visjs.github.io/vis-network/docs/network/interaction.html

I cannot see any hide edges option. However you can use these two -

"selectConnectedEdges"
*#Boolean   *true*  
#When true, on selecting a node, its connecting edges are highlighted.*

OR

**hoverConnectedEdges** 
#Boolean    **true**    
#When true, on hovering over a node, it's connecting edges are highlighted

Working notebook - https://colab.research.google.com/drive/1S3zpSotmAhwx_8Qo81vvTk_egpNpStPu?usp=sharing

Example Here is a sample code

#!pip install pyvis
import pyvis
from pyvis import network
from pyvis.network import Network



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

G.add_edges([(1,2,4),(1,3,2),(2,3,6)])

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
              }
          },
          "interaction":{   
               "selectConnectedEdges": True

}}

G.options=options

G.show('sample.html')


import IPython
IPython.display.HTML(filename= r"/content/sample.html")