views:

51

answers:

1

Hi. I'm trying to build a simple webpage with multiple checkboxes, a Textbox and a submit buttom.

I've just bumped into web programing in Python and am trying to figure out out to do it with CherryPy.

I need to associate each checkbox to a variable so my .py file knows which ones were selected when clicking the 'Start button'. Can someone please give some code example ? Do I have any advantage including some Python Javascript Compiler like Pyjamas?

<_form action="../remote_targets/ssh_grab.py"> <_label for="goal">Host Availability: <_input style="margin-left: 30px;" type="checkbox" name="goal[]" value="cpu" /> CPU idle
<_input style="margin-left: 30px;" type="checkbox" name="goal[]" value="lighttpd" /> Lighttpd Service
<input style="margin-left: 30px;" type="checkbox" name="goal[]" value="mysql" /> Mysql Service
<
/form>

DB stats: Number of events
Waiting in the Queue

Thanks !

+1  A: 

Here's a minimal example:

import cherrypy

class Root(object):
    @cherrypy.expose
    def default(self, **kwargs):
        print kwargs
        return '''<form action="" method="POST">
Host Availability:
<input type="checkbox" name="goal" value="cpu" /> CPU idle
<input type="checkbox" name="goal" value="lighttpd" /> Lighttpd Service
<input type="checkbox" name="goal" value="mysql" /> Mysql Service
<input type="submit">
</form>'''

cherrypy.quickstart(Root())

And here is the terminal output:

$ python stacktest.py 
[10/Sep/2010:14:25:55] HTTP Serving HTTP on http://0.0.0.0:8080/
CherryPy Checker:
The Application mounted at '' has an empty config.
Submitted goal argument: None
127.0.0.1 - - [10/Sep/2010:14:26:09] "GET / HTTP/1.1" 200 276 "" "Mozilla..."
Submitted goal argument: ['cpu', 'mysql']
127.0.0.1 - - [10/Sep/2010:14:26:15] "POST / HTTP/1.1" 200 276 "http://localhost:8003/" "Mozilla..."
[10/Sep/2010:14:26:26] ENGINE <Ctrl-C> hit: shutting down app engine
[10/Sep/2010:14:26:26] HTTP HTTP Server shut down
[10/Sep/2010:14:26:26] ENGINE CherryPy shut down
$

As you can see, CherryPy will collect multiple controls with the same name into a list. You don't need the [] suffix to tell it to do that. Then, iterate over the list to see which values were submitted. (Keep in mind that, if only one item is selected, then the goal argument will be a single string instead of a list!)

fumanchu
You're a saint!I'd vote on your answer if I already had the points.Thanks for pushing me into the right direction.I've mostly got the thing up and running,I'm also using 'argument_name'.length to safeguard against your"if only one item is selected, then the goal argument will be a single string instead of a list" warning.Now I'll need to decipher how to embed some basic CSS into it (which I still know zero :)) Can anyone point me to some little projects examples in CherryPy like this one?I haven't been able to google nothing more than the vanilla 'Hello world!'/'basic blog' examples
stack_zen
About, "'argument_name'.length" Actually I've ended up using type(argument_name) to differentiate how to extract the data
stack_zen
Er, you can't "accept" the answer?
fumanchu