Search code examples
javascriptjquerypythonmako

Python Javascript framework to use with cherrypy and Mako


I am just learning python and python web apps so please forgive my lack of knowledge.

I am writing a web app in python with cherrypy + mako (+ html), and now I am diving into the javascript part of it. Honestly, the rest was already enough to learn... So I was wondering about using a python framework to sort of abstract the javascript syntax from me. (I am mostly a c* dev, python is already different enough to keep me busy)

I don't (yet) need any fancy javascript, although I have already started using jquery and jquery-ui, but I don't understand the syntax well enough and it takes me a long time to do anything... (For example I have not yet found how to get the javascript and mako to interact with each other in a client server way.)

I have learned about pyjamas and other frameworks as such. What would you recommend?


Solution

  • It all depends on what you need the server and client to communicate about. If you give us a specific problem we can give more specific answers.

    Here's a javascript ajax call. It's straight javascript (no framework) and what you would refer to as "not fancy".

    function AddToCart_Clicked(SKU, Quantity)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        if(window.XMLHttpRequest)
            xmlhttp=new XMLHttpRequest();
        else// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById('HeaderShoppingCartlink').innerHTML = 'Shopping Cart (' + xmlhttp.responseText + ')';
            }
        }
    
        xmlhttp.open("GET","/ProductNavigation/AddToCartJS?SKU=" + SKU + "&Quantity=" + Quantity, true);
        xmlhttp.send();
        }
    

    This function adds an item to a user's shopping cart. The function sends a GET request to the Cherrypy function AddToCartJS in the class ProductNavigation with the variables SKU and Quantity. The CherryPy function looks like this.

    @cherrypy.expose
    def AddToCartJS(self, SKU, Quantity):
        CurrentShoppingCart = ShoppingCart()
        CartID = CurrentShoppingCart.AddToCart(SKU, Quantity)
        return CurrentShoppingCart.GetCartCount()
    

    Hope this helps.

    Andrew