Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Plot a PNG using matplotlib in a web request, using Flask.
"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)
@sburns

This comment has been minimized.

Show comment Hide comment
@sburns

sburns Apr 3, 2014

exactly what I needed, thank you.

sburns commented Apr 3, 2014

exactly what I needed, thank you.

@codefever

This comment has been minimized.

Show comment Hide comment
@codefever

codefever Apr 16, 2015

i like it, thx!!!

i like it, thx!!!

@llazzaro

This comment has been minimized.

Show comment Hide comment
@llazzaro

llazzaro May 14, 2015

thanks!

thanks!

@mapfumo

This comment has been minimized.

Show comment Hide comment
@mapfumo

mapfumo Sep 6, 2015

Awesome. Thank you.

mapfumo commented Sep 6, 2015

Awesome. Thank you.

@smargs

This comment has been minimized.

Show comment Hide comment
@smargs

smargs Oct 29, 2015

Hi, thank you for the code. I ran it and got this msg '* Restarting with stat'. So i installed watchdog suggested in this post http://stackoverflow.com/questions/28241989/flask-app-restarting-with-stat and now I get this '* '*Restarting with windowsapi reloader'. And the localhost page doesn't work (keeps loading). Would you know how to make this work ? Thank you.

smargs commented Oct 29, 2015

Hi, thank you for the code. I ran it and got this msg '* Restarting with stat'. So i installed watchdog suggested in this post http://stackoverflow.com/questions/28241989/flask-app-restarting-with-stat and now I get this '* '*Restarting with windowsapi reloader'. And the localhost page doesn't work (keeps loading). Would you know how to make this work ? Thank you.

@anthonycarminati

This comment has been minimized.

Show comment Hide comment
@anthonycarminati

anthonycarminati Nov 19, 2015

Is there a way to embed this into a flask template? Right now i'm passing the response variable into an html template using the render_template function, but the image is not showing up; instead, only the response string is rendering. Thank you in advance!

Is there a way to embed this into a flask template? Right now i'm passing the response variable into an html template using the render_template function, but the image is not showing up; instead, only the response string is rendering. Thank you in advance!

@kwanwasutumkethom

This comment has been minimized.

Show comment Hide comment
@kwanwasutumkethom

kwanwasutumkethom Dec 14, 2016

thank you for alternative way to show plotted graph !

thank you for alternative way to show plotted graph !

@mariacamilaremolinagutierrez

This comment has been minimized.

Show comment Hide comment
@mariacamilaremolinagutierrez

mariacamilaremolinagutierrez May 30, 2017

Thanks a lot! This was super useful :)

Thanks a lot! This was super useful :)

@saurabhsingh13no

This comment has been minimized.

Show comment Hide comment
@saurabhsingh13no

saurabhsingh13no Aug 1, 2017

I am using python3.5 and ran the code with slight modification:

  1. Instead of import StringIO, I used import io
  2. Instead of output = StringIO.StringIO(), I used output = io.BytesIO()

Thanks

I am using python3.5 and ran the code with slight modification:

  1. Instead of import StringIO, I used import io
  2. Instead of output = StringIO.StringIO(), I used output = io.BytesIO()

Thanks

@YannickLeRoux

This comment has been minimized.

Show comment Hide comment
@YannickLeRoux

YannickLeRoux Jan 8, 2018

Great help, thanks

Great help, thanks

@ruhinshaikh

This comment has been minimized.

Show comment Hide comment
@ruhinshaikh

ruhinshaikh Feb 5, 2018

How to embed this response in the html page?

How to embed this response in the html page?

@nickkimer

This comment has been minimized.

Show comment Hide comment
@nickkimer

nickkimer Apr 2, 2018

is there a way to embed this response into html template? I have been searching everywhere but have not been able to find a workaround

is there a way to embed this response into html template? I have been searching everywhere but have not been able to find a workaround

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment