Skip to content

Instantly share code, notes, and snippets.

@dukenmarga
Last active January 26, 2024 10:07
Show Gist options
  • Save dukenmarga/6cb6bb8c650a5c77c488 to your computer and use it in GitHub Desktop.
Save dukenmarga/6cb6bb8c650a5c77c488 to your computer and use it in GitHub Desktop.
matplotlib on CherryPy
# 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