I am using matplotlib to render some figure in a web app. I've used fig.savefig()
before when I'm just running scripts. However, I need a function to return an actual ".png" image so that I can call it with my HTML.
Some more (possibly unnecessary) info: I am using Python Flask. I figure I could use fig.savefig()
and just stick the figure in my static folder and then call it from my HTML, but I'd rather not do that every time. It would be optimal if I could just create the figure, make an image out of it, return that image, and call it from my HTML, then it goes away.
The code that creates the figure works. However, it returns a figure, which doesn't work with HTML I guess.
Here's where I call the draw_polygon
in the routing, draw_polygon
is the method that returns the figure:
@app.route('/images/<cropzonekey>')
def images(cropzonekey):
fig = draw_polygons(cropzonekey)
return render_template("images.html", title=cropzonekey, figure = fig)
And here is the HTML where I am trying to generate the image.
<html>
<head>
<title>{{ title }} - image</title>
</head>
<body>
<img src={{ figure }} alt="Image Placeholder" height="100">
</body>
</html>
And, as you can probably guess, when I load the page, all I get is Image Placeholder
. So, they didn't like the format I fed the figure in with.
Anyone know what matplotlib methods/work-arounds turn a figure into an actual image? I am all over these docs but I can't find anything. Thanks!
BTW: didn't think it was necessary to include the python code that makes the figure, but I can include it if You guys need to see it (just didn't want to clutter the question)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…