473,386 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Problem combining python code and html

Hi all, I hope you have time to help me out a little. My problem is that I
want to combine some python code I have made with some html templates, I
found a tutorial at dev shed:
http://devshed.spunge.org/Server_Sid...CGI/page6.html
and : http://devshed.spunge.org/Server_Sid...CGI/page5.html and tried
to do it like they do, but it doesn't work.

I used the same code they use for making the two display functions and the
same template for the html form. I even tried using the form template they
use too, but it still dosnt work. I will now show how the functions and
templates look like.

The two display functions are:
def Display(Content):
TemplateHandle = open(TemplateFile, "r")
TemplateInput = TemplateHandle.read()
TemplateHandle.close()
BadTemplateException = "There was a problem with the HTML template."
SubResult = re.subn("<!-- *** INSERT CONTENT HERE *** -->",
Content,TemplateInput)
if SubResult[1] == 0:
raise BadTemplateException

print "Content-Type: text/html\n\n"
print SubResult[0]

def DisplayForm():
FormHandle = open(FormFile, "r")
FormInput = FormHandle.read()
FormHandle.close()
Display(FormInput)

The html template form is:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<META NAME="something" CONTENT="Somethingelse">
<title>Title</title>
</head>
<body>
<!-- *** INSERT CONTENT HERE *** -->
</body>
</html>

and the form template is:
<FORM METHOD="POST" ACTION="name.py">
<p>Signin number:<br> <INPUT type="text" NAME="playerid">
<p>Code:<br> <INPUT type="password" NAME="password"></p>
<input type=hidden name="action" value= tjeck >
<p><input type=submit value='Send'></p></form>

A very simple code that should tjeck if the code works could be

#!/pack/python-2.3.2/bin/python2.3
import re
import cgi

FormFile = "forma.html"
TemplateFile = "template.html"

def Display(Content):
TemplateHandle = open(TemplateFile, "r")
TemplateInput = TemplateHandle.read()
TemplateHandle.close()
BadTemplateException = "There was a problem with the HTML template."
SubResult = re.subn("<!-- *** INSERT CONTENT HERE *** -->",
Content,TemplateInput)
if SubResult[1] == 0:
raise BadTemplateException

print "Content-Type: text/html\n\n"
print SubResult[0]
def DisplayForm():
FormHandle = open(FormFile, "r")
FormInput = FormHandle.read()
FormHandle.close()
Display(FormInput)
if not form.has_key('action'):
DisplayForm()
else:
print "I had a key"

The meaning of this little code is, that the first time the script is run,
the form has to be displayed, and when the submit button is pressed the
script reloades, now the script has the key "action" and something else is
being done. But I get the error message:
error: multiple repeat
args = ('multiple repeat',) ", Content,TemplateInput) File
"/home//Python-2.3.2//lib/python2.3/sre.py", line 151, in subn return
_compile(pattern, 0).subn(repl, string, count)
I tried making the templates global, but that dosnt help either.
Is the code from dev shed wrong, or am I doing something wrong.
And is there a nother way to combine python code and html templates?
Thanks for your time
Jul 19 '05 #1
7 2138
Hansan wrote:
I used the same code they use for making the two display functions and the
same template for the html form. I even tried using the form template they
use too, but it still dosnt work. I will now show how the functions and
templates look like.

The two display functions are:
def Display(Content):
TemplateHandle = open(TemplateFile, "r")
TemplateInput = TemplateHandle.read()
TemplateHandle.close()
BadTemplateException = "There was a problem with the HTML template."
SubResult = re.subn("<!-- *** INSERT CONTENT HERE *** -->",
Content,TemplateInput)
re.subn takes a regular expression, in which certain characters have
special meanings. for example, in

<!-- *** INSERT JWZ QUOTE HERE *** -->

"*" is reserved character, which means "repeat preceeding expression".

so "***" means repeating a repeated repeat, which makes very little
sense, which is why the RE engine complains.

changing
SubResult = re.subn("<!-- *** INSERT CONTENT HERE *** -->",
Content,TemplateInput)


SubResult = TemplateInput.replace(
"<!-- *** INSERT CONTENT HERE *** -->",
Content
)

should work better.

</F>

Jul 19 '05 #2
Thanks for you reply and Help Fredrik Lundh

The change did remove the error message, and I can see what was wrong.
However there is now a new problem, the only thing that is displayed is one
single character and that is: <

Do you or anyone else have a suggestion to what the cause to this new
problem is ?

Thanks for your time


"Fredrik Lundh" <fr*****@pythonware.com> wrote in message
news:ma**************************************@pyth on.org...
Hansan wrote:
I used the same code they use for making the two display functions and
the
same template for the html form. I even tried using the form template
they
use too, but it still dosnt work. I will now show how the functions and
templates look like.

The two display functions are:
def Display(Content):
TemplateHandle = open(TemplateFile, "r")
TemplateInput = TemplateHandle.read()
TemplateHandle.close()
BadTemplateException = "There was a problem with the HTML template."
SubResult = re.subn("<!-- *** INSERT CONTENT HERE *** -->",
Content,TemplateInput)


re.subn takes a regular expression, in which certain characters have
special meanings. for example, in

<!-- *** INSERT JWZ QUOTE HERE *** -->

"*" is reserved character, which means "repeat preceeding expression".

so "***" means repeating a repeated repeat, which makes very little
sense, which is why the RE engine complains.

changing
SubResult = re.subn("<!-- *** INSERT CONTENT HERE *** -->",
Content,TemplateInput)


SubResult = TemplateInput.replace(
"<!-- *** INSERT CONTENT HERE *** -->",
Content
)

should work better.

</F>

Jul 19 '05 #3
Hansan a écrit :
Hi all, I hope you have time to help me out a little. My problem is that I
want to combine some python code I have made with some html templates, I
found a tutorial at dev shed: (snip a whole lot of code)
But I get the error message:
error: multiple repeat
args = ('multiple repeat',) ", Content,TemplateInput) File
"/home//Python-2.3.2//lib/python2.3/sre.py", line 151, in subn return
_compile(pattern, 0).subn(repl, string, count)
I tried making the templates global, but that dosnt help either.
Why should it ?
Is the code from dev shed wrong,
Seems like... And it is *very* ugly.
And is there a nother way to combine python code and html templates?


Many. The one I prefer is Zope Page Templates (there's a free-standing
implementation named SimpleTAL), but I didn't tried all and every
existing solutions.

Jul 19 '05 #4
Any suggestions to the problem with only one character being displayed?

Thanks

Thanks for your time
"Bruno Desthuilliers" <bd*****************@free.quelquepart.fr> wrote in
message news:42**********************@news.free.fr...
Hansan a écrit :
Hi all, I hope you have time to help me out a little. My problem is that
I want to combine some python code I have made with some html templates,
I found a tutorial at dev shed:

(snip a whole lot of code)
But I get the error message:
error: multiple repeat
args = ('multiple repeat',) ", Content,TemplateInput) File
"/home//Python-2.3.2//lib/python2.3/sre.py", line 151, in subn return
_compile(pattern, 0).subn(repl, string, count)
I tried making the templates global, but that dosnt help either.


Why should it ?
Is the code from dev shed wrong,


Seems like... And it is *very* ugly.
And is there a nother way to combine python code and html templates?


Many. The one I prefer is Zope Page Templates (there's a free-standing
implementation named SimpleTAL), but I didn't tried all and every existing
solutions.

Jul 19 '05 #5
"Hansan" wrote:
The change did remove the error message, and I can see what was wrong.
However there is now a new problem, the only thing that is displayed is one
single character and that is: <

Do you or anyone else have a suggestion to what the cause to this new
problem is ?


my fault; I somehow expected you to figure out that you had to fix up
the rest of the function as well, and forgot that you were just cutting
and pasting from a rather lousy example, without understand all the de-
tails.

here's the rest of the relevant code:

if SubResult[1] == 0:
raise BadTemplateException

print "Content-Type: text/html\n\n"
print SubResult[0]

in the original code, re.subn returns a 2-tuple containing the new string
and a count, but replace only returns the string. the easiest way to fix
this is to get rid of the BadTemplate check; just replace the above code
with:

print "Content-Type: text/html\n\n"
print SubResult

if you really want the BadTemplateException, you can do:

if SubResult == TemplateInput:
raise BadTemplateException

print "Content-Type: text/html\n\n"
print SubResult

hope this helps!

(I was about to add a short comment on how this illustrates why examples
are not a universal solution to all documentation problems, unless they're
carefully written and carefully documented. but such talk will only offend
the PHP crowd, so I better skip that)

</F>

Jul 19 '05 #6
has
Hansan wrote:
And is there a nother way to combine python code and html templates?


There's a decent list of Python templating engines at:

http://www.python.org/moin/WebProgramming

Look about a third of the way down down under "Variable
Insertion-Replacement Templating Applications (Pre-processors)". The
title is ridiculous but the links are sound.

HTH

Jul 19 '05 #7
Hi all and thanks for the help, With help from Fredrik I got the code
working, I will also look into the links you guys have suggested.
Hope you all are feeling well and happy.

"has" <ha*******@virgin.net> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hansan wrote:
And is there a nother way to combine python code and html templates?


There's a decent list of Python templating engines at:

http://www.python.org/moin/WebProgramming

Look about a third of the way down down under "Variable
Insertion-Replacement Templating Applications (Pre-processors)". The
title is ridiculous but the links are sound.

HTH

Jul 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Magnus Lie Hetland | last post by:
If I want to specify both mode and source encoding using the -*- FOO -*- syntax in emacs -- how can I do that? Both insist on being on the second line of the file, yet I have found no way of...
3
by: navavil | last post by:
Hi A few days ago we released new version of our Front Page (http://www.nana.co.il/) The page gui is based on html + css and its appeard to have problem uploading the images for all the...
3
by: alwayswinter | last post by:
I currently have a form where a user can enter results from a genetic test. I also have a pool of summaries that would correspond to different results that a user would enter into the form. I...
7
by: Barry | last post by:
Hi all, I've noticed a strange error on my website. When I print a capital letter P with a dot above, using & #7766; it appears correctly, but when I use P& #0775 it doesn't. The following...
3
by: exhuma.twn | last post by:
Simple problem: When I define a funtion the way you would with the publisher handler (without using psp), all works as expected. However when I define a publisher-like function and instantiate a...
10
by: John Salerno | last post by:
Is it possible to construct a C# form (using Visual Studio) but write only Python code for the events? Is there some way to tell your program to run Python whenever code is run, since it will be...
2
by: Turbo | last post by:
How to get absolute uri by combining the baseuri and the relative uri in an html page using javscript? I am looking for something similar to python's urlparse.urljoin('baseuri', 'relativeuri');...
2
by: Osiris | last post by:
I found this text about combining C-code with Pyton scripting on the P2P networks in PDF: Python Scripting for Computational Science Hans Petter Langtangen Simula Research Laboratory and...
5
by: Tristan Miller | last post by:
Greetings. Is it possible using HTML and CSS to represent a combining diacritical mark in a different style from the letter it modifies? For example, say I want to render Å‘ (Latin small letter...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.