python - 如何将 Polymer 项目引入 Google App Engine

标签 python google-app-engine polymer

我有 Google App Engine 应用程序,我想将 Polymer 组件引入其中。我认为最好的开始方式是带上 initial Project进入谷歌应用引擎 因此,我使用 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

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

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

Error: Server Error

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

我希望有人可以帮助我继续前进,因为我确实想使用其中的一些组件。

问候,

克里斯

最佳答案

对于初学者,您需要在 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

现在,您的问题似乎是您将 python27app.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 模块中)。

Learn more.

关于python - 如何将 Polymer 项目引入 Google App Engine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25415809/

相关文章:

python - 使用框架和网格的 tkinter gui 布局

用于加载数据的 Python psycopg2 copy_from() 会抛出空整数值 : DataError: invalid input syntax for integer: "" 的错误

python - 字符串由标点组成

AppEngine 上的 django 动态翻译

javascript - 如何为基于 polymer 的网页创建点击事件

python - 等待页面加载使用 Selenium 显式等待python

java - 将文件上传到 google appengine 的代码片段中生成错误

java - 预热请求到底是如何工作的?

polymer - 纸下拉菜单默认选择的选项不起作用

javascript - 将数据从 Firebase 检索到 Polymer 元素