Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / examples / javascript / force.py @ 5cef0f13

History | View | Annotate | Download (1.11 KB)

1
"""
2
==========
3
Javascript
4
==========
5

6
Example of writing JSON format graph data and using the D3 Javascript library to produce an HTML/Javascript drawing.
7
"""
8
# Author: Aric Hagberg <aric.hagberg@gmail.com>
9

    
10
#    Copyright (C) 2011-2019 by
11
#    Aric Hagberg <hagberg@lanl.gov>
12
#    Dan Schult <dschult@colgate.edu>
13
#    Pieter Swart <swart@lanl.gov>
14
#    All rights reserved.
15
#    BSD license.
16
import json
17

    
18
import flask
19
import networkx as nx
20
from networkx.readwrite import json_graph
21

    
22
G = nx.barbell_graph(6, 3)
23
# this d3 example uses the name attribute for the mouse-hover value,
24
# so add a name to each node
25
for n in G:
26
    G.nodes[n]['name'] = n
27
# write json formatted data
28
d = json_graph.node_link_data(G)  # node-link format to serialize
29
# write json
30
json.dump(d, open('force/force.json', 'w'))
31
print('Wrote node-link JSON data to force/force.json')
32

    
33
# Serve the file over http to allow for cross origin requests
34
app = flask.Flask(__name__, static_folder="force")
35

    
36
@app.route('/')
37
def static_proxy():
38
    return app.send_static_file('force.html')
39

    
40
print('\nGo to http://localhost:8000 to see the example\n')
41
app.run(port=8000)