Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: dynamically creating html code with python...

Reply
Thread Tools

Re: dynamically creating html code with python...

 
 
Guest
Posts: n/a
 
      08-11-2008
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

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote :

> 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.
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list




 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      08-11-2008
(E-Mail Removed) a écrit :
> 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.

> 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.


> 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)


> 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.
 
Reply With Quote
 
 
 
 
Guest
Posts: n/a
 
      08-11-2008
This makes sense. Thanks!

I managed to get what I wanted with something similar to what you suggested:

Code:
print "Content-Type: text/html\n\n"
html="""
<html>
    <head>
        <title>
            data analysis site
        </title>
    </head>
    <body>
        <p>This is a test</p>
        <IMG SRC="%s" />
        <p>After image text</p>
    </body>
</html>"""

print html % myChartsLib.myPlotType(TheData)
and the script returns

Code:
    f = StringIO.StringIO()
    pylab.savefig(f)
    return 'data:image/png,' + urllib.quote(f.getvalue())
This works fine in Firefox, but not in IE7. Any ideas why?

BTW, you are right about me not having a clue about http. It's the first time I try to do something with it. May be you could point me out to some good links where I can learn.

I will take a look into Mako too.

Thanks again.

Bruno Desthuilliers <(E-Mail Removed)> wrote :

> (E-Mail Removed) a écrit :
> > 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.
>
> > 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.
>
>
> > 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)
>
>
> > 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.
> --
> http://mail.python.org/mailman/listinfo/python-list




 
Reply With Quote
 
Carsten Haese
Guest
Posts: n/a
 
      08-11-2008
(E-Mail Removed) wrote:
>
Code:
>     f = StringIO.StringIO()
>     pylab.savefig(f)
>     return 'data:image/png,' + urllib.quote(f.getvalue())
>
>
> This works fine in Firefox, but not in IE7. Any ideas why?
>


Yes. Internet Explorer doesn't support the "data:" URI scheme.

--
Carsten Haese
http://informixdb.sourceforge.net
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      08-12-2008
En Mon, 11 Aug 2008 18:47:53 -0300, <(E-Mail Removed)> escribi�:

> BTW, you are right about me not having a clue about http. It's the first
> time I try to do something with it. May be you could point me out to
> some good links where I can learn.


This topic was discussed some time ago - perhaps reading this thread may
help you:
http://mail.python.org/pipermail/pyt...il/485856.html

--
Gabriel Genellina

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamically creating html code with python... Python 2 08-12-2008 05:21 AM
Re: dynamically creating html code with python... Python 1 08-11-2008 06:21 PM
creating a html or jsp file dynamically KK Java 3 07-25-2008 09:07 PM
how do I dynamically change an html meta tag from my code behind code? ntm ASP .Net 2 01-19-2004 05:00 PM
Creating a html drop-down list dynamically Gurry Javascript 4 12-19-2003 01:38 PM



Advertisments