Python Web Framework Flask (1) __python

Source: Internet
Author: User

Flask is a Lightweight Web application framework written using Python. Its WSGI(Web Server Gateway Interface) Toolbox uses Werkzeug , and the template engine uses Jinja2 . Its flexible, easy to learn features, especially for beginners to quickly understand the Python web development framework. This paper mainly introduces the characteristics of flask in a simple case. 1. Installation and commissioning flask installation of 1.1 flask

The installation of flask under Windows is very simple, and it automatically installs the various Third-party packages that flask relies on by simply installing it in the Command window using the Pip method.

Pip Install flask

Because I've already installed it before, so if you install it with PIP, it will appear, as shown in the following picture:


As you can see, the use offlask depends on Werkzeug and jinja2 . 1.2 flask Test

Here's a sample of the official document, Hello World, to test flask(Official Document Link: http://flask.pocoo.org/).

The test code looks like this:

#-*-Coding:utf-8-*-
"" "
Created on Thu June 11:53:20 2017

@author: Administrator" "
#从flask库中导 into the flask function from
flask import flask

#创建Flask对象app
app = Flask (__name__)

'
(1) Using the App.route modifier provided by the program instance, the decorated function is registered as a route
(2) Hello_world () function after @app.route ('/'), when the server is accessed through 127.0.0.1:5000, Triggers the server to execute the Hell0_world () function
'
@app. Route ('/')
def hello_world (): Return
    "

At this point in the browser input 127.0.0.1:5000, enter can see Hello,world. The words.

At the same time, the Python IDE prompts you to receive a request from http/1.1, status code 200, to successfully return the request.

2.Flask Application Case

The following flask case can be used to redirect Web pages, changing the content of Web pages dynamically according to user name. The code looks like this:

#-*-Coding:utf-8-*-
"" "
Created on Tue June 13:57:38 2017

@author: Administrator"
"" from

flask Import flask from 
flask Import abort from 
flask Import redirect 

__author__ = "Zch" 
app = Flask (__name__) c9/> @app. Route ('/') 
def index (): Return 
    ' 

The test results are shown in the following illustration:

When you enter a flask user name, the Web page is automatically redirected to http://blog.csdn.net/flysky1991/article/details/73571968

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.