Deploying NumPy code in the Google cloud

Deploying GAE applications is pretty easy. For NumPy an extra configuration step is required, but that will take only minutes.

How to do it...

Let's create a new application.

  1. Create a new application.

    Create a new application with the launcher (File | New Application). Name it numpycloud. This will create a folder with the same name containing the following files:

    • app.yaml: YAML application configuration file
    • favicon.ico: Icon image
    • index.yaml: Auto generated file
    • main.py: Main entry point for the web application
  2. Add NumPy to the libraries.

    First, we need to let GAE know that we want to use NumPy. Add the following lines to the app.yaml configuration file in the libraries section:

    - name: NumPy
      version: "1.6.1"

    The configuration file should have the following contents:

    application: numpycloud
    version: 1
    runtime: python27
    api_version: 1
    threadsafe: yes
    
    handlers:
    - url: /favicon.ico
      static_files: favicon.ico
      upload: favicon.ico
    
    - url: .*
      script: main.app
    
    libraries:
    - name: webapp2
      version: "2.5.1"
    - name: numpy
      version: "1.6.1"
  3. Write NumPy code.

    To demonstrate that we can use NumPy code, let's modify the main.py file. There is a MainHandler class with a handler method for get requests. Replace this method with the following code:

    def get(self):
        self.response.out.write('Hello world!<br/>')
        self.response.out.write('NumPy sum = ' + str(numpy.arange(7).sum()))

We will have the following code in the end:

import webapp2
import numpy

class MainHandler(webapp2.RequestHandler):
    def get(self):
      self.response.out.write('Hello world!<br/>')
      self.response.out.write('NumPy sum = ' + str(numpy.arange(7).sum()))

app = webapp2.WSGIApplication([('/', MainHandler)],
                              debug=True)

If you click on the Browse button in the GAE launcher, you should see a web page in your default browser, with the following text:

Hello world!NumPy sum = 21

How it works...

GAE is free depending on how much of the resources are used. You can create up to ten web applications. GAE takes the sandboxing approach, which means that NumPy was not available for a while, but now it is, as demonstrated in this recipe.

You should also be aware that GAE currently does not support relational databases. There are other features too, which might make portability a concern.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset