Basic use example of visJS2jupyter


Authors: Brin Rosenthal (sbrosenthal@ucsd.edu), Mikayla Webster (m1webste@ucsd.edu), Julia Len (ljen@ucsd.edu)


This notebook has been tested, and should work with both networkx versions 1.11 and 2.1 and both Python 2 and 3

Import packages


In [4]:
import networkx as nx
import visJS2jupyter.visJS_module

Draw a simple network with default parameters


In [7]:
G = nx.connected_watts_strogatz_graph(30, 5, 0.2)
nodes = list(G.nodes()) # must cast to list to maintain compatibility between nx 1.11 and 2.0
edges = list(G.edges()) # will return an "EdgeView" object in nx 2.0

In [10]:
# define the initial positions of the nodes using networkx's spring_layout function, and add to the nodes_dict.
pos = nx.spring_layout(G)

nodes_dict = [{"id":n,
              "x":pos[n][0]*300,
              "y":pos[n][1]*300} for n in nodes]

node_map = dict(zip(nodes,range(len(nodes))))  # map to indices for source/target in edges

edges_dict = [{"source":node_map[edges[i][0]], "target":node_map[edges[i][1]], 
              "title":'test'} for i in range(len(edges))]

visJS2jupyter.visJS_module.visjs_network(nodes_dict, edges_dict)


Out[10]:
Network | Basic usage

In [ ]: