How to use ajax in Brython

I am writing a web application using Flask and would like to use the browser.ajax function in Brython, but could not find a suitable example. It would be really nice if someone showed a short example of how to use ajax in Brython. More specifically, how to transfer the data entered by the user in the text box to the text box by clicking the submit button. Any help is much appreciated!


(I write this a few weeks after I posted the question above). I followed this tutorial on how to implement ajax in Flask ( http://runnable.com/UiPhLHanceFYAAAP/how-to-perform-ajax-in-flask-for-python ) and tried to replace jquery.ajax with Brython. Unfortunately, I still can't get it to work. Here is my code:

Flask:

@app.route('/') def index(): return render_template('index.html') @app.route('/_add_numbers') def add_numbers(): a = request.args.get('a', 0, type=int) b = request.args.get('b', 0, type=int) return jsonify(result=a + b) 

britt / html:

  <body onload="brython()"> <script type="text/python"> from browser import document as doc from browser import ajax def on_complete(req): if req.status==200 or req.status==0: doc["txt_area"].html = req.text else: doc["txt_area"].html = "error "+req.text def get(url): req = ajax.ajax() a = doc['A'].value b = doc['B'].value req.bind('complete',on_complete) req.open('GET',url,True) req.set_header('content-type','application/x-www-form-urlencoded') req.send({"a": a, "b":b}) doc['calculate'].bind('click',lambda ev:get('/_add_numbers')) </script> <div class="container"> <div class="header"> <h3 class="text-muted">How To Manage JSON Requests</h3> </div> <hr/> <div> <p> <input type="text" id="A" size="5" name="a"> + <input type="text" id ="B" size="5" name="b"> = <textarea type="number" class="form-control" id="txt_area" cols="10" rows = '10'></textarea> <p><a href="javascript:void();" id="calculate">calculate server side</a> </div> </div> </body> </html> 

What I get is the "result": 0. It looks like brython is not sending data to the flag viewer, but I don't know how to fix it. So, it would be great if someone could indicate exactly what I am doing wrong.

+5
source share
2 answers

In your example, the Ajax request is sent using the GET method. In this case, the send () argument is ignored: the data should be sent to the query string appended to the URL

Brython code should be:

 def get(url): req = ajax.ajax() a = doc['A'].value b = doc['B'].value req.bind('complete',on_complete) # pass the arguments in the query string req.open('GET',url+"?a=%s&b=%s" %(a, b),True) req.set_header('content-type','application/x-www-form-urlencoded') req.send() 

If you want to use the POST method, you can save the Brython code as it is, but the flask code must be changed: you must indicate that the function is processing the POST request, and you get the arguments with the "form" attribute instead of "args":

 @app.route('/_add_numbers_post', methods=['POST']) def add_numbers_post(): a = request.form.get('a', 0, type=int) b = request.form.get('b', 0, type=int) return jsonify(result = a+b) 
+5
source

I am working on this - nothing is ready, but writing Python code makes it very painless.

I can’t publish the code I'm working on (and this is far from minimal), but basically you write a Python (P) function to iterate over HTML or form DOM and compile everything that has β€œvalue” in the json-nish structure (dictionary with nested dicionaries and lists as desired). They simply use the browser.ajax object as described in http://brython.info/doc/en/index.html# and pass the object with your data as a parameter to the submit method.

The object data will be bound by URLs in the request body. You just need to decrypt it from there to client side JSON.

As an additional hint: I did not go into this question, but it seems to me that the default URL may not express everything that is possible in JSON. So, the imported brython json module, and execute the send command as follows:

ajax_object.send({"data": json.dumps(json_data)})

This allows me to do this on the client side: json_data = json.loads(urllib.unquote(request.body).split(":",1)[-1] )

("request.body" from Pyramid - for a flask it is "request.data", but only if the conte type is not understood by the flask - check How to get the data received in the Flask request )

+2
source

Source: https://habr.com/ru/post/1203313/


All Articles