Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 11th, 2006, 2:26 PM   #1
MegaArcon
Programmer
 
MegaArcon's Avatar
 
Join Date: Aug 2005
Posts: 66
Rep Power: 0 MegaArcon is an unknown quantity at this point
reloading html select boxs...is there a better way?

Hi all!

I'm doing some work with python and forums and I've created an app that will reload previous form data that a user has entered and saved to a file so they may edit it and re-save the changes. Among that data are several select boxs. I found that reloading the values for those were trickey and the method I used was cluckey and large. I was wondering if anybody knew a more intelegent/elegant way of doing this.

Basically, I've sucked the value back down from the file and, depending on it's value built a html string that will have the value selected. Then I print out the page with that string. Likea so:

.
.
.
#loaded from xml file
theValue =  n.childNodes[0].nodeValue

#html string to be the reloaded select box
htmlString = """
Please select one:
<select>
<option value="1"
"""

if (theValue == "1"):
   htmlString = htmlString + """ selected="selected">1</option>
                                 <option value="2">2</option>
                           """
else:
   htmlString = htmlString + """ >1</option>
                               <option value="2" selected="selected">2</option>
                         """

print myPage % {'select': htmlString}

I did out this example with only two values, but I'm actually doing it for a 24 hr clock, thus there are 24 values for the hour method and then another 7 for the minutes (increments of 10). So, as you can imagine, the code gets really big really fast. Any thoughts? I hope my example is clear enough.

Thanks.
__________________
Isn't that just the way life goes? If it's worth doing, it's NP-Hard. Todd Wareham - Memorial University of Newfoundland
MegaArcon is offline   Reply With Quote
Old Apr 11th, 2006, 2:46 PM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Couldn't you just use a loop?
print "<select>"
for hour in range(0, 24):
    for minute in range(0, 60, 10):
        time = "%2d:%.2d" % (hour, minute)
        if time == saved_value:
            print """<option selected="selected">"""
        else:
            print """<option>"""
        print time
        print """</option>"""
print """</select>"""
And out of interest, what system are you using? Straight CGI? mod_python? CherryPy?
Arevos is offline   Reply With Quote
Old Apr 11th, 2006, 8:33 PM   #3
MegaArcon
Programmer
 
MegaArcon's Avatar
 
Join Date: Aug 2005
Posts: 66
Rep Power: 0 MegaArcon is an unknown quantity at this point
Silly MegaArcon....if repetetion is present, use a loop.

To answer your question (since your always being so nice and answering everyone elses ), straight CGI. I'm doing all the server side stuff with python. (version 2.4.1 I believe....slightly out of date, I know. :p ).

Well, to be more acurate, I'm generating the pages and doing the server side work with python. I'm doing all the dynamic, user-using-page stuff with Javascript and layout with CSS. (Since that's what's it's for.) I'm hoping to have some fun with Ruby this summer in my spare time...see how that fairs in comparison....of course it could be like compareing apples and oranges...I'll stop ranting now....

Thanks Arevos!
__________________
Isn't that just the way life goes? If it's worth doing, it's NP-Hard. Todd Wareham - Memorial University of Newfoundland
MegaArcon is offline   Reply With Quote
Old Apr 12th, 2006, 5:08 AM   #4
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,086
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
I'm a little confused? How does straight CGI work? Will the web browser be constantly outputting everything you send it? As if "print" it outputting it directly to their screen?
Sane is offline   Reply With Quote
Old Apr 12th, 2006, 6:35 AM   #5
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Generally, when a CGI script is accessed, the HTTP server will run the script and take the resulting STDOUT from the CGI script and return it to the user in the HTTP response. The Python CGI module allows the script to get parameters and other information from the calling webserver.

The advantage with CGI is that it's relatively simple to setup. All you need is Python on your machine and a webserver capable of performing CGI.

The disadvantages are that CGI scripts are slow, because each time they're run, the Python interpretor has to be started up and parse the scripts. Systems like mod_python and CherryPy keep the Python intepretor and the Python bytecode in memory, so it's considerably faster. Another disadvantage is that CGI scripts are generally more hard work to program. Consider the following CGI script:
import cgi
print "Content-Type: text/html"
print
form = cgi.FieldStorage()
print "Hello %s" % form["name"]
And the equivalent script in CherryPy:
class Root:
    def index(name):
        return "Hello %s" % name
CGI tends to work on a much lower level. It's a no-frills system, and was popular in the early days of the web, but much less so today.
Arevos is offline   Reply With Quote
Old Apr 12th, 2006, 6:58 AM   #6
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,086
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
That also seems like it would be working against a lot of useful web applications today. Wouldn't forums and many other sites be made insanely slow, since it could not load static content? Or did I misinterpret what you meant when you said it dumps the memory?
Sane is offline   Reply With Quote
Old Apr 12th, 2006, 7:04 AM   #7
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
What do you mean, not load static content? CGI scripts are often used in conjuction with static HTML pages and images.

However, you'd be right in thinking that a forum done in CGI wouldn't be that fast; for each HTTP request, Python would have to be started up and parse through a .py source. For something simple like an email sending script, this doesn't matter so much, but a system that relies on a lot of dynamic content, such as a forum, would suffer noticable slowdown.
Arevos is offline   Reply With Quote
Old Apr 12th, 2006, 7:08 AM   #8
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by Arevos
However, you'd be right in thinking that a forum done in CGI wouldn't be that fast; for each HTTP request, Python would have to be started up and parse through a .py source. For something simple like an email sending script, this doesn't matter so much, but a system that relies on a lot of dynamic content, such as a forum, would suffer noticable slowdown.
So it only parses when the source is changed? If so: How does it verify so?
Does PHP work alike?
And, can CGI be used in C++ and compiled so it wouldn't have to do so every time?
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Apr 12th, 2006, 8:19 AM   #9
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by nnxion
So it only parses when the source is changed? If so: How does it verify so?
Systems like mod_python and CherryPy probably use file timestamps to check if the source has been changed. I know that whenever I change my CherryPy source, the CherryPy server automatically reloads itself. Presumably, mod_python does something similar.
Quote:
Originally Posted by nnxion
Does PHP work alike?
Possibly. I know it retains the PHP interpretor in memory. Perhaps some further caching takes place for executed scripts. I'm not sure whether PHP has anything equivalent to Python's bytecode.
Quote:
Originally Posted by nnxion
And, can CGI be used in C++ and compiled so it wouldn't have to do so every time?
Yes. CGI send data to a program through STDIN, and takes the output from STDOUT to send in a HTTP response. The CGI data passed in through STDIN has to be parsed, and that's what CGI libraries do. CGICC is a CGI library for C++.

Still, just because you can do something, doesn't mean you should. There's a reason few web applications are programmed in C++
Arevos is offline   Reply With Quote
Old Apr 12th, 2006, 7:25 AM   #10
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,086
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Quote:
Originally Posted by Arevos
However, you'd be right in thinking that a forum done in CGI wouldn't be that fast; for each HTTP request, Python would have to be started up and parse through a .py source. For something simple like an email sending script, this doesn't matter so much, but a system that relies on a lot of dynamic content, such as a forum, would suffer noticable slowdown.
That's exactly what I meant. Forums and sites like mine rely a lot on calculating static content to prepare all the pages. My site takes about 20 seconds just to calculate and parse the 400 profile pages and the thousands of other pages on the site. If it were to do this each and every time the .py source is parsed, my website would suffer tremendously. @_@
Sane is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:44 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC