Use python + flask to create an api (the source code is included in the tutorial) and pythonflask

Source: Internet
Author: User

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

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.