Mark Finch
Mark Finch

Reputation: 766

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. I haven't been able to locate the syntax for accessing app.yaml from the code.

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. 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.

Example:

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>

Upvotes: 5

Views: 2803

Answers (3)

coto
coto

Reputation: 2325

You can define variables in app.yaml to make them available to the program's os.environ dictionary:

env_variables:
  variable_name: '<YOUR VALUE>'

When you need to use this variable within the main.py you can call it in this way:

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

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

Upvotes: 3

schuppe
schuppe

Reputation: 2033

With the 1.6.5 release, App Engine support this[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.

Upvotes: 10

Adam Crossland
Adam Crossland

Reputation: 14213

That's not supported, and you should put your application-specific settings into your own YAML file.

Upvotes: 5

Related Questions