Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 continually updated as it receives user input and it'd be a massive pain in the ass to have it reload the page every time something happens. I know I can do {{ function() }} inside the javascript but how do I do {{ function(args) }} inside the javascript using js variables? So far the only thing I can think of is to update an external database like MongoDB with the js then use Python to read from that, but this process will slow down the website quite a lot.

The jQuery needs to get a list of dictionary objects from the Python function which can then be used in the html. So I need to be able to do something like:

JS:

var dictlist = { getDictList(args) }; dictlist.each(function() {     $("<.Class>").text($(this)['Value']).appendTo("#element"); }); 

Python:

def getDictList(args):     return dictlistMadeFromArgs 
like image 984
Sneaky Beaver Avatar asked May 01 '15 13:05

Sneaky Beaver


People also ask

How do you transfer data using a Flask?

Flask sends form data to template Flask to send form data to the template we have seen that http method can be specified in the URL rule. Form data received by the trigger function can be collected in the form of a dictionary object and forwarded to the template to render it on the corresponding web page.

Can JavaScript be used with Flask?

As I'm sure you know, the reason is that JavaScript is the only language that runs natively in web browsers. In Chapter 14 you saw me add a simple JavaScript enabled link in a Flask template to provide real-time language translations of blog posts.

Can JavaScript and Python work together?

Answer: No, Python cannot replace Javascript. In fact, the two languages complement each other. Javascript is used as a client-side scripting language, whereas Python is mostly used as a server-side scripting language.

How to get data from JavaScript to Python with flask?

1 Answer 1. 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.

How to pass selected post to JavaScript in flask?

This is how I pass my selected post into Javascript. Now from Javascript to Flask. function myFunction (x) { $.getJSON ($SCRIPT_ROOT + '/check_selected', { post: x }, function (data) { var response = data.result; console.log (response); } }); }

How to return the result from flask function using JSON?

Now from Javascript to Flask. function myFunction (x) { $.getJSON ($SCRIPT_ROOT + '/check_selected', { post: x }, function (data) { var response = data.result; console.log (response); } }); } This is how I return the result from flask by using JSON.

How do I create a simple flask app using FETCH API?

This article covers the creation of a simple flask app that can serve data to a web interface using the Fetch API. We start by building a repository containing an empty templates folder and an app.py file. Our app.py file contains the data required to create a web interface. To do this we use the flask ( pip install flask ) python library.


Video Answer


2 Answers

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/<jsdata>') 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/<javascript_data> with an AJAX GET as follows:

$.get( "/getmethod/<javascript_data>" ); 

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/<jsdata>') 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.

like image 157
Alper Turan Avatar answered Sep 18 '22 10:09

Alper Turan


.html

... id="clickMe" onclick="doFunction();"> 

.js

    function doFunction()     {         const name = document.getElementById("name_").innerHTML          $.ajax({             url: '{{ url_for('view.path') }}',             type: 'POST',             data: {                 name: name             },             success: function (response) {             },             error: function (response) {             }         });      }; 

.py

@app.route("path", methods=['GET', 'POST']) def view():     name = request.form.get('name')     ... 
like image 35
Ayse Avatar answered Sep 19 '22 10:09

Ayse