Use python + flask to create an api (the source code is included in the tutorial) and pythonflask
1. Background
OK. Maybe many of my friends often use various APIs like facebook, github, and even api. So many people also want to create their own APIs. There are very few tutorials on the Internet. Today I just made a try and published the method.
First show the effect:
Use the "curl" method to return a json value. You can also try it:
curl -i http://ospafzone.duapp.com/ospaf
2. Code
First, let's talk about the environment, that is, python + flask, which is particularly well configured.
from flask import Flask, jsonifyapp = Flask(__name__)tasks = [ { 'id': 1, 'title': u'OSPA', 'description': u'This is ospaf-api test', 'done': False }, { 'id': 2, 'title': u'Garvin', 'description': u'I am garvin', 'done': False }]@app.route('/', methods=['GET'])def home(): return jsonify({'tasks': tasks})if __name__ == '__main__': app.run(debug=True)
This is the code in the Local running environment. After the code is executed, enter "http: // 127.0.0.1: 5000/" in the browser to see the effect. A json file is returned.
Note:
@app.route('/', methods=['GET'])
This line of code '/' corresponds to the following function definition def home (). The debugging URL is "http: // 127.0.0.1: 5000 /"
For '/osspaf', the corresponding function definition def ospaf (). The debugging URL is "http: // 127.0.0.1: 5000/osspaf"
OK. You should understand it.
/********************************
* This article is from the blog "Li bogarvin"
* Reprinted please indicate the source: http://blog.csdn.net/buptgshengod
**************************************** **/
How to Use ajax in flask in python to automatically refresh the desired webpage without flash
Flask is a good thing. I will try again now. Ajax refresh is simple. You can learn about jquery. The refresh Part of the result you returned is implemented in js. Call the json response of flask.
I just went to the flask tutorial. Here, it has a pattern for flask, which has a chapter that describes how to reference ajax and jquery. You can add the sample code to your program first, and then create a json response result by yourself. Then, you can use js to display the content. I really don't want to construct html and directly return html. It is also possible to use js. writedocument.
What should I do if I want to build a website that is very simple to vote? A little basic knowledge of Python and FLask
Hello, asp php jsp cgi can be selected at will. We recommend asp followed by php