5

When pressing a button i call a javascript function in my html file which takes two strings as parameters (from input fields). When the function is called i want to pass these parameters to my flask file and call another function there. How would i accomplish this?

The javascript:

<script>
    function ToPython(FreeSearch,LimitContent)
    {
        alert(FreeSearch);
        alert(LimitContent);
    }
</script>

The flask function that i want to call:

@app.route('/list')
def alist(FreeSearch,LimitContent):
    new = FreeSearch+LimitContent;
    return render_template('list.html', title="Projects - " + page_name, new = new)

I want to do something like "filename.py".alist(FreeSearch,LimitContent) in the javascript but its not possible...

Bonbin
  • 305
  • 1
  • 6
  • 19

1 Answers1

3

From JS code, call (using GET method) the URL to your flask route, passing parameters as query args:

/list?freesearch=value1&limit_content=value2

Then in your function definition:

@app.route('/list')
def alist():
    freesearch = request.args.get('freesearch')
    limitcontent = request.args.get('limit_content')
    new = freesearch + limitcontent
    return render_template('list.html', title="Projects - "+page_name, new=new)

Alternatively, you could use path variables:

/list/value1/value2

and

@app.route('/list/<freesearch>/<limit_content>')
def alist():
    new = free_search + limit_content
    return render_template('list.html', title="Projects - "+page_name, new=new)
Jérôme
  • 13,328
  • 7
  • 56
  • 106
  • Thank! How do i add `/list?freesearch=value1,limit_content=value2` to my javascript? – Bonbin Oct 12 '16 at 10:36
  • AFAIU, what you want to do is execute a GET (or POST, that's up to you) from your JS code to your python flask app. For how to execute the request from JS, see [this answer](http://stackoverflow.com/a/4033310/4653485). – Jérôme Oct 12 '16 at 10:38