如何将Polymer项目导入Google App Engine

4 投票
2 回答
3532 浏览
提问于 2025-04-18 17:59

我有一个Google App Engine的应用程序,想要把Polymer组件引入其中。我觉得最好的开始方式就是把初始项目放到Google App Engine上。

于是我使用Google App Engine Launcher创建了一个新的Google App Engine应用程序,然后在Google App Engine上创建了我的应用。

这个测试应用的URL是http://polymertvshelp.appspot.com/

接着,我把Polymer项目移动到我的文件夹里,并上传到Google App Engine。

应用程序运行正常,打开页面时显示了

你好,世界!

这段文字。

然后我找到了一篇帖子,告诉我一些后续步骤,但我似乎缺少了什么。帖子的链接

在帖子中,作者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秒后再试。

我希望有人能帮我,因为我真的想玩玩这些组件。

祝好,

Chris

2 个回答

2

首先,你需要在 url: .* 之前声明所有的 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

现在,你的问题似乎是你把 app.yaml 中的 python27 声明和 python25 应用的代码搞混了。对于旧版本,你的 handlers 声明应该是这样的:

- 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 中声明的那样(在你的情况下,它会在 main 模块中寻找 app 对象)。

了解更多信息。

2

Jamie Gomez,再次感谢你的帮助。 我成功让这个项目运行起来了,想分享一下我是怎么做到的,可能会对其他有同样兴趣的人有帮助。

  1. 我用Google App Engine创建了一个新项目,使用的是Python SDK。
  2. 我没有做任何修改就直接运行了这个应用。
  3. 我创建了一个名为'templates'的文件夹。
  4. 在这个新的'templates'文件夹里,我又创建了一个名为'components'的文件夹。
  5. 我把从Google Polymer项目网站下载的所有'polymer'的'Paper Elements'放进了这个'components'文件夹里(也就是'templates'文件夹里的那个)。
  6. 我在项目的根目录下又创建了一个'components'文件夹(和'templates'文件夹在同一层级)。 注意:这两个'components'文件夹里需要包含相同的内容。
  7. 在'templates'文件夹里,我放了一个'about_v2.html'文件,下面是代码。

<!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'文件设置成这样。

    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'文件设置成下面这样。

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. 然后我运行了这个应用,并点击链接进入我的polymer页面。

注意:当我把这个测试应用上传到appspot.com时,我发现polymer页面在我的电脑上运行正常,但在我的手机上却不行。

我猜… Appspot还没有完全支持Polymer。

撰写回答