import ipycytoscape
import ipywidgets as widgets
import networkx as nx
G = nx.complete_graph(5)
undirected = ipycytoscape.CytoscapeWidget()
undirected.graph.add_graph_from_networkx(G)
undirected
The above graph should update when you run the next cell
G2 = nx.Graph()
G2.add_node('separate node 1')
G2.add_node('separate node 2')
G2.add_edge('separate node 1', 'separate node 2')
undirected.graph.add_graph_from_networkx(G2)
add_graph_from_networkx
takes an argument directed
that if True will ensure all edges given the directed class, which will style them with an arrow.
G = nx.complete_graph(5)
directed = ipycytoscape.CytoscapeWidget()
directed.graph.add_graph_from_networkx(G, directed=True)
directed
You can also make graphs with both directed and undirected edges by adding 'directed' to the 'classes' attribute of the edge data
from random import random
G = nx.complete_graph(5)
for s, t, data in G.edges(data=True):
if random() > .5:
G[s][t]['classes'] = 'directed'
mixed = ipycytoscape.CytoscapeWidget()
mixed.graph.add_graph_from_networkx(G)
mixed