~ubuntu-branches/ubuntu/trusty/python-networkx/trusty-proposed

« back to all changes in this revision

Viewing changes to examples/ubigraph/dynamic_shortest_path.py

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2009-02-28 13:36:24 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090228133624-9l5rwi1ftlzc7b0l
* Upload to unstable now that lenny is released (yay).
* Fix FTBFS with python-support 0.90.3: no longer rely on its internal
  behaviour, and xnow set tests/test.py executable right after “setup.py
  install” (Closes: #517065).
* Drop executable bits from bz2 files.
* Update Vcs-* fields: move from DPMT's svn to collab-maint's git.
* Remote DPMT from Uploaders, following Piotr Ożarowski's request.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
"""
 
3
Dynamically draw shortest path in Watts-Stogatz small world graph using 
 
4
Ubigraph callbacks.
 
5
 
 
6
"""
 
7
__author__ = """Aric Hagberg (hagberg@lanl.gov)\n Katy Bold (kbold@princeton.edu"""
 
8
#    Copyright (C) 2008 by 
 
9
#    Aric Hagberg <hagberg@lanl.gov>
 
10
#    Dan Schult <dschult@colgate.edu>
 
11
#    Pieter Swart <swart@lanl.gov>
 
12
#    Distributed under the terms of the GNU Lesser General Public License
 
13
#    http://www.gnu.org/copyleft/lesser.html
 
14
 
 
15
 
 
16
def vertex_callback(id):
 
17
    import sys
 
18
    global start
 
19
    try:
 
20
        if start is not None:
 
21
            s=nx.shortest_path(G,G.idnode[start],G.idnode[id])
 
22
            e=zip(s[0:-1],s[1:])
 
23
            epath=[(u,v,G.get_edge(u,v)) for u,v in e]
 
24
            print >> sys.stderr, s
 
25
            G.set_edge_attr(epath,style=wideyellow)
 
26
            start=None
 
27
        else:
 
28
            start=id
 
29
            G.set_edge_attr(style=gray)
 
30
    except:
 
31
        return -1
 
32
    return 0
 
33
 
 
34
 
 
35
 
 
36
if __name__ == "__main__":
 
37
    import random
 
38
    import networkx as nx
 
39
 
 
40
    print "double click nodes to find shortest path"
 
41
    G=nx.UbiGraph(nx.watts_strogatz_graph(50,4,0.1))
 
42
    G.node_labels() # turn on node labels
 
43
    G.set_edge_attr(color='#3f3f3f') # dark grey edges
 
44
    gray=G.new_edge_style(color='#3f3f3f',width='2.0')
 
45
    wideyellow=G.new_edge_style(color='#ffff00',width='6.0')
 
46
 
 
47
    start=None
 
48
 
 
49
 
 
50
    # call back server        
 
51
    myPort = random.randint(20739,20999)
 
52
    # Set up a callback for left double-clicks on vertices.
 
53
    G.ubigraph.set_vertex_style_attribute(0, "callback_left_doubleclick", 
 
54
                "http://127.0.0.1:" + str(myPort) + "/vertex_callback")
 
55
 
 
56
    # Now make an XMLRPC server to handle tha callbacks.
 
57
    from SimpleXMLRPCServer import SimpleXMLRPCServer
 
58
    # Create server
 
59
    server = SimpleXMLRPCServer(("localhost", myPort))
 
60
    server.register_introspection_functions()
 
61
    server.register_function(vertex_callback)
 
62
    # Run the server's main loop
 
63
    print "Listening for callbacks from ubigraph_server on port " + str(myPort)
 
64
    server.serve_forever()