generate HTML

S

s99999999s2003

hi
i have fucntion that generates a HTML page

def genpage(arg1,arg2):
print ''' <div align="right"><font size="-1">BLAH BLAH.....%s %s
''' % (arg1, arg2)

print ''' <table> ....blah blah... %s %s

</table>''' % (arg1,arg2)'

The func is something like that, alot of open''' and closing ''' triple
quotes. anyway, i wish to print all these into a HTML output file so
that when i click on it, it shows me the html page. How can i do that?

i tried
f = open("output.html","w")
f.write ( 'print '''<div align ....

I am stuck at above after doing a lot of f.write for every line of HTML
.. Any betterways to do this in python?

something like here documents in a shell script where it can also be
directed to a file.
thanks
 
J

Jeffrey Schwab

hi
i have fucntion that generates a HTML page

def genpage(arg1,arg2):
print ''' <div align="right"><font size="-1">BLAH BLAH.....%s %s
''' % (arg1, arg2)

print ''' <table> ....blah blah... %s %s

</table>''' % (arg1,arg2)'

The func is something like that, alot of open''' and closing ''' triple
quotes. anyway, i wish to print all these into a HTML output file so
that when i click on it, it shows me the html page. How can i do that?

i tried
f = open("output.html","w")
f.write ( 'print '''<div align ....

I am stuck at above after doing a lot of f.write for every line of HTML
. Any betterways to do this in python?

something like here documents in a shell script where it can also be
directed to a file.
thanks

Why not just redirect the output of your Python script from the shell?
E.g.:

python generate_html.py > output.html
 
S

Scott David Daniels

Jeffrey said:
(e-mail address removed) wrote: ....
...
Why not just redirect the output of your Python script from the shell?
E.g.:

python generate_html.py > output.html

Or you can even do the following Python dance:

def gen_to_file(filename, arg1, arg2):
import sys
former, sys.stdout = sys.stdout, open("output.html", "w")
try:
genpage(arg1, arg2)
finally:
htmlfile, sys.stdout = sys.stdout, former
htmlfile.close()
print 'file %r written' % filename

--Scott David Daniels
(e-mail address removed)
 
S

s99999999s2003

hi thanks for all the help
actually i am doing it the hard way

alist = [ '<div align="right"><font size="-1">TEST<br>',
'<p align="center"><strong><br>',
'blah' ]

f = open('test.html",'w')
f.writelines(alist)
f.close()

but everytime i hit
....
alist = [ '<div align="right"><font size="-1">....
ValueError: unsupported format character '"' (0x22) at index 14

what does it mean?

i tried alist = [ '<div align="right"><font size="-1">TEST<br>'] on the
IDE and it works

thanks.
 
J

Jim

Perhaps you are trying to do this:
'text to go here: %s' % ('text',)
? For that you need a double-quoted string:
"text to go here: %s" % ('text',)
(or triple-doubles: """ .. """ as you noted).

Jim
 
K

Kent Johnson

Jim said:
Perhaps you are trying to do this:
'text to go here: %s' % ('text',)
? For that you need a double-quoted string:
"text to go here: %s" % ('text',)

Uh, no, not in Python:'text to go here: text'

Kent
 
T

Thomas Guettler

Am Tue, 15 Nov 2005 02:52:54 -0800 schrieb s99999999s2003:
alist = [ '<div align="right"><font size="-1">....
ValueError: unsupported format character '"' (0x22) at index 14

Look at this:

===> python
Python 2.3.4 (#1, Feb 7 2005, 15:50:45)
[GCC 3.3.4 (pre 3.3.5 20040809)] on linux2
Type "help", "copyright", "credits" or "license" for more information.Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: unsupported format character 'q' (0x71) at index 2

If you don't have a '% (...)' behind the string, all percent signs are
ignored. But if you use it, %q will bring you an error because only %s, %d, ...
is supported.

If you create html, here the way I do it:

rows=[]
for i in range(...).
rows.append("....")
rows=''.join(rows)

date=time.strftime()
html="""
Today: %(date)s
<table>
%(rows)s
</table>
....""" % locals()

outfile="out.html"
fd=open(outfile, "wt")
fd.write(html)
fd.close()
print "Created %s" % outfile

HTH,
Thomas
 
S

s99999999s2003

thanks
i will check out the example you have given.actually my html output is
a bit dynamic in the sense that i may have different number of rows
depending on some inputs.
so i am putting everything into a list and using 'extend' to append to
that list for every dynamically generated rows using for/while loops.
then at the end of the code, i use writelines() to output to the file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,610
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top