如何将Polymer项目引入Google App Engine

时间:2014-08-20 22:56:10

标签: python google-app-engine polymer

我有Google App Engine应用程序,我想将Polymer组件带入。我认为最好的方法是将initial Project引入Google App Engine 所以我使用Google App Engine Launcher创建了我的新Google App Engine应用程序。然后我在Google App Engine上创建了我的应用程序。

此测试应用的网址为http://polymertvshelp.appspot.com/

然后我将Polymer项目移动到我的文件夹中并将其上传到Google App Engine

应用程序在显示

的页面上着陆
  

Hello world!

文本。

然后我发现一篇帖子告诉我接下来的一些步骤,但我遗漏了一些东西。邮政URL

在Mike的帖子中,作者通过删除以下

给了我修改的main.py的代码
import webapp2

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

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

然后我将Mike的代码粘贴到文件

 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 ()

这是现在在这个main.py文件中执行的唯一代码

我还修改了app.yaml文件,使其看起来像

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

我还通过删除相对路径前面的..修改了step-2文件夹中的index.html。

当我运行应用程序时,我现在得到了 500服务器错误

  

错误:服务器错误

     

服务器遇到错误,无法完成您的请求。   请在30秒后再试一次。

我希望有人能帮助我,因为我肯定会想要使用其中一些组件。

此致

克里斯

2 个答案:

答案 0 :(得分:2)

对于初学者,您需要在 url: .*之前声明所有网址处理程序,因为这样会抓住所有网址处理程序,因此您的app.yaml应如下所示:

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

现在,您的问题似乎是您正在将 python27 app.yaml 声明与 python25 应用的代码混合在一起。对于旧版本,您的处理程序声明将如下所示:

- url: .*
  script: main.py

注意声明的脚本是如何实际的 python文件,它将由服务器执行。

现在,在框架的最新版本和推荐版本中,您的应用程序代码应如下所示:

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)

根据 app.yaml 声明,注意如何创建应用程序并让服务器选择它(在您的情况下,它会查找app对象中的main对象{1}}模块)。

Learn more.

答案 1 :(得分:2)

杰米戈麦斯再次感谢你的帮助。 我能够完成这项工作并希望分享我是如何做到的,以防万一其他人可能有同样的兴趣。

  1. 我使用Google App Engine创建了一个新项目 - python sdk
  2. 我没有更改就运行了应用程序
  3. 我制作了'模板'文件夹
  4. 我在新的'templates'文件夹中创建了'components'文件夹
  5. 我把从Google聚合物项目网站下载的所有'聚合物''得到纸元素'('components'文件夹中的所有内容)都放在那里
  6. 我在项目的根目录创建了另一个'components文件夹(与我''templates'文件夹相同的级别) 注意:两个“组件”文件夹都需要包含相同的项目
  7. 在templates文件夹中我放置了一个'about_v2.html',代码发布在
  8. 下面

    <!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. 我的'app.yaml'文件看起来像
    2.     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. 我将'main.py'看作如下
      2. 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. 然后我运行了应用程序并点击链接进入我的聚合物页面
        2. 注意:当我将此测试应用程序加载到appspot.com时,我注意到聚合物页面在我的计算机上工作正常但不适用于我的移动设备。

          我的猜测...... Appspot还没有为Polymer做好准备。