Commit 373d35f1 authored by Julien Muchembled's avatar Julien Muchembled

demo: update graph without reloading the whole page

parent 0bb9d147
...@@ -306,13 +306,14 @@ if len(sys.argv) > 1: ...@@ -306,13 +306,14 @@ if len(sys.argv) > 1:
class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler): class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
_path_match = re.compile('/(.+)\.html$').match _path_match = re.compile('/(.+)\.(html|svg)$').match
pages = 'ipv6', 'ipv4', 'tunnels' pages = 'ipv6', 'ipv4', 'tunnels'
def do_GET(self): def do_GET(self):
svg = None svg = None
try: try:
page = self.pages.index(self._path_match(self.path).group(1)) name, ext = self._path_match(self.path).groups()
page = self.pages.index(name)
except AttributeError, ValueError: except AttributeError, ValueError:
if self.path == '/': if self.path == '/':
self.send_response(302) self.send_response(302)
...@@ -322,7 +323,7 @@ if len(sys.argv) > 1: ...@@ -322,7 +323,7 @@ if len(sys.argv) > 1:
self.send_error(404) self.send_error(404)
return return
if page < 2: if page < 2:
svg = route_svg(page) body = route_svg(page)
else: else:
gv = registry.Popen(('python', '-c', r"""if 1: gv = registry.Popen(('python', '-c', r"""if 1:
import math, json import math, json
...@@ -349,22 +350,38 @@ if len(sys.argv) > 1: ...@@ -349,22 +350,38 @@ if len(sys.argv) > 1:
print '}' print '}'
"""), stdout=subprocess.PIPE, cwd="..").communicate()[0] """), stdout=subprocess.PIPE, cwd="..").communicate()[0]
if gv: if gv:
svg = subprocess.Popen(('neato', '-Tsvg'), body = subprocess.Popen(('neato', '-Tsvg'),
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
).communicate(gv)[0] ).communicate(gv)[0]
if not svg: if not body:
self.send_error(500) self.send_error(500)
return return
mt = 'text/html' if ext == 'svg':
body = """<html> mt = 'image/svg+xml'
<head><meta http-equiv="refresh" content="10"/></head> else:
<body><span style="position: absolute">%s</span> mt = 'text/html'
body = """<html>
<head><script>function refresh() {
var xhr = new XMLHttpRequest();
xhr.open('GET', '%s.svg', true);
xhr.addEventListener("load", function(e) {
if (e.target.status == 200)
document.getElementsByTagName("svg")[0].outerHTML =
e.target.responseXML.documentElement.outerHTML;
});
xhr.addEventListener("error", function(e) {
clearTimeout(refresh_timer);
});
xhr.send();
}</script></head>
<body onload="refresh_timer = setInterval(refresh, 10000)">
<span style="position: absolute">%s</span>
%s %s
</body> </body>
</html>""" % (' '.join(x if i == page else </html>""" % (name, ' '.join(x if i == page else
'<a href="%s.html">%s</a>' % (x, x) '<a href="%s.html">%s</a>' % (x, x)
for i, x in enumerate(self.pages)), for i, x in enumerate(self.pages)),
svg[svg.find('<svg'):]) body[body.find('<svg'):])
self.send_response(200) self.send_response(200)
self.send_header('Content-Length', len(body)) self.send_header('Content-Length', len(body))
self.send_header('Content-type', mt + '; charset=utf-8') self.send_header('Content-type', mt + '; charset=utf-8')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment