views:

346

answers:

5

How do you serve Cheetah in production?

Guys can you share the setup on how to precompile and serve cheetah in production

Since we dont compile templates in webpy it is getting upstream time out errors. If you could share a good best practise it would help

*

Jeremy [email protected] wrote: For a production site, I use Cheetah with pre-compiled templates - it's very fast (the templates import especially quickly when python compiled and optimised). A bit of magic with the imp module takes a template name and a base directory (configured in a site-specific config) and loads up that template, taking care of #extends and

import directives as appropriate. I don't use the built-in support for

Cheetah, however. The new template library is also only imported to display the debugerror page

*

+1  A: 

Maybe compile automagically on as needed basis:

import sys
import os
from os import path
import logging
from Cheetah.Template import Template
from Cheetah.Compiler import Compiler

log = logging.getLogger(__name__)

_import_save = __import__
def cheetah_import(name, *args, **kw):
  """Import function which search for Cheetah templates.

  When template ``*.tmpl`` is found in ``sys.path`` matching module
  name (and corresponding generated Python module is outdated or
  not existent) it will be compiled prior to actual import.
  """
  name_parts = name.split('.')
  for p in sys.path:
    basename = path.join(p, *name_parts)
    tmpl_path = basename+'.tmpl'
    py_path = basename+'.py'
    if path.exists(tmpl_path):
      log.debug("%s found in %r", name, tmpl_path)
      if not path.exists(py_path) or newer(tmpl_path, py_path):
        log.info("cheetah compile %r -> %r", tmpl_path, py_path)
        output = Compiler(
            file=tmpl_path,
            moduleName=name,
            mainClassName=name_parts[-1],
            )
        open(py_path, 'wb').write(str(output))
      break
  return _import_save(name, *args, **kw)

def newer(new, old):
    """Whether file with path ``new`` is newer then at ``old``."""
    return os.stat(new).st_mtime > os.stat(old).st_mtime

import __builtin__
__builtin__.__import__ = cheetah_import
lispmachine
A: 

how do we use this cheetah_import

A: 

lispmachine the solution you provide is cheetah_import but can you tell how to use it with webpys cheetah.py

in webpy cheetah.py we use

return web.render('index.html') and that compiles the html file dynamically i want to precompile them

A: 

In this autocompile function, or cheetah_import does cheetah fill out or replace out the #Include items

and are you [email protected] in the webpy emailing list thanks

pylabs
A: 

This works

try:web.render('mafbase.tmpl', None, True, 'mafbase')
except:pass

This is what i did with you code from cheetahimport import * sys.path.append('./templates') cheetah_import('mafbase')

includes dont work in the given method.

This is the error i got

localhost pop]$ vi code.py
[mark@localhost pop]$ ./code.py 9911
http://0.0.0.0:9911/
Traceback (most recent call last):
 File "/home/mark/work/common/web/application.py", line 241, in process
 return self.handle()
File "/home/mark/work/common/web/application.py", line 232, in handle
return self._delegate(fn, self.fvars, args)
File "/home/mark/work/common/web/application.py", line 411, in _delegate
return handle_class(cls)
File "/home/mark/work/common/web/application.py", line 386, in handle_class
return tocall(*args)
File "user.py", line 264, in proxyfunc
return func(self, *args, **kw)
File "/home/mark/work/pop/code.py", line 1801, in GET
return web.render('subclass.html')
File "/home/mark/work/common/web/cheetah.py", line 104, in render
return str(compiled_tmpl)
File "/usr/lib/python2.5/site-packages/Cheetah-2.0.1-py2.5-linux-i686.egg/Cheetah/Template.py", line 982, in __str__
def __str__(self): return getattr(self, mainMethName)()
File "templates/mafbase.py", line 713, in respond
self._handleCheetahInclude("widgetbox.html", trans=trans, includeFrom="file", raw=False)
File "/usr/lib/python2.5/site-packages/Cheetah-2.0.1-py2.5-linux-i686.egg/Cheetah/Template.py", line 1512, in _handleCheetahInclude
nestedTemplateClass = compiler.compile(source=source,file=file)
File "/usr/lib/python2.5/site-packages/Cheetah-2.0.1-py2.5-linux-i686.egg/Cheetah/Template.py", line 693, in compile
 fileHash = str(hash(file))+str(os.path.getmtime(file))

File "/usr/lib/python2.5/posixpath.py", line 143, in getmtime return os.stat(filename).st_mtime OSError: [Errno 2] No such file or directory: '/home/mark/work/pop/widgetbox.html'

pylabs