繁体   English   中英

如何使用Google App Engine Python将变量从app.yaml传递到main.py.

[英]How to pass a variable from app.yaml to main.py with Google App Engine Python

我试图从app.yaml传递一些配置变量到我的main.py. 我无法找到从代码中访问app.yaml的语法。

例如,您希望用户将其客户端号码放在app.yaml中,并从main.py访问它以传递到main.html。 虽然在main.py中创建一个变量来传递它很容易,但它似乎更适合放入app.yaml。

例:

的app.yaml

    application: xyz
    version: 1
    runtime: python27
    ...
    clientID: (ID here)

main.py

    myID = appYAML.clientID
    ...
    values = {'xyz': blah.blah, 'myID': myID }

main.html中

    ...
    <script>
      ...
      {% ifequal myID %}
        my_client = {{myID}}
      ...
    </script>

随着1.6.5版本的发布,App Engine支持这个[1]:

- In your app.yaml file, you can include an env_variables stanza that will set
  the given environment variables in your application's runtime.

有关如何使用此信息的信息,请访问: https//cloud.google.com/appengine/docs/python/config/appconfig#Python_app_yaml_Defining_environment_variables

这不受支持,您应该将特定于应用程序的设置放入您自己的YAML文件中。

您可以在app.yaml中定义变量,使其可用于程序的os.environ字典:

env_variables:
  variable_name: '<YOUR VALUE>'

当您需要在main.py中使用此变量时,可以通过以下方式调用它:

import os
CUSTOM_SETTINGS = os.environ['variable_name']

文档: https//developers.google.com/appengine/docs/python/config/appconfig#Python_app_yaml_Defining_environment_variables

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM