Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

need to package jinja2 template for python

(UPDATE: I've made a better question with a better answer here. I was going to delete this question, but some of the answers might prove useful to future searchers.)

My question is just about identical to this, but that answer is ugly (requires a dir structure including sharedtemplates/templates/templates/), incomplete as posted (user "answered" his own question), and assumes some knowledge I don't have.

I'm working on my first python-backed web application. The javascript component is well under development using a static HTML page. Now I want a server-side python component to handle AJAX calls and render an HTML template using jinja2.

I've had success with python before, creating GUI apps using tkinter/requests. Love the language, but the python environment (environments?) is confusing. I'm not working in a virtualenv.

According to jinja2 docs, HTML templates have to be in something called a package. Then you create an Environment with a PackageLoader that knows the name of the package and the template dir:

from jinja2 import Environment, PackageLoader
env = Environment(loader=PackageLoader('yourapplication', 'templates'))

So, here's my index.py (it's just a stub and doesn't even try to render anything, but you can at least tell if it crashes).

#!/usr/bin/python
from jinja2 import Environment, PackageLoader # no prob, jinja2 correctly installed using pip

env = Environment(loader=PackageLoader('mypkg', 'template')) # causes server error

# if it doesn't crash, just put up a basic html page for now
print ("Content-type: text/html\r\n\r\n")
print("<html><head><title>hello</title></head><body>hello wuld</body></html>")

Here's the directory structure:

index.py
mypkg/
mypkg/template/index.html
mypkg/__init__.py  # empty

Relevant line from error log:

ImportError: No module named mypkg

Maybe I need to structure this differently, and I'm pretty sure I'll need to create and invoke a setup.py to install the module. That's part of what the other answer left out: what's in setup.py and how does it work in this case? I've looked at dozens of resources on setup.py and none of them seems to pertain to the question of installing HTML templates.

Thanks in advance for your help!

UPDATE: fragilewindows pointed to a resource that tells about "developer mode", which is probably part of the answer. The difficulty here is, I'm looking to package this template for local deployment, not for distribution. But 99% of the online documentation is about packaging projects for PyPi. I don't need to package a "project", just a dinky HTML template. Indeed, the only reason I need to package the template is because that's the default way for jinja2 to access templates (and I do want to go native in python).

I just need to convince the environment that "mypkg" is installed, and that "template" is a directory within the install. You can see that my efforts so far are naive; I expect the right answer will be correspondingly lightweight.

like image 749
Juan Tomas Avatar asked Jan 06 '23 15:01

Juan Tomas


2 Answers

I've discovered a WORKAROUND, here. In this usage, the template is not part of a module or package; it is loaded directly from the file system. File system:

./index.py
./template.html

index.py:

#!/usr/bin/python
import jinja2

templateLoader = jinja2.FileSystemLoader( searchpath="." )
templateEnv = jinja2.Environment( loader=templateLoader )
TEMPLATE_FILE = "template.html"
template = templateEnv.get_template( TEMPLATE_FILE )
outputText = template.render( ) # this is where to put args to the template renderer

print ("Content-type: text/html\r\n\r\n")
print(outputText)
like image 88
Juan Tomas Avatar answered Jan 08 '23 04:01

Juan Tomas


I don't know the process involved with packaging but I figure since Jinja2 is written in Python, the process would be the same as packaging any other application in Python.

Here are a few links that may be useful to you:

  • The Hitchhiker's Guide to Python (great resource): Packaging Explained
  • Alternatives to Packaging: freeze your application
  • Python Packaging User Guide (probably the most useful to you)

I hope this helps.

like image 41
fragilewindows Avatar answered Jan 08 '23 04:01

fragilewindows