Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

python outputs to top of page

Status
Not open for further replies.

gfender

IS-IT--Management
May 22, 2003
55
GB
Hi,
I'm working with python server pages and trying to integrate an old ASP program in with the new python program. The only way i've gotten this to work is by using <script language=&quot;python&quot; runat=&quot;server&quot;> otherwise the asp will not run. Inside the the script statement i'm calling Response.Write and it's outputting it to the top of the page and not in the specific table. I checked the tables and cells, they're okay. I've tried document.write and print with no luck. Anyone know what's going on?

Thank you for your help.
 
Remember the output is based on order of execution. If you had a python function in the middle of the table but were calling it before or after that table then the output would get shoved to the top. Along the same lines, if you aren't outputting tr's and td's but you are outputting into the middle of a table after a /tr, your output will get shoved above the table because it is not structured to fit in the table.

As for the problem with writing ASP pages strictly in Python, I assume that means your using
Code:
<%@Language=Python@>
. If you have an older version of python you will need to register the ActiveX component by hand, newer copies offer you that option during the install (2.2 here, and it did).
In order for the output to be seen you have to use the ASP objects that bind back to IIS, in this case Response.Write. You still have access to all of the standard ASP objects (this and JScript is where you see the real difference between ASp objects and VBScript objects). So your Response, Request, Server, etc objects will still be available to you.

Here is the 4Guys article I used when I first looked into installing Python on my system:
-Tarwn

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
minilogo.gif alt=tiernok.com
The never-completed website
 
I've taken out the <%@Language=Python@> statement because I want the default scripting language to be vbscript. I'm trying to include a header file that's written in vbscript and the only way i've gotten it to work is by making the default language vbscript and using <script language=&quot;python&quot; runat=&quot;server&quot;> for my python code.

I've checked over my tables and nothing seems out of place, all the tr's and td's are closing in the right place. I tested output immediately after the script statement, in vbscript, and it printed in the correct spot. It must have something to do with the order that it executes like you were saying, Tarwn.

I tried running a test code on the 4guys site you gave me but the client executing did not work. Would it help if i executed this on the client-side?
 
No, then your clients would all have to have python installed on their systems. Let me see the code for where your calling the function, perhaps a few lines before, the line you call it on, and a few lines after. I may be able to help more that way.

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
minilogo.gif alt=tiernok.com
The never-completed website
 
<title>Add New Hardware</title>
<link rel=&quot;stylesheet&quot; href=&quot;../format.css&quot;>
<!-- #INCLUDE FILE = &quot;header.asp&quot; -->
<h1 class=&quot;title&quot;>Add New Hardware</h1>

<script language=&quot;python&quot; runat=&quot;server&quot;>
import win32com.client
import pythoncom
import inv
Response.Write(&quot;Testing python using the script tag&quot;)
</script>

<!-- #INCLUDE FILE = &quot;footer.asp&quot; -->


The python output is appearing at the very top of the page while the content in header.asp is correctly placed.
 
wow, that is odd. You don't seem to be doing anything incorrect, I'm playing with a copy on my server, I'll let you know what I turn up.

-Tarwn

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
minilogo.gif alt=tiernok.com
The never-completed website
 
Ok, it appears that the python script is being interpreted/executed in a parallel process at the same time as the HTML. So whereas he html still has to be processed into the buffer, the python script is ready to go.
Don't quote me on this, thats just my best guesss after tinkering with it a bit.

The solution is to put all of your output from the Python in functions and call those functions when you want your output.

Here is another comparison:
Code:
<%
Option Explicit
%>
<html>
<head><link rel=&quot;stylesheet&quot; href=&quot;format.css&quot;></head>
<body>
<!-- #INCLUDE FILE = &quot;header.asp&quot; -->
<table border=&quot;1&quot;>
	<tr>
		<td>
			Python Row 1
		</td>
		<td>
<script language=&quot;Python&quot; runat=&quot;server&quot;>
Response.Write(&quot;This gets added to the buffer immediately as the script is processed/interpreted<br>&quot;)

def print_str():
	Response.Write(&quot;This gets evaluated only when it is called.&quot;)

</script>
		</td>
	</tr>
	<tr>
		<td>
			Python Row 2
		</td>
		<td>
			<%=print_str()%>
		</td>
	</tr>
	<tr>
		<td>
			VBScript Row 1
		</td>
		<td>
			<%=&quot;Just a VBScript string to show it's in the right location&quot;%>
		</td>
	</tr>
</table>
<!-- #INCLUDE FILE = &quot;footer.asp&quot; -->
</body>
</html>

Now technically I would generally split the code a little like this.

The python will only include class and function definitions. It will be necessary to call these functions or instantiate these classes from the VBScript. For readability I would place this in either an external include file or near the top of the script:
Code:
<%
Option Explicit
%>
<script language=&quot;Python&quot; runat=&quot;server&quot;>
def print_str():
	Response.Write(&quot;This gets evaluated only when it is called.&quot;)

def incr(valu):
	return valu + 1
</script>
<html>
<head><link rel=&quot;stylesheet&quot; href=&quot;format.css&quot;></head>
<body>
<!-- #INCLUDE FILE = &quot;header.asp&quot; -->
<table border=&quot;1&quot;>
	<tr>
		<td>
			Python Row 1
		</td>
		<td>
			5 + 1 = <%=incr(5)%>
		</td>
	</tr>
	<tr>
		<td>
			Python Row 2
		</td>
		<td>
			<%=print_str()%>
		</td>
	</tr>
	<tr>
		<td>
			VBScript Row 1
		</td>
		<td>
			<%=&quot;Just a VBScript string to show it's in the right location&quot;%>
		</td>
	</tr>
</table>
<!-- #INCLUDE FILE = &quot;footer.asp&quot; -->
</body>
</html>

I would advise keeping all of the python code in the same set of script tags. The other reason I like it at the top is that this way the interpreter can go ahead and have time to read through all of the funtions before we have a chance to make a call. If the two processes are running independantly we don't want to take a chance of the VBScript calling a function before the Python interpreter has gotten to it yet.

Anyways, just my take on this, hope it helps,

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
minilogo.gif alt=tiernok.com
The never-completed website
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top