Using matplotlib in GAE

后端 未结 2 1756
名媛妹妹
名媛妹妹 2021-02-04 11:28

My tags and title quite clearly state my problem. I want to use matplotlib to create real-time plots in Google App Engine. I\'ve read the documentation and searched on SO and Go

相关标签:
2条回答
  • 2021-02-04 12:21

    I'm not familiar with sys module. To give an answer to the question I prefer using webapp2. This is a working handler:

    import webapp2
    import StringIO
    import numpy as np
    import matplotlib.pyplot as plt
    
    
    class MainPage(webapp2.RequestHandler):
        def get(self):
            plt.plot(np.random.random((20)))
            sio = StringIO.StringIO()
            plt.savefig(sio, format="png")
            img_b64 = sio.getvalue().encode("base64").strip()
            plt.clf()
            sio.close()
            self.response.write("""<html><body>""")
            self.response.write("<img src='data:image/png;base64,%s'/>" % img_b64)
            self.response.write("""</body> </html>""")
    
    app = webapp2.WSGIApplication([('/', MainPage)], debug=True)
    

    Alternatively, you could write the sio.getvalue() in the blobstore with files api and use the method get_serving_url() of images api for avoid to encode in base64.

    0 讨论(0)
  • 2021-02-04 12:29

    The problem was that you were setting the MATPLOTLIBDATA and MPLCONFIGDIR environment variables to your app directory before importing matplotlib. Since you didn't have any fonts in your app directory, it couldn't load any fonts.

    0 讨论(0)
提交回复
热议问题