Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » PythonGoto page 1, 2  Next

dynamically creating html code with python...

 
Jump to:  
 
Guest
PostPosted: Mon Aug 11, 2008 11:15 am    Post subject: dynamically creating html code with python...
       
Hi,

how can I combine some dynamically generated html code (using python) with the output of a urllib.openurl() call?

I have tried to use the StringIO() class with .write functions, but it did not work. Below is the code that does not work.

Code:

f=StringIO.StringIO()
f.write('<html><head><title>data analysis</title></head><body>')
f.write(urllib.urlopen("http://localhost/path2Libs/myLibs.py", urllib.urlencode(TheData)))
f.write("</body></html>")

print "Content-type: text/html\n"
print f.read()
f.close()


What is wrong with this approach/code? Is there an easier way of doing it?

Thanks.
 

 
Carsten Haese
PostPosted: Mon Aug 11, 2008 11:55 am    Post subject: Re: dynamically creating html code with python...
       
anartz@anartz.cjb.net wrote:
Quote:
Hi,

how can I combine some dynamically generated html code (using python) with the output of a urllib.openurl() call?

I have tried to use the StringIO() class with .write functions, but it did not work. Below is the code that does not work.

Help us help you. "Does not work" is not a helpful description. Do you
get an error message? If so, what is the error message? Do you get an
unexpected result? If so, what is the unexpected result, and what is the
result you expected?

Quote:
Is there an easier way of doing it?

Probably. You seem to be invoking a python program that's local to your
computer, so it's not quite clear why you're going the roundabout way of
serving it up through a web server. Then again, you're not telling us
the actual problem you're trying to solve, so we have no idea what the
best solution to that problem might be.

--
Carsten Haese
LINK
 

 
Jerry Hill
PostPosted: Mon Aug 11, 2008 12:07 pm    Post subject: Re: dynamically creating html code with python...
       
On Mon, Aug 11, 2008 at 9:15 AM, <anartz@anartz.cjb.net> wrote:
Quote:
Code:

f=StringIO.StringIO()
f.write('<html><head><title>data analysis</title></head><body>')
f.write(urllib.urlopen("http://localhost/path2Libs/myLibs.py", urllib.urlencode(TheData)))
f.write("</body></html>")

print "Content-type: text/html\n"
print f.read()
f.close()


What is wrong with this approach/code? Is there an easier way of doing it?

A StringIO object works a lot like a file. When you write to it, it
keeps track of the current position in the file. When you read from
it, it reads from the current position to the end of the file. Once
you're done writing to the StringIO object, you can rewind the
position to the beggining and then read to the end, like this:

f = StringIO.StringIO()
f.write('This is some data')
f.seek(0)
print f.read()

StringIO objects also have a special getvalue() method, which allows
you to get the entire contents without changing the current position.
You can replace your f.read() with f.getvalue() without having to mess
with seek(), but then your code won't work with real files, if that's
important.

--
Jerry
 

 
Guest
PostPosted: Mon Aug 11, 2008 1:30 pm    Post subject: Re: dynamically creating html code with python...
       
Sorry, my fault...

I am trying to build a web application for data analysis. Basically some data will be read from a database and passed to a python script (myLibs.py) to build an image as follows.

Code:

f=urllib.urlopen("http://localhost/path2Libs/myLibs.py",urllib.urlencode(TheData))
print "Content-type: image/png\n"
print f.read()
f.close()


This section behaves as expected, and I can see the chart on the web-page.

Now, I would like to add some text and possibly more charts (generated in the same way) to my web-page. This is what I need help with.

I tried to add some html code to the f variable (file-type variable) before and after the plot generated (see first post). When I load the page in a browser, I get a blank page, not even the chart (that I used to get) appears any more.

There is no error messages in the server's error log, and when I run it as a python script I get the following output:

Content-type: image/png\n

My question:
How can I use python to dynamically add descriptive comments (text), and possibly more charts to the web-page?

Hope this is more explanatory.

Thanks

Anartz@anartz.cjb.net wrote :

Quote:
Hi,

how can I combine some dynamically generated html code (using python) with the output of a urllib.openurl() call?

I have tried to use the StringIO() class with .write functions, but it did not work. Below is the code that does not work.

Code:

f=StringIO.StringIO()
f.write('<html><head><title>data analysis</title></head><body>')
f.write(urllib.urlopen("http://localhost/path2Libs/myLibs.py", urllib.urlencode(TheData)))
f.write("</body></html>")

print "Content-type: text/html\n"
print f.read()
f.close()


What is wrong with this approach/code? Is there an easier way of doing it?

Thanks.



--
LINK
 

 
Guest
PostPosted: Mon Aug 11, 2008 2:26 pm    Post subject: Re: dynamically creating html code with python...
       
I have tried calling a script containing the code below from a web browser and it did not get the text.

Code:

#!c:/Python25/python.exe -u

import StringIO

f=StringIO.StringIO()
f.write('<html><head><title>data analysis site</title></head><body>')
f.write("<p>This is a trial test</p>")
f.write("</body></html>")

print "Content-type: text/html\n"
print f.read()
f.close()


So, I assume this is not the way to create web pages.... any links that can help me take the right way?

Thanks!


Jerry Hill <malaclypse2@gmail.com> wrote :

Quote:
On Mon, Aug 11, 2008 at 9:15 AM, <anartz@anartz.cjb.net> wrote:
Code:

f=StringIO.StringIO()
f.write('<html><head><title>data analysis</title></head><body>')
f.write(urllib.urlopen("http://localhost/path2Libs/myLibs.py", urllib.urlencode(TheData)))
f.write("</body></html>")

print "Content-type: text/html\n"
print f.read()
f.close()


What is wrong with this approach/code? Is there an easier way of doing it?

A StringIO object works a lot like a file. When you write to it, it
keeps track of the current position in the file. When you read from
it, it reads from the current position to the end of the file. Once
you're done writing to the StringIO object, you can rewind the
position to the beggining and then read to the end, like this:

f = StringIO.StringIO()
f.write('This is some data')
f.seek(0)
print f.read()

StringIO objects also have a special getvalue() method, which allows
you to get the entire contents without changing the current position.
You can replace your f.read() with f.getvalue() without having to mess
with seek(), but then your code won't work with real files, if that's
important.

--
Jerry
--
LINK
 

 
Jerry Hill
PostPosted: Mon Aug 11, 2008 2:46 pm    Post subject: Re: dynamically creating html code with python...
       
On Mon, Aug 11, 2008 at 12:26 PM, <anartz@anartz.cjb.net> wrote:
Quote:
I have tried calling a script containing the code below from a web browser and it did not get the text.

You quoted my post that answered this question, but did not implement
either of the two solutions I suggested. I continue to suggest that
you either: f.seek(0) before you f.read(), or that you replace
f.read() with f.getvalue().

Also, you may want to read the docs on
StringIO - LINK
File objects - LINK

--
Jerry
 

 
Guest
PostPosted: Mon Aug 11, 2008 3:05 pm    Post subject: Re: dynamically creating html code with python...
       
Hi,

Thanks for your patience.

I got the text displayed in the web browser with the following code:

Code:

f=StringIO.StringIO()
f.write('<html><head><title>data analysis site</title></head><body>')
f.write("<p>This is a trial test</p>")
f.write("</body></html>")

print "Content-type: text/html\n"
print f.getvalue()
f.close()


Now I am trying to put both the image and the text together, but the following lines do not create the text with the chart that I expected.

Code:

f=StringIO.StringIO()
f.write('<html><head><title>data analysis site</title></head><body>')
f.write("<p>This is a trial test</p>")
f.write(urllib.urlopen("http://localhost/myLibs/ChartLib.py",urllib.urlencode(TheData)))
f.write("</body></html>")

print "Content-type: text/html\n"
print f.getvalue()
f.close()


I am wondering if urllib.urlopen is the command I need to revise.

Thanks for the pointers as well. I will look into them.




Jerry Hill <malaclypse2@gmail.com> wrote :

Quote:
On Mon, Aug 11, 2008 at 12:26 PM, <anartz@anartz.cjb.net> wrote:
I have tried calling a script containing the code below from a web browser and it did not get the text.

You quoted my post that answered this question, but did not implement
either of the two solutions I suggested. I continue to suggest that
you either: f.seek(0) before you f.read(), or that you replace
f.read() with f.getvalue().

Also, you may want to read the docs on
StringIO - LINK
File objects - LINK

--
Jerry
--
LINK
 

 
Timothy Grant
PostPosted: Mon Aug 11, 2008 3:59 pm    Post subject: Re: dynamically creating html code with python...
       
On Mon, Aug 11, 2008 at 10:05 AM, <anartz@anartz.cjb.net> wrote:
Quote:
Hi,

Thanks for your patience.

I got the text displayed in the web browser with the following code:

Code:

f=StringIO.StringIO()
f.write('<html><head><title>data analysis site</title></head><body>')
f.write("<p>This is a trial test</p>")
f.write("</body></html>")

print "Content-type: text/html\n"
print f.getvalue()
f.close()


Now I am trying to put both the image and the text together, but the following lines do not create the text with the chart that I expected.

Code:

f=StringIO.StringIO()
f.write('<html><head><title>data analysis site</title></head><body>')
f.write("<p>This is a trial test</p>")
f.write(urllib.urlopen("http://localhost/myLibs/ChartLib.py",urllib.urlencode(TheData)))
f.write("</body></html>")

print "Content-type: text/html\n"
print f.getvalue()
f.close()


I am wondering if urllib.urlopen is the command I need to revise.

Thanks for the pointers as well. I will look into them.




Jerry Hill <malaclypse2@gmail.com> wrote :

On Mon, Aug 11, 2008 at 12:26 PM, <anartz@anartz.cjb.net> wrote:
I have tried calling a script containing the code below from a web browser and it did not get the text.

You quoted my post that answered this question, but did not implement
either of the two solutions I suggested. I continue to suggest that
you either: f.seek(0) before you f.read(), or that you replace
f.read() with f.getvalue().

Also, you may want to read the docs on
StringIO - LINK
File objects - LINK

--
Jerry


It looks to me like you are opening the url, but never retrieving the
content of the url.

I think you may have better luck with urllib2 which has a read() method.

LINK

--
Stand Fast,
tjg. [Timothy Grant]
 

 
Diez B. Roggisch
PostPosted: Mon Aug 11, 2008 4:21 pm    Post subject: Re: dynamically creating html code with python...
       
anartz@anartz.cjb.net wrote:

Quote:
Hi,

Thanks for your patience.

I got the text displayed in the web browser with the following code:

Code:

f=StringIO.StringIO()
f.write('<html><head><title>data analysis site</title></head><body>')
f.write("<p>This is a trial test</p>")
f.write("</body></html>")

print "Content-type: text/html\n"
print f.getvalue()
f.close()


Now I am trying to put both the image and the text together, but the
following lines do not create the text with the chart that I expected.

Code:

f=StringIO.StringIO()
f.write('<html><head><title>data analysis site</title></head><body>')
f.write("<p>This is a trial test</p>")

f.write(urllib.urlopen("http://localhost/myLibs/ChartLib.py",urllib.urlencode(TheData)))
f.write("</body></html>")

print "Content-type: text/html\n"
print f.getvalue()
f.close()


I am wondering if urllib.urlopen is the command I need to revise.

Thanks for the pointers as well. I will look into them.

That's bogus. instead of the urllib-stuff, you need to write out a <img>-tag
with the src pointing to your image - most probably like this:

<img src="/myLibs/ChartLib.py"/>

Diez
 

 
Bruno Desthuilliers
PostPosted: Mon Aug 11, 2008 6:25 pm    Post subject: Re: dynamically creating html code with python...
       
anartz@anartz.cjb.net a écrit :
Quote:
Sorry, my fault...

I am trying to build a web application for data analysis. Basically
some data will be read from a database and passed to a python script
(myLibs.py) to build an image as follows.

Code:

f=urllib.urlopen("http://localhost/path2Libs/myLibs.py",urllib.urlencode(TheData))
print "Content-type: image/png\n"
print f.read()
f.close()


This section behaves as expected, and I can see the chart on the
web-page.

Indeed. Using an http request to call a local script is totally
braindead, but this is another problem.

Quote:
Now, I would like to add some text and possibly more charts
(generated in the same way) to my web-page.

Which one ? What you showed is a way to generate an image resource (with
mime-type 'image/png'), not an html page resource (mime-type :
text/html). Images resources are not directly embedded in html pages -
they are *referenced* from web pages (using an <img> tag), then it's up
to the user-agent (usually, the browser) to emit another http request to
get the image.


Quote:
This is what I need help
with.

Not tested (obviously), but what you want is something like:

print "Content-type: text/html\n"
print """
<html>
<head>
<title>data analysis site</title>
</head>
<body>
<p>This is a trial test</p>
<img src="http://localhost/myLibs/ChartLib.py?%s" />
</body>
</html>
""" % urllib.urlencode(TheData)


Quote:
My question: How can I use python to dynamically add descriptive
comments (text), and possibly more charts to the web-page?

The code you showed so far either tried to add text/html to an image
(that is, binary data), or to embed the image's binary data into
text/html. None of this makes sense. Period. The problem is not with
Python. The problem is that you can't seriously hope to do web
programming without any knowledge of the http protocol.

Also and FWIW, you'd be better using a decent templating system (mako,
cheetah, genshi, tal, whatever fits your brain) instead of generating
html that way.
 

Page 1 of 2 .:. Goto page 1, 2  Next

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

hotel panorama kreta dessous In vitro Szyby samochodowe newsy.bober.duu.pl