What, when , where and how to use ARGS and VARS

2,644 views
Skip to first unread message

theoffi...@gmail.com

unread,
Apr 2, 2013, 3:56:25 AM4/2/13
to web...@googlegroups.com
Hi guys! Sorry but I still do not quite understand after reading the chapters about "Dispatching" and "URL". i guess my programming foundation isn't that strong... and i have problem understanding without referring to examples.

Here is my situation and i would like to get a general idea on how to solve the problem. May be my idea was wrong so please correct me :)

I have just successfully connect a device to my Raspberry Pi, with web2py as the webserver on Raspberry pi. Currently, i have created a home page using the template, and there is a "Login" button on it. When i click on the image (button), it directs me to default/test.html. the device gets the login information and turns on and off the testlight.

in default/index.html:
<a href = "{{=URL(c='default', f='test')}}"><img src ="/Comfort2/static/images/login.jpg" width ="128" height="69"></a>


in default.py:

def
test():
import serial
import time
response.flash=T("welcome home!")
time.sleep(1)

serialport= serial.Serial("/dev/ttyAMA0", 9600, timeout=0.5) #opening the port


serialport.write("\x03LI1234\x0D") #write a serial command to log into the device
reply=serialport.readlines(1)
print reply
time.sleep(1)
serialport.write("\x03O!0101\x0D") #turn on a testlight
time.sleep(2)
serialport.write("\x03O!0100\x0D") #turn off a testlight
return dict()
 
However, i would like to separate it into a few tasks. So, after pressing the "login" button, it will only login and direct me to test.html with 2 buttons (Turn on light and turn off light). if i press on the turn on light button, the testlight will turn on and same for the other one. I would like all these to be under one function because the commands can only be sent when it is logged into the device. (if it is under another function, i will need to login again.) So do i need to use args and vars at this point?

I do not really understand what is args and vars and how are they used. the information in the chapters taught me that
request.args = ['x', 'y', 'z'] 
request.vars={'p':1, 'q':2}

but what are x,y,z,p, q, 1 and 2? under what conditions are they declared and where do they appear?

Another question: can i display a jQuery keypad using web2py?

My progress using cherrypy is slightly more ahead than web2py. i am sure i can transfer my work in cherrypy over to web2py... but i must get the whole concept clear... please help me.
sorry that i sound very noob.... but much thanks to everyone for your patience and guidance! :)

Niphlod

unread,
Apr 2, 2013, 4:28:50 AM4/2/13
to web...@googlegroups.com
/app/default/test
points to executing your test() inside the controller named default.py
args and vars are, in respect
- args : /app/default/test/1/2/3
- vars : /app/default/test?foo=bar&foo=bar2&hello=world

To sum up, args are fine if you want cleaner urls, but you can't put whatever you want on them (imagine something like /app/default/test/ùà+:\/
, not a "smart" way to pass garbles on the url, but, e.g., something like /app/default/test/1/on where 1 is the "bulb" number and on is the "action" you want to do seems fine).
"Vars" on the other hand allow more flexibility, "garbled" parameters are allowed...maybe the nicest "feature" is that if you need a single variable holding multiple values "vars" parses them automatically (e.g. "turn on bulb 1 and bulb 2" can be done with /app/default/test?bulb=1&bulb=2&action=on) . Urls are not "clean" as with "args" but the functionality is the same.

Bottomline, use what you feel appropriate, either one would do just fine.

theoffi...@gmail.com

unread,
Apr 2, 2013, 4:57:57 AM4/2/13
to web...@googlegroups.com
Thank you for your reply again :)
Okay, i just did a google search on the term "foobar", and haha i have been wondering why does it appear anywhere!
anyway, hmm, it seems clearer with your examples. so if i were to use args, like you said, to turn on bulb 1, i will put it as something like:

<a href = "{{=URL('test', args =['bulb', '1', 'on'])}}"><img src ="/Comfort2/static/images/on.jpg" width ="75" height="75"></a> ?

in default.py, i put this?

 if request.args(0)=='1':
        serialport.write("\x03O!0101\x0D") # serial command to turn the light on.

and if using vars:

<a href = "{{=URL('test',  vars = dict(bulb='1', state='on' ))}}"><img src ="/Comfort2/static/images/on.jpg" width ="75" height="75"></a> ?

if request.vars ={'bulb':1, 'state' :on}
        serialport.write("\x03O!0101\x0D") # serial command to turn the light on.

what about the following URL? what will it do?

<a href = "{{=URL('test', args =['bulb', '1'], vars = dict(state='on'))}}"><img src ="/Comfort2/static/images/on.jpg" width ="75" height="75"></a>

And all the above are in the same function 'test', so can i omit 'test' in the URL?

thanks again!

Niphlod

unread,
Apr 2, 2013, 6:20:21 AM4/2/13
to web...@googlegroups.com


On Tuesday, April 2, 2013 10:57:57 AM UTC+2, theoffi...@gmail.com wrote:
Thank you for your reply again :)
Okay, i just did a google search on the term "foobar", and haha i have been wondering why does it appear anywhere!
anyway, hmm, it seems clearer with your examples. so if i were to use args, like you said, to turn on bulb 1, i will put it as something like:

<a href = "{{=URL('test', args =['bulb', '1', 'on'])}}"><img src ="/Comfort2/static/images/on.jpg" width ="75" height="75"></a> ?

in default.py, i put this?

 if request.args(0)=='1':
        serialport.write("\x03O!0101\x0D") # serial command to turn the light on.


[bulb, 1, on] "translates" to
request.args(0) = bulb
request.args(1) = 1
etc etc etc (i.e. numbering starts from 0)
 
and if using vars:

<a href = "{{=URL('test',  vars = dict(bulb='1', state='on' ))}}"><img src ="/Comfort2/static/images/on.jpg" width ="75" height="75"></a> ?

if request.vars ={'bulb':1, 'state' :on}
        serialport.write("\x03O!0101\x0D") # serial command to turn the light on.

better to be a little less specific
if request.vars.bulb == '1' and request.vars.state == 'on':
 

what about the following URL? what will it do?

<a href = "{{=URL('test', args =['bulb', '1'], vars = dict(state='on'))}}"><img src ="/Comfort2/static/images/on.jpg" width ="75" height="75"></a>


request.args(0) = bulb
request.args(1) = 1
request.vars.state = 'on'

all of this can be discovered if you turn the response.generic_patterns on and you do (for testing purposes) a simple
def test():
    return dict()

In the generic view there's included "response toolbar" that you can use to "inspect" what's going on (e.g., what request.args is specifically)
Or, if you have a view already, include at the top
{{=response.toolbar()}}
to show it.
 
And all the above are in the same function 'test', so can i omit 'test' in the URL?

See the book on how to use the URL() function.
http://web2py.com/books/default/chapter/29/04#URL

URL() without arguments "points" to the app/controller/function that generated it.
 

Cliff Kachinske

unread,
Apr 2, 2013, 9:03:29 AM4/2/13
to web...@googlegroups.com
You can think of request.args as a Python list.  You can find out more about Python lists here http://docs.python.org/2/tutorial/introduction.html#lists and here http://docs.python.org/2/tutorial/datastructures.html#more-on-lists

You can think of request.vars as a Python dictionary.  Look here: http://docs.python.org/2/tutorial/datastructures.html#dictionaries

The Web2py manual also has an introductory chapter on Python http://web2py.com/books/default/chapter/29/02

Anthony

unread,
Apr 2, 2013, 9:14:14 AM4/2/13
to web...@googlegroups.com
Just to add a bit...

and if using vars:

<a href = "{{=URL('test',  vars = dict(bulb='1', state='on' ))}}"><img src ="/Comfort2/static/images/on.jpg" width ="75" height="75"></a> ?

if request.vars ={'bulb':1, 'state' :on}

Note that in Python, to test equality you use "==", not just a single "=" (which is used for assignment only). Also, note that request.vars (and request itself) is a gluon.storage.Storage object, not a dict (though it inherits from dict and you can in fact test equality with a dict as above). See http://web2py.com/books/default/chapter/29/04#request (response and session are Storage objects as well). request.args is a gluon.storage.List object, which inherits from list.

In the generic view there's included "response toolbar" that you can use to "inspect" what's going on (e.g., what request.args is specifically)
Or, if you have a view already, include at the top
{{=response.toolbar()}}
to show it.

You can also show just the args and vars in your view via:

{{=request.args}}
{{=request.vars}}
 
And all the above are in the same function 'test', so can i omit 'test' in the URL?

web2py needs the application, controller, and function in the URL to know where to route the request, though you can eliminate a default application, controller, and function if desired by properly specifying the routing -- see http://web2py.com/books/default/chapter/29/04#URL-rewrite for details. Even in that case, though, you would still use the URL() function in the same way -- it will automatically take into account any custom routing specified.

If you want to see what the URL() function generates, I recommend starting a web2py shell and trying it there. For a shell:

python web2py.py -S yourapp -M

(The -M tells it to execute the model files for the app -- you'll be in a Python shell with the full web2py environment as well as the models for your app.)

Anthony

Anthony

unread,
Apr 2, 2013, 9:16:04 AM4/2/13
to web...@googlegroups.com
Another question: can i display a jQuery keypad using web2py?

I don't see why not -- show some code and maybe we can help.

Anthony 

theoffi...@gmail.com

unread,
Apr 3, 2013, 11:07:46 PM4/3/13
to web...@googlegroups.com
Thanks Niphlod, Cliff, and Anthony for your helpful replies. :D
I have just successfully controlled a LED lamp after understanding the URL and arguments. I am so happy!!
here are my codes for your inspection :D

in test.html
<a href = "{{=URL('test', args =['bulb','1', 'on'])}}"><img src ="/Comfort2/static/images/on.jpg" width ="75" height="75"></a>
<a href = "{{=URL('test', args =['bulb','1', 'off'])}}"><img src ="/Comfort2/static/images/off.jpg" width ="75" height="75"></a>

in default.py
if request.args(1)== '1' and request.args(2)=='on':
        serialport.write("\x03O!0101\x0D")
elif request.args(1)== '1' and request.args(2)=='off':
        serialport.write("\x03O!0100\x0D")

Attached is a work-in-progress cherrypy script. it has the jQuery keypad (i got it from here http://jsfiddle.net/pjaaar/6Zh2V/ ). i was wondering if i could use jquery in web2py, the web page may seem more interactive. But i don't mind doing 2 different interfaces for cherrypy and web2py. :D Afterall, learning all these are getting more exciting.

remote_control_keypad_new.py

Anthony

unread,
Apr 4, 2013, 12:06:02 AM4/4/13
to web...@googlegroups.com
Attached is a work-in-progress cherrypy script. it has the jQuery keypad (i got it from here http://jsfiddle.net/pjaaar/6Zh2V/ ). i was wondering if i could use jquery in web2py, the web page may seem more interactive.

Yes, you can absolutely use jQuery with web2py. Like Cherrypy, web2py is a server-side framework -- you can do whatever you want with the client-side code. In fact, the web2py scaffolding app includes jQuery, and there's a chapter in the book dedicated to jQuery and Ajax stuff: http://web2py.com/books/default/chapter/29/11. web2py even simplifies Ajax calls a bit via its ajax() function.

Anthony

The Organisation of Secret Shoppers

unread,
Apr 4, 2013, 1:30:34 AM4/4/13
to web...@googlegroups.com
Cool! :D I need to study more on that chapter! Thanks again Anthony :)


--
 
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/ICo5-yFgxmc/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages