Skip to content

Instantly share code, notes, and snippets.

@tkf
Created October 19, 2011 21:04
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save tkf/1299670 to your computer and use it in GitHub Desktop.
Save tkf/1299670 to your computer and use it in GitHub Desktop.
matplotlib on Flask
import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot
import numpy
from flask import Flask, send_file
from cStringIO import StringIO
app = Flask(__name__)
def plot(image):
x = numpy.linspace(0, 10)
y = numpy.sin(x)
pyplot.plot(x, y)
pyplot.savefig(image, format='png')
@app.route('/image.png')
def image_png():
image = StringIO()
plot(image)
image.seek(0)
return send_file(image,
attachment_filename="image.png",
as_attachment=True)
@app.route('/')
def index():
return '<img src="image.png">'
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment