Reputation: 40593
In Google App Engine, I have the following code which shows a simple HTML page.
import os
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
class IndexHandler(webapp.RequestHandler):
def get(self):
template_values = { }
path = os.path.join(os.path.dirname(__file__), '../templates/index.html')
self.response.out.write(template.render(path, template_values))
The issue is that the page isn't always rendered. The index.html is a simple "Hello World!". After a couple of page refresh the page is displayed properly (i.e. the index.html file is found...). I tried to call flush at the end, but it didn't help. I am able to repro this with the SDK and on their server.
Am I missing something? Does someone have an idea of what is going on?
Thanks
Upvotes: 1
Views: 79
Reputation: 101149
Your handler script (the one referenced by app.yaml) has a main() function, but needs this stanza at the end:
if __name__ == '__main__':
main()
What's happening is that the first time your script is run in a given interpreter, it interprets your main script, which does nothing (thus returning a blank response). On subsequent invocations, the interpreter simply executes your main() (a documented optimization), which generates the page as expected. Adding the stanza above will cause it to execute main on initial import, too.
Upvotes: 3
Reputation: 882093
Can't reproduce -- with directory changed to ./templates
(don't have a ../templates
in my setup), and the usual main
function added, and this script assigned in app.yaml
to some arbitrary URL, it serves successfully "Hello World" every time. Guess we need more info to help -- log entries (maybe add logging.info
calls here?), app.yaml
, where's main
, etc, etc...
Upvotes: 0