How do I send data from JS to Python with Flask?

后端 未结 1 1165
暗喜
暗喜 2020-12-07 18:03

I\'m making a website with Flask and I\'d like to be able to execute python code using data from the page. I know that I can simply use forms but it\'s a single page that is

1条回答
  •  隐瞒了意图╮
    2020-12-07 18:34

    To get data from Javascript to Python with Flask, you either make an AJAX POST request or AJAX GET request with your data.

    Flask has six HTTP methods available, of which we only need the GET and POST. Both will take jsdata as a parameter, but get it in different ways. That's how two completely different languages in two different environments like Python and Javascript exchange data.

    First, instantiate a GET route in Flask:

    @app.route('/getmethod/')
    def get_javascript_data(jsdata):
        return jsdata
    

    or a POST one:

    @app.route('/postmethod', methods = ['POST'])
    def get_post_javascript_data():
        jsdata = request.form['javascript_data']
        return jsdata
    

    The first one is accessed by /getmethod/ with an AJAX GET as follows:

    $.get( "/getmethod/" );
    

    The second one by using an AJAX POST request:

    $.post( "/postmethod", {
        javascript_data: data 
    });
    

    Where javascript_data is either a JSON dict or a simple value.

    In case you choose JSON, make sure you convert it to a dict in Python:

    json.loads(jsdata)[0]
    

    Eg.

    GET:

    @app.route('/getmethod/')
    def get_javascript_data(jsdata):
        return json.loads(jsdata)[0]
    

    POST:

    @app.route('/postmethod', methods = ['POST'])
    def get_post_javascript_data():
        jsdata = request.form['javascript_data']
        return json.loads(jsdata)[0]
    

    If you need to do it the other way around, pushing Python data down to Javascript, create a simple GET route without parameters that returns a JSON encoded dict:

    @app.route('/getpythondata')
    def get_python_data():
        return json.dumps(pythondata)
    

    Retrieve it from JQuery and decode it:

    $.get("/getpythondata", function(data) {
        console.log($.parseJSON(data))
    })
    

    The [0] in json.loads(jsdata)[0] is there because when you decode a JSON encoded dict in Python, you get a list with the single dict inside, stored at index 0, so your JSON decoded data looks like this:

    [{'foo':'bar','baz':'jazz'}] #[0: {'foo':'bar','baz':'jazz'}]
    

    Since what we need is the just the dict inside and not the list, we get the item stored at index 0 which is the dict.

    Also, import json.

    0 讨论(0)
提交回复
热议问题