After searching for few hours, I ended up having the following, and it worked well. Hope this helps someone else in future.
HTML and JS code: loging.html:
<html>
<head>
<title>Flask Intro - login page</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="static/bootstrap.min.css" rel="stylesheet" media="screen">
<script type="text/javascript" src="http://code.jquery.com/jquery 2.1.4.min.js"></script>
</head>
<body>
<input id="submitbutton" type="submit" value="Test Send Data">
<!----------------------------------->
<script type="text/javascript">
function runPyScript(input){
var jqXHR = $.ajax({
type: "POST",
url: "/login",
async: false,
data: { mydata: input }
});
return jqXHR.responseText;
}
$('#submitbutton').click(function(){
datatosend = 'this is my matrix';
result = runPyScript(datatosend);
console.log('Got back ' + result);
});
</script>
Python code: app.py:
from flask import Flask, render_template, redirect, url_for,request
from flask import make_response
app = Flask(__name__)
@app.route("/")
def home():
return "hi"
@app.route("/index")
@app.route('/login', methods=['GET', 'POST'])
def login():
message = None
if request.method == 'POST':
datafromjs = request.form['mydata']
result = "return this"
resp = make_response('{"response": '+result+'}')
resp.headers['Content-Type'] = "application/json"
return resp
return render_template('login.html', message='')
if __name__ == "__main__":
app.run(debug = True)
~/reverse_pca.pyis a relative path and it looks like it is meant to be an absolute path. Anyway, you should mention any error messages (because "does not work" can mean many things) and you should watch the web server logs.