Created
August 30, 2013 16:44
-
-
Save maedoc/6391880 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cherrypy | |
import StringIO | |
js = """ | |
function image_adder(xhr, out) { | |
return function() { | |
if (xhr.readyState == 4 && xhr.status == 200) { | |
var img = document.createElement('img') | |
img.src = 'data:image/png;base64,' + xhr.responseText; | |
out.insertBefore(img, out.children[0]); | |
} | |
} | |
} | |
function request_plot() { | |
var url = "/plot?cmd=" + document.getElementById('cmd').value | |
, out = document.getElementById('output') | |
, xhr = new XMLHttpRequest(); | |
xhr.onreadystatechange = image_adder(xhr, out); | |
xhr.open("GET", url, true); | |
xhr.send(); | |
} | |
""" | |
ui = """ | |
<html> | |
<script>{js}</script> | |
<body> | |
<form method="get" action="javascript:request_plot()"> | |
<input id="cmd" type="text" name="cmd"> | |
</form> | |
<div id="output"> | |
<div><h6>output goes here</h6></div> | |
</div> | |
</body> | |
</html> | |
""".format(js=js) | |
code_templ = """ | |
from pylab import * | |
from numpy import * | |
fig = figure() | |
{} | |
fig.savefig(fd, format='png') | |
""" | |
class plotter(object): | |
@cherrypy.expose | |
def index(self): | |
return ui | |
@cherrypy.expose | |
def plot(self, cmd=None): | |
fd = StringIO.StringIO() | |
exec code_templ.format(cmd) in {'fd': fd} | |
return fd.getvalue().encode('base64') | |
cherrypy.config.update({'server.socket_port': 8081}) | |
cherrypy.quickstart(plotter()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment