ga('set', 'anonymizeIp', 1);
This article gives a brief introduction of creating RESTful API using python flask library.
from flask import Flask, render_template, jsonify, request, url_for, redirect, make_response
Flask useful s:
Refer the flask API document.
app = Flask(__name__)
if __name__ == '__main__':
app.run(host="0.0.0.0", port="5000", debug=True)
Configure the ip and port then launch the flask server.
@app.route('/')
def index():
return render_template('index.html')
Below is the html code if you want to get the relative filepath such as the css files and the js files.
Login
In order to get the relative files, we also need to configure CORS setting.
from flask import Flask, render_template, jsonify, request, url_for, redirect, make_response
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(host="0.0.0.0", port="5000", debug=True)
Startup the flask server by command line:python server.py
@app.route('/getData', methods=['GET'])
def getData():
result = {"result": 0,"data":""}
result["result"] = 1
result["data"] = "test"
return jsonify(result)
@app.route('/postData', methods=['POST'])
def postData():
# if POST data is json format
VAL_FROM_JSON = request.json.get('KEY')
# if POST data resource is form submit
VAL_FROM_FORM = request.form.get('KEY')
# if return page is index route
return redirect(url_for('index'))