如何将Polymer项目引入Google App Engine?

4
我有一个使用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。
该应用程序可以正常运行,显示页面上的“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秒后重试。

我希望有人能帮助我,因为我真的很想尝试一些这些组件。
问候,
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中声明的python27python25应用程序的代码混合在一起。对于旧版本,你的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 对象)。

了解更多。


Jamime,谢谢你的信息。我现在正在处理导入webapp2代码的问题。你发送的其余部分真的很快就理解了。我确实不得不标记threadsafe = no。希望很快能够回来并带来成功的消息。问候,Chris - Chris S
我在这方面运气不佳,所以我将提出另一个问题,目的是简化事情。我希望那个问题的答案最终能帮助我在这里取得成功。 - Chris S

2

再次感谢Jamie Gomez的帮助。

我已经成功实现了它,并想分享一下我是如何做到的,以防其他人也有相同的兴趣。

  1. 我使用Google App Engine - python sdk创建了一个新项目
  2. 我没有进行任何更改就运行了应用程序
  3. 我创建了一个“templates”文件夹
  4. 我在这个新的“templates”文件夹中创建了一个“components”文件夹
  5. 我将从Google polymer项目网站下载的所有“polymer”“get Paper Elements”(即“components”文件夹中的所有内容)放在那里
  6. 我在我的项目根目录(与我的“templates”文件夹处于同一级别)中创建了另一个“components”文件夹 注意:两个“components”文件夹都需要包含相同的项目
  7. 在模板文件夹中,我放置了一个“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>

我把我的'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

我把'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. 然后我运行了应用程序,并单击链接以进入我的聚合物页面。

注意:当我将此测试应用程序加载到appspot.com时,我注意到聚合物页面在我的计算机上可以正常工作,但在我的移动设备上无法正常工作。

我猜测... Appspot 还没有准备好支持 Polymer。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接