Skip to content

Instantly share code, notes, and snippets.

@dukenmarga
Forked from tkf/mplonflask.py
Created August 19, 2014 04:34
Show Gist options
  • Save dukenmarga/1e236421d8804b7b99b3 to your computer and use it in GitHub Desktop.
Save dukenmarga/1e236421d8804b7b99b3 to your computer and use it in GitHub Desktop.
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