from graph_tool.all import *
from numpy.random import *
from numpy.linalg import norm
import sys, os, os.path
from gi.repository import GLib
import graph_tool.all as gt; import matplotlib; import math; import numpy;
from gi.repository import Gtk, Gdk, GdkPixbuf, GObject
g = random_graph(500, lambda: 1 + poisson(5), directed=False)
#step = 0.05 # move step
step=0.05
K = 0.5 # preferred edge length
pos = sfdp_layout(g, K=K, max_iter=1) # initial layout positions
offscreen = sys.argv[1] == "offscreen" if len(sys.argv) > 1 else False
max_count = 50
if offscreen and not os.path.exists("./frames"):
os.mkdir("./frames")
if not offscreen:
win = GraphWindow(g, pos, geometry=(400, 400),bg_color=[0,0,0,1])
else:
win = Gtk.OffscreenWindow()
win.set_default_size(400, 400)
win.graph = GraphWidget(g, pos)
win.add(win.graph)
count = 0
def update_state():
global count
sfdp_layout(g, pos=pos, K=K, init_step=step, max_iter=1)
count += 1
win.graph.fit_to_window(ink=True)
win.graph.regenerate_surface()
win.graph.queue_draw()
# if doing an offscreen animation, dump frame to disk
if offscreen:
pixbuf = win.get_pixbuf()
pixbuf.savev(r'./frames/dancing%06d.png' % count, 'png', [], [])
if count > max_count:
sys.exit(0)
return True
# Bind the function above as an 'idle' callback.
cid = GLib.idle_add(update_state)
# We will give the user the ability to stop the program by closing the window.
win.connect("delete_event", Gtk.main_quit)
# Actually show the window, and start the main loop.
win.show_all()
Gtk.main()