[英]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.