简体   繁体   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

I am trying to pass some configuration variables to my main.py from app.yaml. 我试图从app.yaml传递一些配置变量到我的main.py. I haven't been able to locate the syntax for accessing app.yaml from the code. 我无法找到从代码中访问app.yaml的语法。

For example you want to have the user put their client number in app.yaml and access it from main.py to pass into main.html. 例如,您希望用户将其客户端号码放在app.yaml中,并从main.py访问它以传递到main.html。 While it would be easy to create a variable in main.py to pass it, it seems to be something that would be better put into app.yaml. 虽然在main.py中创建一个变量来传递它很容易,但它似乎更适合放入app.yaml。

Example: 例:

app.yaml 的app.yaml

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

main.py main.py

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

main.html main.html中

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

With the 1.6.5 release, App Engine support this[1]: 随着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.

Information on how to use this is available at: https://cloud.google.com/appengine/docs/python/config/appconfig#Python_app_yaml_Defining_environment_variables . 有关如何使用此信息的信息,请访问: https//cloud.google.com/appengine/docs/python/config/appconfig#Python_app_yaml_Defining_environment_variables

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

You can define variables in app.yaml to make them available to the program's os.environ dictionary: 您可以在app.yaml中定义变量,使其可用于程序的os.environ字典:

env_variables:
  variable_name: '<YOUR VALUE>'

When you need to use this variable within the main.py you can call it in this way: 当您需要在main.py中使用此变量时,可以通过以下方式调用它:

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

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

暂无
暂无

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

相关问题 问:谷歌应用引擎app.yaml如何处理main.py中的网址? - Q: google app engine app.yaml how to handle urls from within main.py? main.py或app.yaml是否确定此示例中App Engine cron任务使用的URL? - Does main.py or app.yaml determine the URL used by the App Engine cron task in this example? Google App Engine | Python |的app.yaml - Google App Engine | Python | APP.YAML 将 main.py 更改为 __init__.py 后,app.yaml 无法定位入口点 - app.yaml cannot locate entry point after changing main.py into __init__.py 在 Google App Engine 中部署 FLASK 应用程序时从 app.py 更改为 main.py - Change from app.py to main.py when deploying a FLASK app in Google App Engine Google应用程序引擎python cron:app.yaml文件中的testcron.py在做什么? - Google app engine python cron: What is the testcron.py doing in the app.yaml file? app.yaml文件:运行2个Python文件Google App Engine - app.yaml file: Running 2 Python Files Google App Engine 如何为Google App Engine应用程序写入`app.yaml`文件? - How to write `app.yaml` file for Google App Engine app? 如何在登录名中使用自定义身份验证:app.yaml中的required属性(Google App引擎,python) - How to use custom authentication with the login: required attribute in app.yaml ( Google app engine, python ) 如何使用App Engine app.yaml文件中定义的路由从Google Cloud Storage提供内容? - How to serve content from Google Cloud Storage with routes defined in App Engine app.yaml file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM