Python Flask jquery usage notes

Source: Internet
Author: User
Tags tojson

0. PrefaceRecently for some reason again learning flask Framework. Organize related content with blogs. The flask frame and apache+php are slightly different, and the JS and CSS files in the flask frame are stored in a relatively fixed position.

Under normal circumstances, it is located under the static folder (see Figure 1 folder structure).

This paper attempts to illustrate the use of jquery and Ajax in the flask framework with a very easy addition sample.

This sample will be executed in the Raspberry Pi, note that the Windows platform and Linux platform can also perform the demo sample, and Python has good cross-platform performance.

"Code Warehouse"The code warehouse is locatedBitBucket, you can download the ZIP package or clone the code via TORTOISEHG.

"Related blog posts""1" Python extension library installation uses third-party mirror source "2" Python Flask Preschool "3" Front end learning--html4 and HTML5 set page language character set "4" Front end learning--jquery Ajax use Experience
1. Introduction of jqueryJquery.js files need to be stored in the static folder, in the front-end code also need to specify the path of the JS file, these paths are "relative path."

It is strongly recommended to save all the files in UTF8 format, so as not to produce Chinese garbled phenomenon.

"Folder Structure" Figure 1 folder Structure "static folder"--jqury.js "Templates folder"--main.html "flask-jquery.py"
2. Simple Demo Sample"1" front-end Section    "Templates folder"--main.html
<! DOCTYPE html>"Brief description""1" <meta charset= "utf-8" > Web page encoded as UTF8. Please note that the Web page file is also saved as UTF8 format "2" <script src= "{{url_for (' static ', filename= ' Jquery.js ')}}" ></script> Loaded in St            Jquery.js file "3" var $SCRIPT _root = {{Request.script_root|tojson|safe}} in the Atic folder; Sorry, I do not understand the meaning of "4" Now:new Date (). GetTime () A small trick to prevent browser caching."2" Back-end Section"Flask-jquery.py"
#-*-Coding:utf-8-*-from flask Import flask, jsonify, render_template, Requestapp = Flask (__name__) @app. Route ("/") def I Ndex (): # home face    return render_template ("main.html")   @app. Route ('/add ') def add_numbers ():    a = Request.args.get (' A ', 0, type=int)    B = Request.args.get (' B ', 0, Type=int)    return jsonify (result = a + b)   if __ name__== "__main__":    app.run (host = "0.0.0.0", port = 8080, debug = True)
"Brief description" "1" request.args.get (' a ', 0, Type=int) front end is submitted by the Get method, in which the URI extracts the parameters A and the parameter B.    A 0 in the middle is the default value for a and B, and when the function fails, a or B is only equal to 0. "2" app.run (host = "0.0.0.0", port = 8080, debug = True).

All IP addresses are available and the port number is 8080. Instead of the default port of 80.


"3" executed in Raspberry PiTransfer the folder FTP you see in Figure 1 to the Raspberry Pi. Execute flask-jquery.py and you canpython flask-jquery.py
Figure 2 Front-end page
Figure 3 Background Debug output


Python Flask jquery usage notes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.