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. :(
On Monday, July 30, 2012 1:58:22 PM UTC-7, curiouslearn wrote:
>
> 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);
>
>
>
> ==========================================================================
>
> On Mon, Jul 30, 2012 at 1:59 PM, Anthony wrote:
> > 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
> >
> >
> > On Monday, July 30, 2012 1:55:14 PM UTC-4, curiouslearn wrote:
> >>
> >> 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.
> >
> > --
> >
> >
> >
>
--