Passing a matplotlib figure to HTML (flask)

前端 未结 5 1644
遥遥无期
遥遥无期 2020-11-29 20:41

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 a

5条回答
  •  执念已碎
    2020-11-29 21:15

    from flask import Flask, send_file
    from io import StringIO
    import matplotlib.pyplot as plt
    from StringIO import StringIO
    @app.route('/fig/')
    def fig():
          plt.plot([1,2,3,4], [1,2,3,4])
          img = StringIO()
          plt.savefig(img)
          img.seek(0)
          return send_file(img, mimetype='image/png')
    

    The other answers are correct ,I just wanted to show the header files that has to be included. This program creates a simple graph and sends it over to the html page.

提交回复
热议问题