From the ipycytoscape documentation I can see the following:
import ipycytoscape as cs
from ipycytoscape import *
import networkx as nx
# === create custom node class (inherits from cytoscape node class)
class CustomNode(cs.Node):
def __init__(self, name, classes=''):
super().__init__()
self.data['id'] = name
self.classes = classes
n1 = CustomNode("node 1", classes='class1')
n2 = CustomNode("node 2", classes='class2')
# === create graph, add custom nodes and an edge
G = nx.Graph()
G.add_node(n1)
G.add_node(n2)
G.add_edge(n1, n2, directed=True)
My question is a fundamental one, rather than looking for helping with code. Above n1 and n2 are built with a class inheriting from a ipycytoscape class CustomNode(cs.Node). But they are added as nodes to a networkx graph !!!
How is that possible? What I mean is that a ipycytoscape object is added to a networkx object.
From: https://networkx.org/documentation/stable/tutorial.html#what-to-use-as-nodes-and-edges
You might notice that nodes and edges are not specified as NetworkX objects. This leaves you free to use meaningful items as nodes and edges. The most common choices are numbers or strings, but a node can be any hashable object (except None), and an edge can be associated with any object x using G.add_edge(n1, n2, object=x).
So this isn't specific to ipycytoscape, you can use any custom object as a Node for networkx. Like so:
import networkx as nx
class myClass:
def __init__(self, something):
self.something = something
n1 = myClass(53)
n2 = myClass("node 2")
# === create graph, add custom nodes and an edge
G = nx.Graph()
G.add_node(n1)
G.add_node(n2)
G.add_edge(n1, n2, directed=True)
Inheriting from the ipycytoscape Node object is only done to make it work more easily with ipycytoscape.