4
votes

I have Google App Engine application that I would like to bring Polymer components into. I thought the best way to start off would be to bring the initial Project into Google App Engine So I created my new Google App Engine Application using Google App Engine Launcher. I then created my application on Google App Engine.

the URL for this test application is http://polymertvshelp.appspot.com/

So I then moved the Polymer project into my folder and uploaded it into Google App Engine

The application works landing on the page that displays the

Hello world!

text.

So then I find a post that tells me some next steps but I am missing something. the Post URL

In the post Mike the author gave me the code for the main.py which I modified by deleting the following

import webapp2

class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write('Hello world!')

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

I then pasted Mike's code into the file

 import random
import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template

class MainHandler(webapp.RequestHandler):
  def get (self, q):
    if q is None:
        i = random.randint(1,11)
        q = 'step-2/index.html'
    path = os.path.join (os.path.dirname (__file__), q)
    self.response.headers ['Content-Type'] = 'text/html'
    self.response.out.write (template.render (path, {}))

class GuideHandler(webapp.RequestHandler):
  def get (self, q):
    q = 'icgt-registration-guide.pdf'
    path = os.path.join (os.path.dirname (__file__), q)
    self.response.headers ['Content-Type'] = 'application/pdf'
    self.response.out.write (template.render (path, {}))

def main ():
  application = webapp.WSGIApplication ([('/(.*html)?', MainHandler)], debug=True)
  util.run_wsgi_app (application)

if __name__ == '__main__':
  main ()

this is the only code that executes in this main.py file now

I also modified the app.yaml file so that it looks like

application: polymerxxxx
version: 2
runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: /favicon\.ico
  static_files: favicon.ico
  upload: favicon\.ico

- url: .*
  script: main.app

- url: /components
  static_dir: components
- url: /images
  static_dir: images

I also modified the index.html in the step-2 folder by removing the .. in front of the relative paths.

When I run the application I now get 500 server error

Error: Server Error

The server encountered an error and could not complete your request. Please try again in 30 seconds.

I'm hoping someone can get me going because I sure would like to play with some of these components.

Regards,

Chris

2

2 Answers

2
votes

For starters, you need to declare all your URL handlers before url: .*, because that will catch all, so your app.yaml should look like this:

application: polymerxxxx
version: 2
runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: /favicon\.ico
  static_files: favicon.ico
  upload: favicon\.ico
- url: /components
  static_dir: components
- url: /images
  static_dir: images
- url: .*
  script: main.app

Now, your problem seems to be that you're mixing the app.yaml declaration of python27 with the code of a python25 app. For the older version, your handlers declaration would look like this:

- url: .*
  script: main.py

Notice how the declared script is the actual python file, which would be executed by the server.

Now, in the newest and recommended version of the framework, your application code should look something like this:

import webapp2

class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write('Hello, World!')

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

Notice how you just create the application and let the server pick it up, as per the app.yaml declaration (in your case, it looks for the app object in the main module).

Learn more.

2
votes

Jamie Gomez again thank-you for your help. I was able to make this work and wanted to share how I did it just in case someone else might have the same interest.

  1. I created a new project with Google App Engine - python sdk
  2. I ran the application with no changes
  3. I made a 'templates' folder
  4. I made a 'components' folder inside of that new 'templates' folder
  5. I put all of my 'polymer' 'get Paper Elements' downloaded from the Google polymer project website (everything in the 'components' folder) goes there
  6. I created another 'components folder at the root of my project (same level as my 'templates' folder) Note: Both 'components' folders need to contain the same items
  7. in the templates folder I place an 'about_v2.html' the code is posted below

<!DOCTYPE html>
<html>
  <head>
    <title>About V2</title>
  </head>
  <body>
 
<H1>About Our Application</H1>
<br /><br />
<p><a href="components/paper-radio-group/demo.html" target="_blank">Paper Radio Group Demo</a></p>
<hr>
<H3>Links</H3>
    <a href="/">Home</a><br/>
  </body>
</html>
  1. I make my 'app.yaml' file look like

    application: hellopolymer
    version: 2
    runtime: python27
    api_version: 1
    threadsafe: yes

    handlers:
    - url: /favicon\.ico
      static_files: favicon.ico
      upload: favicon\.ico
    - url: /components
      static_dir: components
    - url: .*
      script: main.app

    libraries:
    - name: webapp2
      version: latest
    - name: jinja2
      version: latest
  1. I made the 'main.py' look like the following

import os
import webapp2
import jinja2

env = jinja2.Environment(
  loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates')))

class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write('Hello world! <a href="/about_v2.html">About</a>.<br />')
       
class AboutPage_v2(webapp2.RequestHandler):
    def get(self):
        template_values = {
            
        }

        template = env.get_template('about_v2.html')
        self.response.out.write(template.render(template_values))


app = webapp2.WSGIApplication([
    ('/', MainHandler),
    ('/about_v2.html', AboutPage_v2)],

                              debug=True)
  1. I then ran the application and clicked on the links to get to my polymer page

Note: When I loaded this test app to the appspot.com I noticed that the polymer page works fine on my computer but doesn't work for my mobile device.

My guess... Appspot is not yet ready for Polymer.