force.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. """
  2. ==========
  3. Javascript
  4. ==========
  5. Example of writing JSON format graph data and using the D3 Javascript library to produce an HTML/Javascript drawing.
  6. """
  7. # Author: Aric Hagberg <aric.hagberg@gmail.com>
  8. # Copyright (C) 2011-2019 by
  9. # Aric Hagberg <hagberg@lanl.gov>
  10. # Dan Schult <dschult@colgate.edu>
  11. # Pieter Swart <swart@lanl.gov>
  12. # All rights reserved.
  13. # BSD license.
  14. import json
  15. import flask
  16. import networkx as nx
  17. from networkx.readwrite import json_graph
  18. G = nx.barbell_graph(6, 3)
  19. # this d3 example uses the name attribute for the mouse-hover value,
  20. # so add a name to each node
  21. for n in G:
  22. G.nodes[n]['name'] = n
  23. # write json formatted data
  24. pen_weight = json_graph.node_link_data(G) # node-link format to serialize
  25. # write json
  26. json.dump(pen_weight, open('force/force.json', 'w'))
  27. print('Wrote node-link JSON data to force/force.json')
  28. # Serve the file over http to allow for cross origin requests
  29. app = flask.Flask(__name__, static_folder="force")
  30. @app.route('/')
  31. def static_proxy():
  32. return app.send_static_file('force.html')
  33. print('\nGo to http://localhost:8000 to see the example\n')
  34. app.run(port=8000)