Trying to invoke python function from js using jQuery and Flask
<p>I'm trying to design an interactive website for a presentation I'm making. I'm new at all this flask stuff, and I'm having some hard time understanding why my code doesnt work.</p>

<p>I'm trying to create a website, that every line in it is clickable, and on click I want to bind a python function that according to the text of the line do some calculation and replaces that text.
For that I have build a simple html:</p>

<pre><code>&lt;!doctype html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
  &lt;meta charset="utf-8"&gt;
  &lt;title&gt;click demo&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;


&lt;p&gt;2016-04-21 09:12:59&lt;/p&gt;
&lt;p&gt;Second Paragraph&lt;/p&gt;
&lt;p&gt;2016-04-21 09:12:59 bla bla&lt;/p&gt;

&lt;script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"&gt;&lt;/script&gt; 
&lt;script&gt;

$( "p" ).click(function() {
  $.getJSON('/background_process', {
        line: $(this).text(),
    }, function(data) {
        $(this).text(data.result);
    });
        return false;
});

&lt;/script&gt;

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

<p>I also wrote flask code that should be binded once I click some line on my web page:</p>

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


@app.route("/")
def index():
    return render_template('index.html')

@app.route("/background_process")
def background_process():
    try:
        lang = request.args.get('line', 0, type=str)
        string = lang.split()[0]
        if re.match(r'\d\d\d\d-\d\d-\d\d',string):
            return jsonify(result=string)
        else:
            return jsonify(result="Doesnt start with a date")
    except Exception as e:
        return str(e)
if __name__ == "__main__":
    app.run()  
</code></pre>

<p>The problem is that the function does not work for some reason unless i'm specifying which spot to put the text by id of a slot in my html page, and I cant understand what went wrong.</p>

<p>You have two problems in your app. 
The first one is that you are not importing the request module:</p>

<pre><code>from flask import Flask, jsonify, request
</code></pre>

<p>Second, in your template, you are referring to <code>this</code> that is out of scope. 
This should work:</p>

<pre><code>$( "p" ).click(function() {                                                                               
  var this_p = this;                                                                                      
  $.getJSON('/background_process', {                                                                      
        line: $(this_p).text(),                                                                           
    }, function(data) {                                                                                   
       $(this_p).text(data.result);                                                                       
    });                                                                                                   
        return false;                                                                                     
}); 
</code></pre>

javascript jquery python flask