I have been finding a lot of information about how to write Jinja code and have 2 .py files page1.py
and page2.py
which reference .jinja
files in the templates
directory. When I run python page1.py
I accurately get the webpage I want. In fact, when I run python page1.py > page1.html
and then navigate to myWebpage/page1.html
, it works perfectly.
I'm wondering, however, if jinja is supposed to (or can) run that automatically as soon as I enter myWebpage/page1.html
into my browser. The thing is, I am using Jinja to automatically update the page as soon as parameters on the backend change. Do I need to run this python code using cron
or is it supposed to generate the new .html
code as soon as a user refreshes the webpage?
Below are the contents of my python and jinja pages:
(1) contents of /var/www/html/page1.py
#!/usr/bin/env python
### taken from http://kagerato.net/articles/software/libraries/jinja-quickstart.html
# Load the jinja library's namespace into the current module.
import jinja2
# In this case, we will load templates off the filesystem.
# This means we must construct a FileSystemLoader object.
#
# The search path can be used to make finding templates by
# relative paths much easier. In this case, we are using
# absolute paths and thus set it to the filesystem root.
templateLoader = jinja2.FileSystemLoader( searchpath="/usage_statistics/webfront" )
# An environment provides the data necessary to read and
# parse our templates. We pass in the loader object here.
templateEnv = jinja2.Environment( loader=templateLoader )
# This constant string specifies the template file we will use.
TEMPLATE_FILE = "templates/page1.jinja"
# Read the template file using the environment object.
# This also constructs our Template object.
template = templateEnv.get_template( TEMPLATE_FILE )
# Specify any input variables to the template as a dictionary.
templateVars = { "title" : "Test Example",
"description" : "A simple inquiry of function." }
# Finally, process the template to produce our final text.
outputText = template.render( templateVars )
print outputText
(2) contents of /var/www/html/templates/page1.jinja
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>{{ title }}</title>
<meta name="description" content="{{ description }}" />
</head>
<body>
<div id="content">
<p>Why, hello there!</p>
</div>
</body>
</html>
(3) contents of /var/www/html/page2.py
#!/usr/bin/env python
import jinja2
templateLoader = jinja2.FileSystemLoader( searchpath="/usage_statistics/webfront" )
env = jinja2.Environment( loader=templateLoader )
TEMPLATE_FILE = "templates/page2.jinja"
template = env.get_template( TEMPLATE_FILE )
# Here we add a new input variable containing a list.
# Its contents will be expanded in the HTML as a unordered list.
FAVORITES = [ "chocolates", "lunar eclipses", "rabbits" ]
templateVars = { "title" : "Test Example",
"description" : "A simple inquiry of function.",
"favorites" : FAVORITES
}
#outputText = template.render( templateVars )
#template.render( templateVars )
print template.render( templateVars )
(4) contents of page2.jinja
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>{{ title }}</title>
<meta name="description" content="{{ description }}" />
</head>
<body>
<div id="content">
<p>Greetings visitor! These are a list of my favorite things:</p>
<ul>
{% for item in favorites %}
<li>{{ item }}</li>
{% endfor %}
</ul>
</div>
</body>
</html>