I am using Jinja2 templates for my GAE Python application. Actually there are a couple of small applications inside one project. They are, for example, blog and site. So, the first one is for blog and the second one is for site =). I have this folders structure:
/
/apps
/blog
/site
/templates
/blog
/site
I also have a code for accessing templates folder for each application. It looks like this:
template_dirs = []
template_dirs.append(os.path.join(os.path.dirname(__file__), 'templates/project'))
Of course, it doesn't work as it's wrong. It returns a string like this: base/data/home/apps/myapplication/1.348460209502075158/apps/project/templates/project
And I need it to return a string like this: base/data/home/apps/myapplication/1.348460209502075158/apps/templates/project How can I do that using absolute paths, not relative? I suppose I need to get the root of the my whole GAE project some way. Thanks!
To simply access the app's root path, use the module as though it were a string: var appRoot = require('app-root-path'); var myModule = require(appRoot + '/lib/my-module. js'); Side note: the module actually returns an object, but that object implements the toString method, so you can use it as though it were a string.
The application root directory is the physical directory on your disk the IIS uses as the starting-point for your applications on the web. You create the application root directory (usually called "ISAPI") in the GAS installation directory path, for example, $FGLASDIR\ISAPI.
The app. yaml file defines your configuration settings for your runtime as well as general app, network, and other resource settings.
The only way you can delete the default version of your App Engine app is by deleting your project. However, you can stop the default version in the GCP Console. This action shuts down all instances associated with the version. You can restart these instances later if needed.
The easiest way to get the root path of your app is to put a module in the root of your app, which stores the result of os.path.dirname(__file__)
, then import that where needed. Alternately, call os.path.dirname(module.__file__)
on a module that's in the root of your app.
Why not put an os.path.abspath around file
template_dirs.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates/project'))
It's kind of a kludge, and I didn't write this with the love and caring I write most of my code with, but maybe this will be useful for you...
import os
def app_root():
"""Get the path to the application's root directory."""
app_id = os.environ['APPLICATION_ID']
path = os.environ['PATH_TRANSLATED']
path_parts = path.split(app_id, 1)
root_path = path_parts[0] + app_id
# If this is ran on Google's servers, there is an extra dir
# that needs to be traversed to get to the root
if not os.environ['SERVER_SOFTWARE'].startswith('Dev'):
root_path += '/' + path_parts[1].lstrip('/').split('/', 1)[0]
return root_path
Please note that for this to work on the SDK your application's root directory MUST be named the same as your application's ID.
Additionally, this makes assumptions about the directory structure used by Google on the production servers, so it's entirely possible that they could change something and break this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With