Generator functions and user interfaces

psaffrey at googlemail.com psaffrey at googlemail.com
Wed Sep 10 12:38:57 EDT 2008


I'm trying to implement an interactive graph visualisation tool using
matplotlib.

I want to use a spring layout, where nodes repulse each other and
edges act as springs to pull connected nodes together. Usually, this
algorithm runs through a number of iterations of attraction/repulsion
to allow the nodes to converge to suitable positions. However, rather
than running all these iterations to lay out the graph and then
rendering it, I want to animate the graph as it is "springing" into
place, and then allow the user to drag nodes around and have the graph
redraw on the fly.

My idea for doing this was to use a generator function, where I yield
the position of the nodes after each iteration and then call draw() on
the position yielded. Does this seem like a sensible approach? The
problem is that the node positions that are being operated on by the
generator function may be altered by user input - dragging the nodes -
and I'm not sure if this will break the way that the new positions are
yielded. How do I use a generator function that might stop (when the
nodes stop moving) but then need to restart again (once the user moves
the nodes)?

I'm quite an experienced Python programmer but I've never taken the
trouble to get my head around generator functions, so any guidance
welcome!

Peter



More information about the Python-list mailing list