Passing a matplotlib figure to HTML (flask)

匿名 (未验证) 提交于 2019-12-03 01:12:01

问题:

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/') 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.

        <span class="pun" bdsfid="209">{{</span><span class="pln" bdsfid="210"> title </span><span class="pun" bdsfid="211">}}</span><span class="pln" bdsfid="212"> </span><span class="pun" bdsfid="213">-</span><span class="pln" bdsfid="214"> image</span>Image Placeholder

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)

回答1:

You have to separate the HTML and the image into two different routes.

Your /images/ route will just serve the page, and in the HTML content of that page there will be a reference to the second route, the one that serves the image.

The image is served in its own route from a memory file that you generate with savefig().

I obviously didn't test this, but I believe the following example will work as is or will get you pretty close to a working solution:

@app.route('/images/') def images(cropzonekey):     return render_template("images.html", title=cropzonekey)  @app.route('/fig/') def fig(cropzonekey):     fig = draw_polygons(cropzonekey)     img = StringIO()     fig.savefig(img)     img.seek(0)     return send_file(img, mimetype='image/png')

Your images.html template the becomes:

        <span class="pun" bdsfid="309">{{</span><span class="pln" bdsfid="310"> title </span><span class="pun" bdsfid="311">}}</span><span class="pln" bdsfid="312"> </span><span class="pun" bdsfid="313">-</span><span class="pln" bdsfid="314"> image</span>Image Placeholder


回答2:

I am working with Python 3.x, I have changed some lines of the code and it worked for me. I had the following error message: ".....object has no attribute 'savefig'"

@app.route('/fig/')  def fig(cropzonekey):     #fig = draw_polygons(cropzonekey)     fig = plt.plot([1,2,3,4], [1,2,3,4])     #img = StringIO()     img = BytesIO()     #fig.savefig(img)     plt.savefig(img)     img.seek(0)     return send_file(img, mimetype='image/png')


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!