Using plotly from Google App engine

452 views Asked by At

I am trying to use plotly from a Google App engine app. The standalone python program works but when I try to incorporate it in my google app engine app, I get import errors for sqlite and plotly both of which are needed for my project. How can I get GA Engine to recognize these imports?

The code boiled down to the simplest is this:

form="""
<form action="/sqlhandler">
    <input name="q">
    <input type="Submit">
</form>
"""
class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/html'
        self.response.write(form)

class SQLHandler(webapp2.RequestHandler):
    def get(self):
        q = self.request.get("q")
        import sql_queries
        url = sql_queries.plot_graph(q)
        self.response.headers['Content-Type'] = 'text/html'
        self.response.write(url)

    app = webapp2.WSGIApplication([
        ('/', MainPage),
        ('/sqlhandler', SQLHandler)
       ], debug=True)
2

There are 2 answers

2
Tim On

How can I get GA Engine to recognize these imports?

Neither sqlite3 nor plotly are available on GAE by default (see https://cloud.google.com/appengine/docs/python/tools/libraries27).

In order to use them, you have to include the source files of these libraries in your GAE application and upload them with the rest of the project.

0
user2399453 On

This is technically a hack but what I did to workaround my problem was to run my plotly graph producing code in a background service that listens to requests on a socket, manipulates downloaded data, creates a plotly graph and returns the plotly URL back so that it can be embedded in the html page to be displayed. Its slight overhead but the socket communication is on the same local machine so hopefully this is not so bad. This way I don't have to install libraries, not sure what other problems I will run into down the road.