google-app-engine
Serve html files and run python script in google app engine
I am trying to set up a website with many static html files. Currently I am serving the html files as static files with an app.yaml handler of - url: /(.*) static_files: www/\1 upload: www/(.*) secure: always What I need to do now is the ability to run some python code whenever someone is redirected to one of these html files. Is there some way to both serve html files as static files, and run a python script when it is called? Besides javascript, of course. My initial thought was to have a handler for html files that would run a script, and then that script would do the backend task it needs to do and then serve the html file as a template using webapp2 or jinja. My html files are stored a few directories down from my python file, in several sub-directories: -app.yaml -main.py -www -version1 --sub directories stored here Currently my test handler to try and get this to work looks like this: class testHandler(BaseHandler): def get(self, url): tplPath = self.request.url parseURL = urlparse(tplPath) path = "www" + dirname(parseURL.path) filename = basename(parseURL.path) return jinja2.Environment( loader=jinja2.FileSystemLoader(path) ).get_template(filename).render() This gets url or the html file that the requested, adds the www part of the path (side-note, I removed the www part from the served url to keep it consistent with an old website layout), and then tries to render the html file located in the path folder. When I try to run this, say by accessing https:// AppID.appspot.com/version1/product1/index.html, I get the error TemplateNotFound: index.html I checked the path that is being sent to the loader, and it is the correct path to the directory that contains the index.html file. Is there something obvious i'm missing here? Keep in mind my experience using jinja2 is very limited. Alternatively, is there any other way I could run a python script and still serve the html files? Any help is appreciated.
You are using a relative path to the templates, and Jinja2 needs an absolute path according to this post. So instead of path = "www" + dirname(parseURL.path), do this: # assuming that dirname is os.path.dirname: path = os.path.join(dirname(__file__),"www" + dirname(parseURL.path)) What this line does is it gets the directory name of __file__, and __file__ is the full, absolute path of the current *.py file that you are running. So, dirname(__file__) is your working directory. After replacing that one line, the rest of your code should work.
Related Links
Appengine's Indexing order, cursors, and aggregation
Works locally but throws java.lang.IncompatibleClassChangeError when deployed to GAE
Google App Engine Datastore: Turkish Characters won't sort properly
JPA + Google SQL + GWT + Eclipse
how to convert the string version of a key back into a form that I can use the get() function on to get the entity instance
Redirect To Specific URL in Google app engine
Exporting data to Google Spreadsheet from Google App Engine (Java version)
How is md5Hash calculated for com.google.appengine.api.blobstore.BlobInfo
Bad response to a BigQuery query: kind:discovery#restDescription instead of bigquery#queryResults
Google app engine jobs in datastore admin freeze
BadValueError('Property %s must be a float' % self.name) BadValueError: Property USD must be a float
HttpError 400 in jobs.get(jobId,ProjectId) even with right values
GAE - creating an app issue
Is NDB model _post_delete_hook called after transaction? OR Best way to clean a blob from the blobstore when its referencing entity is deleted
If verify Google user logged in
Moving HTML files to their own directory in Google App Engine (Using Jinja2 Templates) - Error 13