passing javascript variables to python code in web2py view

5,130 views
Skip to first unread message

Curiouslearn

unread,
Jul 30, 2012, 1:55:14 PM7/30/12
to web...@googlegroups.com
Is it possible to pass a javascript variable to python code in a
web2py view? The only questions and answers I have seen this in the
forum are about how to do this for the URL() function. I want to know
if this is possible for the "for" loop.

For example:

<script>
var numrows = 5;

{{for i in range(numrows):}}
//do something here
{{pass}}

</script>

The above does not work. Is there a way that above can be made to
work? If so, can above work for cases in which the javascript variable
is an array or a json object.

Thank you.

Anthony

unread,
Jul 30, 2012, 1:59:58 PM7/30/12
to web...@googlegroups.com
What are you trying to do? Note, the Python code in the view is executed on the server side, before the HTML page is sent to the browser. The Javascript code is executed by the browser itself once the HTML page has been rendered. Once the page is in the browser, no Python code can run there -- your Python code and Javascript cannot iteract. You can use Python to write Javascript on the server, but the Python cannot dynamically generate or change the Javascript in the browser.

Anthony

Curiouslearn

unread,
Jul 30, 2012, 4:58:22 PM7/30/12
to web...@googlegroups.com
Anthony, thanks for your response. Please see the response below.

I was trying to do the following. Read data from a database and add it
to a table on the html page using ajax. I am using the standard ajax
functions available in jquery. The controller function sends the
database output in the form of json. I wanted to add this output to
the table. Currently, I am just using javascript to do so, but I
thought that for a more complicated scenario, it may be useful if I
could use python. In hindsight, I should have tried to see if python
can directly read the json format output sent by the controller
function. Please see the relevant controller function and the part of
the view (with some comments added to explain what I was trying to
do).

Relevant controller function that is called by the Ajax code
-------------------------------------------------------------------------
import simplejson as sjson

def returndatafromdb():
con, c = connecttodb()
c.execute('select * from results')
results = c.fetchall()
c.close()
con.close()
if len(results) > 0:
return sjson.dumps(dict(results=results))


Relevant part from the view
----------------------------------

<script type="text/javascript">
// Setting width of the table
$("#resultstable").attr('width', "300px");
// Aligning text to center
$("table").css("text-align", "center");

$.ajaxSetup({
cache: false
});

var loadurl = "{{=URL('default', 'returndatafromdb')}}";

var numrows = 0;
var getdata = function(){
$.post(
loadurl,
null,
function(serverresponse){ // serverresponse is in json format
var results = serverresponse.results;

// Instead of following javascript, I was wondering if
I could use
// web2py TR() and TD() helpers. But for that I need
to read "results"
// which is a javascript variable.

if (results.length > numrows) {
for (var i=0; i < results.length; i++) {
tablerow = "<tr><td>" + results[i][1] + "</td>" +
"<td>" + results[i][2] + "</td>" + "</tr>";
$(tablerow).appendTo("#resultstable");
};
};
numrows = results.length;
},
"json"
);
};

var checkdatabase = function(){
getdata();
setTimeout(checkdatabase, 6000);
}

$('#b1').click(checkdatabase);



==========================================================================
> --
>
>
>

howesc

unread,
Aug 1, 2012, 1:47:04 PM8/1/12
to web...@googlegroups.com
in these cases i do all the python on the server side and format the resulting JSON in a way that i can handle easily in javascript.  we don't yet have web browsers that run python. :(

Vincenzo Ampolo

unread,
Aug 1, 2012, 2:31:59 PM8/1/12
to web...@googlegroups.com
On 07/30/2012 10:55 AM, Curiouslearn wrote:
> The above does not work. Is there a way that above can be made to
> work? If so, can above work for cases in which the javascript variable
> is an array or a json object.

Make an ajax call using $.getScript() encoding the variables you want in
the request as args or vars like

$.getScript({{=URL('gen_javascript')}}/my_js_variable1/my_js_variable2)

your server side gen_javascript function will generate the correct
javascript and $.getScript() will run it.

--
Vincenzo Ampolo
http://vincenzo-ampolo.net
http://goshawknest.wordpress.com

Vincenzo Ampolo

unread,
Aug 1, 2012, 2:40:24 PM8/1/12
to web...@googlegroups.com
On 08/01/2012 11:31 AM, Vincenzo Ampolo wrote:

>
> $.getScript({{=URL('gen_javascript')}}/my_js_variable1/my_js_variable2)

sorry. It shoudl be

$.getScript("{{=URL('gen_javascript')}}"+"/"+my_js_variable1+"/"my_js_variable2)

not tested though. so other problem may appear. but I hope I've given
you the concept to do that...

Niphlod

unread,
Aug 1, 2012, 2:42:05 PM8/1/12
to web...@googlegroups.com
I usually do the following:

controller.py

def function():
   
....
   
....
    jhelper
= dict(myvar='1', myothervar='2')
   
return dict(whatever=whatever, jhelper=XML(json(jhelper)))



in view controller/myfunction.html

.....
<script type="text/javascript">
<!--
jhelper
= {{=jhelper}};
console
.log(jhelper.myvar)
// -->
</script>

So I can use inside the javascript code the object containing all the vars

Curiouslearn

unread,
Aug 5, 2012, 11:46:41 AM8/5/12
to web...@googlegroups.com
Thanks very much to all for your suggestions. I will have to try these
different ways as they are not immediately obvious to me (I am new to
many of these technologies).

Thanks again.
> --
>
>
>
Reply all
Reply to author
Forward
0 new messages