Outputting strings in html

0 views
Skip to first unread message

Stephan Diehl

unread,
Dec 3, 2002, 1:49:21 PM12/3/02
to
Ben wrote:
> Hey all,
> i'm very new to python programming (impressed so far - i love the way it
> handles cgi forms - WAY better than C :), but i'm a bit confused on one
> or two things....
>
> variables, unlike C aren't declared with types, right? so doing
> color = "#FFFF00" would give me a string FFFF00....but how can i use
> that if i'm writting html in a script?
> i want to do something like this:
>
> print """
> <html><head><title>hello</title></head>
> <body bgcolor=color>
> .....
> """
> but I can't get the string to be displayed?
> i've tried bgcolor="+color+", and searched google for "python """ print
> variable", but haven't found anything of use yet.
> any help for a noob?
>
> thanks.

use '<body bgcolor="%s">' % color

This just works like printf in C. %s is a placeholder for a string and
on the right of the '%' is the variable who's value is substituted.

Skip Montanaro

unread,
Dec 3, 2002, 2:20:33 PM12/3/02
to

>> print """
>> <html><head><title>hello</title></head>
>> <body bgcolor=color>
>> .....
>> """
>> but I can't get the string to be displayed?

Stephan> use '<body bgcolor="%s">' % color

Stephan> This just works like printf in C.

In some cases, even better than printf-style is to use named substitution.
If you have a lot of % formatting to do in a single string, matching up all
the %s's with all the variables can be cumbersome. This simple example
doesn't do the technique justice, but is analogous to the above example:

'<body bgcolor="%(color)s">' % locals()

where color is a local variable and locals() returns a dictionary
representing the current local variables (names as keys). "color" is looked
up as a key and its value substituted for "%(color)s".

With named substitution you can do all sorts of interesting things. For
example, you can get it to search locals() then globals() with:

d = {}
d.update(globals())
d.update(locals())

"some formatting string" % d

You can get expression expansion on-the-fly as well (though check the
sources of your formatting strings - this can be a security hole!):

class EvalDict:
def __getitem__(self, key, locals=None, globals=None):
caller = sys._getframe(1)
if locals is None:
locals = caller.f_locals
if globals is None:
globals = caller.f_globals
return eval(key, locals, globals)

"some formatting string" % EvalDict()

Or define your own interesting (to you) lookup semantics:

class SuperDict(EvalDict):
def __init__(self, *args):
self.mydicts = args

def __getitem__(self, key):
# check user-specified dicts first
for d in self.mydicts:
try:
return d[key]
except KeyError:
pass
# fall back to expression eval
caller = sys._getframe(1)
return EvalDict.__getitem__(self, key, caller.f_locals, caller.f_globals)

import math
"%(sin(47))f" % SuperDict(math.__dict__)

With judicious fiddling of EvalDict you can also have recursive evaluation
of formatting strings like "%(sin(%(myvar)f)f".

and-you-thought-python-needed-a-preprocessor-ly, y'rs,

--
Skip Montanaro - sk...@pobox.com
http://www.mojam.com/
http://www.musi-cal.com/

Mikkel Rasmussen

unread,
Dec 3, 2002, 4:22:20 PM12/3/02
to
How about this?

color = "#FFFF00"


print """
<html><head><title>hello</title></head>

<body bgcolor=""" + color + """>
.....
"""

Cheers
Mikkel Rasmussen
http://hjem.get2net.dk/footech


Ben <m...@here.nerd> wrote in message
news:pan.2002.12.03....@here.nerd...


> Hey all,
> i'm very new to python programming (impressed so far - i love the way it
> handles cgi forms - WAY better than C :), but i'm a bit confused on one
> or two things....
>
> variables, unlike C aren't declared with types, right? so doing
> color = "#FFFF00" would give me a string FFFF00....but how can i use
> that if i'm writting html in a script?
> i want to do something like this:
>

> print """
> <html><head><title>hello</title></head>
> <body bgcolor=color>
> .....
> """
> but I can't get the string to be displayed?

Reply all
Reply to author
Forward
0 new messages