import piplite
await piplite.install(['ipycytoscape', 'pandas', 'networkx'])
import ipycytoscape as cy
cyGraph = cy.CytoscapeWidget()
cytoscape_json = {
'nodes': [
{ 'data': { 'id': '0', 'name': 'Cytoscape', 'classes': 'node' }},
{ 'data': { 'id': '1', 'name': 'Grid', 'classes': 'node' }},
{ 'data': { 'id': '2', 'name': 'Cola', 'classes': 'node' }},
{ 'data': { 'id': '3', 'name': 'Popper', 'classes': 'node' }},
{ 'data': { 'id': '4', 'name': 'Cytoscape.js', 'classes': 'node'}}
],
'edges': [
{'data': { 'source': '4', 'target': '0' }},
{'data': { 'source': '1', 'target': '2' }},
{'data': { 'source': '1', 'target': '3' }},
{'data': { 'source': '2', 'target': '3' }},
{'data': { 'source': '4', 'target': '4' }},
{'data': { 'source': '4', 'target': '3' }},
]
}
All of the examples in this notebook are created in loco for sake of simplicity but it's also possible to load them externally in many different ways.
For an extensive description of all the attributes available on a cytoscape graph, check the cytoscape documentation.
cyGraph.graph.add_graph_from_json(cytoscape_json)
cyGraph
cyGraph.set_layout(name='random')
cyGraph
import pandas as pd
pdCy = cy.CytoscapeWidget()
robots = ['marvin','c3po','r2d2','data']
universe = ['douglas adams','star wars','star wars','star trek']
cooleness_lvl = ['10', '3', '10', '10']
robotsRates = list(zip(robots, universe, cooleness_lvl))
df = pd.DataFrame(data = robotsRates, columns=['robot', 'universe', 'cooleness_lvl'])
pdCy.graph.add_graph_from_df(df, ['universe'], ['robot', 'cooleness_lvl'])
pdCy
pdCy.graph.nodes
edge = cy.Edge(data={"source": 1, "target": 2})
pdCy.graph.add_edge(edge)
edge = cy.Edge(data={"source": 'parent-2', "target": 'parent-1'})
pdCy.graph.add_edge(edge)
pdCy.graph.edges
pdCy.set_style([
{
'selector': 'node[name] *= ""',
'style': {
'background-color': 'blue',
}},
{
'selector': '[id *= "parent-1"]',
'style': {
'background-color': 'yellow',
}},
{
'selector': '[id *= "parent-2"]',
'style': {
'background-color': 'red',
'color': 'blue'
}
}])
pdCy
More information about cytoscape selectors are available here.
pdCy.set_tooltip_source('name')
import networkx as nx
nxCyGraph = cy.CytoscapeWidget()
nxGraph = nx.complete_graph(5)
nxCyGraph.graph.add_graph_from_networkx(nxGraph)
nxCyGraph
directCyGraph = cy.CytoscapeWidget()
directNxGraph = nx.complete_graph(4)
directCyGraph.graph.add_graph_from_networkx(directNxGraph, directed=True)
directCyGraph
from random import random
mixedNxGraph = nx.complete_graph(5)
for s, t, data in mixedNxGraph.edges(data=True):
if random() > .5:
mixedNxGraph[s][t]['classes'] = 'directed'
mixedGraph = cy.CytoscapeWidget()
mixedGraph.graph.add_graph_from_networkx(mixedNxGraph)
mixedGraph
Custom Nodes and Edges can be created for any kinds of graphs, meaning it's not only restricted to networkx objects. You just have to use the ipycytoscape API for that. We see an example of the API's use on the Pandas example.
class CustomNode(cy.Node):
def __init__(self, name, classes = ''):
super().__init__()
self.data['id'] = name
self.classes = classes
n1 = CustomNode("node 1", classes = 'first')
n2 = CustomNode("node 2", classes = 'second')
G = nx.Graph()
G.add_node(n1)
G.add_node(n2)
G.add_edge(n1, n2)
customInheritedGraph = cy.CytoscapeWidget()
customInheritedGraph.graph.add_graph_from_networkx(G)
customInheritedGraph.graph.nodes
#from py2neo import Graph
#cy.add_graph_from_neo4j(neo4j_graph)
import ipywidgets as widgets
cyGraph
cyGraph.set_style([{
"selector": "edge.highlighted",
"css": {
"line-color": "red"
}
}])
btn = widgets.Button(description = "red edges", disabled = False)
def btn_callback(b):
for edge in cyGraph.graph.edges:
edge.classes = " highlighted"
btn.on_click(callback = btn_callback)
display(btn)
def paint_blue(event):
auxNode = cyGraph.graph.nodes[int(event['data']['id'])]
auxNode.classes += ' blue'
cyGraph.on('node', 'click', paint_blue)
cyGraph.set_style([{
"selector": "edge.highlighted",
"css": {
"line-color": "red"
}
},
{
"selector": "node.blue",
"css": {
"background-color": "blue"
},
}])
cyGraph
List of events
These events can be applied to either nodes
or edges
objects: