title: How to communicate between JavaScript and Python in a CGI script
body: <p>I have a CGI script which runs on local host. It has a Python function named compute(argument 1) and prints html tags along with a JavaScript function called onClick which gets called when a button is clicked on the browser. I am not sure how to communicate between JavaScript and Python here. </p>
  
  <p>Here is a snippet of the CGI file </p>
  
  <pre><code>   import cgi
  
     def worker():
        #Does something here and returns an python list called data
     def compute(index) 
         #Does something with data[index] and returns another python list called data2
     print ("Content-type: text/html")
     print ()
     print ("&lt;html&gt;")
     print ("&lt;head&gt;")
     print ("&lt;title&gt;Worker&lt;/title&gt;")
     print("""&lt;script type = "text/javascript"&gt;
     var count = 0;
     function onClick(){
      count = count+1;
       document.getElementById("dis").innerHTML = count; 
      };
    &lt;/script&gt;""")
     print ("&lt;/head&gt;")
     print ("&lt;body&gt;")
    &lt;button  type = "button" class  = "next" onclick = "onClick()" &gt;Next &amp;rarr; &lt;/button&gt;
     print("""Count: &lt;p id = "dis" &gt; 0 &lt;/p&gt;""")
     print ("&lt;/body&gt;")
     print ("&lt;/html&gt;")
  </code></pre>
  
  <p>My question here is how do I make the JavaScript function OnClick() call the python function compute() by passing variable count as a parameter and then storing the resulting list returned by compute() as a JavaScript variable. It would be better if worker function does not have to be called again. It would be fine to even return a single variable in compute(index) rather than a python list and pass it to JavaScript. Thanks in advance :).</p>