Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Plot a PNG using matplotlib in a web request, using Flask.

View app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
"Plot a PNG using matplotlib in a web request, using Flask."
 
# Install dependencies, preferably in a virtualenv:
#
# pip install flask matplotlib
#
# Run the development server:
#
# python app.py
#
# Go to http://localhost:5000/plot.png and see a plot of random data.
 
import random
import StringIO
 
from flask import Flask, make_response
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
 
 
app = Flask(__name__)
 
 
@app.route('/plot.png')
def plot():
fig = Figure()
axis = fig.add_subplot(1, 1, 1)
 
xs = range(100)
ys = [random.randint(1, 50) for x in xs]
 
axis.plot(xs, ys)
canvas = FigureCanvas(fig)
output = StringIO.StringIO()
canvas.print_png(output)
response = make_response(output.getvalue())
response.mimetype = 'image/png'
return response
 
 
if __name__ == '__main__':
app.run(debug=True)

exactly what I needed, thank you.

i like it, thx!!!

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.