0

I have installed the Google App Engine; the GAE launcher GUI; and the Python SDK.

I have successfully deployed a webpage using the GUI, and my test page is now viewable at the external URL.

Unfortunately, the only way I have known to do this, is to include all my HTML code in the Python .py file within the GAE directory. What I WANT to do, is to include code in my .py file that calls a separate .html file. So far despite my best search efforts I can't understand how to write this. Can anyone help please?

Thanks in advance

4
  • 2
    Did you read the documentation? It shows you exactly how to use Jinja2 templates with Python. Commented Jan 11, 2014 at 16:39
  • The GAE documentation is full of examples on how to use templating and assets, did you read through those yet? Commented Jan 11, 2014 at 16:56
  • Hi Daniel. Thanks for replying. I have followed the instructions of the help pages to include Jinja2 (ie adding reference to the .yaml file). However when I do this, and then try to run my web app in the GAE GUI, it fails to do so. IIeinstead of a little green circle appearing, a "!" In a warning triangle is displayed. Afterwards I can't get the web app to ru , or the browser to load the page when accessing localhost:8080 (the port allocated by the Google GUI). Commented Jan 11, 2014 at 19:08
  • Can you show your yaml file with jinja2 templates? Commented Jan 11, 2014 at 21:00

1 Answer 1

1

First off, install Jinja2 using pip in a terminal (or command line):

pip install jinja2

Secondly, include it in your app.yaml file as a library.

libraries:
- name: webapp2
  version: "2.5.2"
- name: jinja2
  version: "2.6"

Declare a Jinja environment in your code so that you can render templates:

import jinja2

env = jinja2.Environment(
    loader=jinja2.FileSystemLoader('templates')
)

Now you need to create a template .html file in a 'templates' folder. If we create an "index.html" template that we want to render in code we would write:

template = env.get_template('index.html')
self.response.write(template.render())

There is obviously a lot more you can do with templates than simply render html - you can have a look at features on Jinjas website.

The process for doing all of this is better explained here in the python tutorial for google app engine.

If you need an example, I have recently open sourced a google app engine application I developed on github and you can take a look at it for some guidance if you wish.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.