Python's Flask framework installation app

Source: Internet
Author: User

Flask is a lightweight WEB application framework written using Python. Its WSGI Toolbox uses Werkzeug, and the template engine uses JINJA2, which uses BSD licensing.

Flask is also known as "microframework" because it uses a simple core and adds other features with extension. Flask does not have the default database, form validation tools used. However, Flask retains the elasticity of amplification, which can be added with flask-extension: ORM, form validation tools, file uploads, various open authentication technologies. The latest version is 0.12.

Installing installation

Under Windows, Run command prompt cmd as an administrator, executing:

Pip Install Flask
under Linux or Mac, you might want to use:

sudo pip install Flask

Finished, this is finished, better than Django, environment variables do not need to be configured.

My Python 2.7 installation directory is: D:\Python27

After the installation is complete, there will be D:\Python27\Lib\site-packages\flask directory;

The D:\Python27\Scripts directory will have: Flask.exe

QuickStart Quick Start

Find a catalogue, mine is: D:\Python\flask

Create a file: hello.py, which reads as follows:

From flask Import Flaskapp = Flask (__name__) @app. Route ('/') def index ():    return ' index Page ' @app. Route ('/hello ') def Hello ():    return ' Hello, World ' @app. Route ('/user/<username> ') def show_user_profile (username):    # Show The user profile for that user    return ' user%s '% username@app.route ('/post/<int:post_id> ') def show_post (post_ ID):    # show The post with the given ID, the ID was an integer    return ' post%d '% post_id@app.route ('/buy ', methods= [' POST ']) def buy ():    Stripe_token = request.form[' Stripetoken ']if __name__ = = "__main__":    App.run ()

open cmd, switch directory to: D:\Python\flask

Execute the following command to start the service:

Set Flask_app=hello.pyset Flask_debug=1flask Run

Command Description:

1, set up the app's startup file;

2, set to debug mode start;

3, Operation;

If you use the Linux class operating system, you need to replace the set with the export
After successful launch, we can access the following prompts: Http://127.0.0.1:5000/

If you want to quit, press CTRL + C.

access: http://127.0.0.1:5000/, we will see the output: index Page

access: Http://127.0.0.1:5000/hello, you will see the output: hello, World

visit: Http://127.0.0.1:5000/user/%e5%be%aewx%e7%ac%91,  You see the output: user Micro-WX laugh

and access: Http://127.0.0.1:5000/buy, we will receive a prompt: method is not supported.


Because we have specified: methods=[' POST ']

Routing

Small partners who have used Java SPRINGMVC should

@app. Route ('/buy ', methods=[' POST ')
It feels familiar, it's like requestmapping.

@RequestMapping ("/index") public String Index () {return "Projectpage/index";}

In the go language, you can use HTTP. Handlefunc ("/trans", trans) //Set access routes

make your server visible to the outside as you run the server above, you will notice that the server is accessible only from your own computer and not from any other computer on the network.

This is the default value because in debug mode, the user of the application can execute arbitrary python code on the computer. If you disable the debugger or trust users on the network, you can make the server publicly available by adding--host = 0.0 on the command line. 0.0来:

Flask Run--host=0.0.0.0
Static Files Dynamic Web applications also require static files.

this is usually where CSS and JavaScript files are placed. simply create a folder named static in the package or in the module directory, which will be available at the/static of the application. For example, I created a static folder under the D:\Python\flask directory, put some static files under it, and then I can access it through http://127.0.0.1:5000/static/index.html.


Let's say so much first, it's really easy to get started.

====================== Document Information ===========================

Copyright Disclaimer: Non-commercial free reprint-retain attribution-Annotated source

Attribution (by): TESTCS_DN (micro-wx laughter)

Article source: [Ignorant life, record bit] (HTTP://BLOG.CSDN.NET/TESTCS_DN)

============== This article starts with the personal subscription number (micro-wx laughter) ============

Python's Flask framework installation app

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.