Using matplotlib in GAE

后端 未结 2 1758
名媛妹妹
名媛妹妹 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("""""")
            self.response.write("" % img_b64)
            self.response.write(""" """)
    
    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.

提交回复
热议问题