Execute python script -- Ajax and Flask
<p>I apologize if this is unclear or anything like that. I'm very very new to web programming of any kind, so please be patient. When a link is clicked, I want to run a python script and then display the results. What is currently happening is it's just returning the HTML page. I have an <em>idea</em> of why but no clue how to fix it. I believe the issue is with the Flask python code, but please any input is appreciated. I will comment the area I believe to be the problem</p>

<p>Flask (Python) code:</p>

<pre><code>from flask import Flask, render_template
app = Flask(__name__)



@app.route("/")
def index():
    return "Hello, world!"

@app.route('/cgi-bin/cputemp.py', methods=['GET', 'POST'])
#this is where  need to put something, but I don't know what. 
#Without defining this route I was getting a 405 error. I have no idea
#what would go here -- this is just the directory to the python and I 
#thought the routes were for different web pages the user could access.
#Again, I believe *this* is the source of the problem. Obviously 
#right now it's just returning the HTML of the following test() function.


@app.route('/test', methods=['GET', 'POST'])
def test():
    return render_template("test.html")

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000, debug=True)
</code></pre>

<p>test.html</p>

<pre><code>&lt;!DOCTYPE html&gt;

&lt;html&gt;

&lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"&gt;&lt;/script&gt;

&lt;script src="/static/test.js"&gt;&lt;/script&gt; 
&lt;div id="swiss"&gt;&lt;a href="javascript:cputemp2()"&gt;Click to display CPU Temp&lt;/a&gt;&lt;/div&gt;

&lt;/html&gt;
</code></pre>

<p>test.js</p>

<pre><code>function cputemp2()
{
    $.ajax(
    {
        type: "POST",
        url: "cgi-bin/cputemp.py",
        dataType: "html",
        success: function(msg)
        {
        console.log(msg); # It's just returning the HTML of test.html currently
        document.getElementById('swiss').innerHTML = msg;
        },
    });
}    
</code></pre>

<p>cputemp.py</p>

<pre><code>#!/usr/bin/python
import cgi;
import cgitb;
import time
cgitb.enable()
import commands
import sys
import string
print "Content-type: text/html\n\n";
mytemp1 = commands.getoutput('/opt/vc/bin/vcgencmd measure_temp | cut -d "=" -f2 | cut -f1')
output = "Pi CPU Temp is: " + mytemp1
print output
</code></pre>

<p>My question is -- I thought the AJAX code in the test.js file would be handling the call to the python script. All it does is execute the method below the route to that directory in my Flask code. So do I need to run the python code there? How do I do this? </p>

<p>Thank you <em>so</em> much, I'm really  lost and stuck on this.</p>