[Tutor] HTMLgen

Lance E Sloan lsloan@umich.edu
Wed, 25 Sep 2002 06:38:17 -0400


--On Tuesday, September 24, 2002 11:59 -0400 Andrzej Kolinski 
<AKolinski@nriindustries.com> wrote:
> C:\>python
>>>> from HTMLgen import *
>>>> p = Paragraph("bla, bla/n, bla")
>>>> print p
> <P> bla, bla
> bla</P>
>
> But when I wanted to run the same test within IDLE I got:
>
>>>> from HTMLgen import *
> Traceback (most recent call last):
>   File "<pyshell#0>", line 1, in ?
>     from HTMLgen import *
> ImportError: No module named HTMLgen
>>>>
>
> I suspect I have some kind of installation/setup problem, but I don't know
> what the problem is.

I haven't used IDLE very much, so I'm not sure about this, but I would 
guess that your path (in sys.path) isn't set up correctly.  I bet that when 
you ran your program in the interpreter, HTMLgen was in your current 
directory, but when you were running IDLE, the current directory became the 
directory where IDLE is installed.  I think you could solve the problem 
like this:

  import sys
  sys.path.append('c:/path/to/HTMLgen/here')

Followed by the rest of your program.  Of course, fill in the correct path 
instead of using my example.

You could also add that path to Python's default sys.path.  You can edit 
one of the Python config files (I forget which) and I think you can also 
set an environment variable to do the same thing.

I'd also like to say that I recommend against putting HTML or 
HTML-generating code in programs.  I write CGIs for a living and at my 
organization, we always put HTML in template files, separate from the 
programs.  We had modules for doing this in Perl and when I introduced 
Python into our mix, I had to find something similar.

At first, I came up with my own solution.  Read lines from a file and 
replace values using %-substitution:

  def fillTemplate(filename = None, varDict = vars()):
    """Read a template from the specified file and using the string
      formatting operator (%), fill blanks in the template
      (e.g. %(<keyname>)s) with values from the specified dictionary."""

    if (filename == None) or (varDict == None):
      raise "filename and variable dictionary required"

    fo = open(filename, 'r')
    tmpl = fo.read() % varDict
    fo.close()

    return tmpl

So if I have a template file that contains:

  <html>
  <head><title>%(title)s</title></head>
  <body>
  %(message)s
  </body>
  </head>

and I call fillTemplate like this:

  fillTemplate('templatefilename', {'title': 'Hello', message: 'World'})

I get:

  <html>
  <head><title>Hello</title></head>
  <body>
  World
  </body>
  </head>

That function is one of the first I wrote in Python, so it could probably 
use some fixing, but in general it's a working idea.

Later I learned about Zope's DocumentTemplate module.  It's quite complex, 
but for my projects, it was what I needed.  I just downloaded Zope and 
pried DocumentTemplate out of it and it has worked well for me.

--
Lance E Sloan
Web Services, Univ. of Michigan: Full-service Web and database design,
development, and hosting.  Specializing in Python & Perl CGIs.
http://websvcs.itd.umich.edu/ - "Putting U on the Web"