I am sure you have built network graphs in python before using a special library known as Networkx. Have you ever wondered if there was a way to interact with graphs? Guess what?! There is a library named Pyvis which helps to improve the interactivity of network graphs in Python programming language.
Also Read: NetworkX Package – Python Graph Library
Installing the Pyvis library is simple and straightforward that can be done using the pip command in the command prompt of the system using the command below.
Let’s now move on to the code implementation of the interactive network graphs using the Pyvis library in Python programming language. We will start off by importing all the necessary libraries/modules using the code snippet below.
from pyvis import network as net from IPython.core.display import display, HTML import random
We will start off by creating a network graph with only nodes and no edges between them. The creation of an empty graph can be done using the Network function which specifies the properties of the network graph inside it including the background color, heading, height, and width.
Next, we will make use of the
add_node function to add nodes to the network graph. We will be adding 10 nodes (from 1 to 10) and then convert the network graph into HTML format to add interactivity and save the HTML file as well.
g_only_nodes = net.Network(height='600px',width='90%', bgcolor='white',font_color="red", heading="Networkx Graph with only Nodes") for i in range(1,11): g_only_nodes.add_node(i) g_only_nodes.show('Only_Nodes.html') display(HTML('Only_Nodes.html'))
Have a look at what the network graph with only nodes looks like.
The next step in the creation of network graphs is adding edges between the nodes. We will be adding random edges between random nodes. Have a look at the function for the same below.
def generate_edge(): s = random.randint(1,10) d = random.randint(1,10) return (s,d)
In the function, we will be generating random source and destination nodes pair using the
random.randint function. We will be getting random nodes between 1 and 10. To make sure we have enough edges; we will be generating 20 random edges. To make sure that the same edge is not repeated again and again, we will be keeping a record of the pairs of (source, destination) nodes. Have a look at the code below.
g = net.Network(height='600px',width='90%', bgcolor='white',font_color="red", heading="A Simple Networkx Graph") for i in range(1,11): g.add_node(i) i=0 chosen_set =  while(i!=20): eg = generate_edge() if(eg!=eg and not (eg in chosen_set)): chosen_set.append(eg) g.add_edge(eg,eg) i+=1 g.show('Simple_Network_Graph.html') display(HTML('Simple_Network_Graph.html'))
After the addition of edges, we will have a network graph that looks something like the one below. Look how amazing the network graph turns out to be and how interactive the graph is!
Pyvis is a powerful python module for visualizing and interactively manipulating network graphs using Python programming language. I hope you were able to build the network graphs using the library and enjoyed interacting with the graphs.
Thank you for reading!
Happy coding! 😃
Also Read: Network Analysis in Python – A Complete Guide