Last active
January 26, 2024 10:07
-
-
Save dukenmarga/6cb6bb8c650a5c77c488 to your computer and use it in GitHub Desktop.
matplotlib on CherryPy
This file contains 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
# Modified from https://gist.github.com/tkf/1299670 by @tkf | |
import matplotlib | |
matplotlib.use('Agg') | |
from matplotlib import pyplot | |
import numpy | |
import cherrypy | |
from cStringIO import StringIO | |
class HelloWorld: | |
@cherrypy.expose | |
def index(self): | |
return ''' <img src="image.png" width="640" height="480" border="0" /> ''' | |
@cherrypy.expose | |
def image_png(self): | |
img = StringIO() | |
self.plot(img) | |
img.seek(0) | |
return cherrypy.lib.static.serve_fileobj(img, | |
content_type="png", | |
name="image.png") | |
def plot(self, image): | |
x = numpy.linspace(0, 10) | |
y = numpy.sin(x) | |
pyplot.plot(x, y) | |
pyplot.savefig(image, format='png') | |
if __name__ == '__main__': | |
cherrypy.quickstart(HelloWorld()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment