samedi 25 juin 2016

How to interface JQuery with Flask?

I'm writing a Google Chrome extension that uses a jQuery.post() call to send data to an external website. The external website handles the data using a Flask endpoint and generates a result. Unfortunately I am not sure how to transfer the result back to the client. How can I do this?

I've tried using a render_template call within Flask, like so:

app.route("/my_endpoint", methods = ['POST'])
def my_endpoint():
    print ('hi')  # this statement prints
    results = ...
    if request.method == 'POST':
        # want to eventually replace this with
        # return render_template("results.html", results=results)
        return render_template("test.html")

But this doesn't actually load the page test.html.

I've also tried transferring the data back to the Chrome extension using a callback (which I would prefer not to do if possible), as in

post_results = function(input_data) {
    jQuery.post("my_flask_endpoint",
        input_data,
        function (data, textStatus, jqXHR) {
            ...
        }

But I'm not sure what to put in the callback function, because it seems like "data" is a bunch of HTML, and I don't know how to load pages given only an HTML string (as opposed to the URL).

Aucun commentaire:

Enregistrer un commentaire