Python's Flask framework installation app

Source: Internet
Author: User

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

Flask is also known as "Microframework". Because it uses a simple core, add other features with extension. Flask does not have a default database, window Validation tool. However, Flask retains the elasticity of amplification and is able to add these features with flask-extension: ORM, window validation tools, file uploads, various open authentication technologies. The latest version number is 0.12.

Installing installation

Execute command prompt cmd under Windows as an administrator. Perform:

Pip Install Flask
Linux or Mac may need 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 folder is: D:\Python27

After the installation is complete. There will be D:\Python27\Lib\site-packages\flask folder;

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

Fast Start Quick Start

Just find a folder, mine is: D:\Python\flask

Create a file: hello.py, content such as the following:

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 folder to: D:\Python\flask

Execute the following command to start the service:

Set Flask_app=hello.pyset Flask_debug=1flask Run

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdgvzdgnzx2ru/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "/>

Command Description:

1, set up the app's startup file;

2, set to debug mode start;

3, implementation;

Suppose you use a Linux class operating system, you need to replace set with export
Upon successful launch, we are able to access the following prompts: Http://127.0.0.1:5000/

If you want to quit, press CTRL + C to be able.

interview: http://127.0.0.1:5000/. We will see output: index Page

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

interview: Http://127.0.0.1:5000/user/%E5%BE%AEwx%E7%AC%91.   You see the output: user Micro-WX laugh

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

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdgvzdgnzx2ru/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "/>

Because we specified: methods=[' POST ']

Routing

Small partners who have used Java SPRINGMVC should

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

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

In the Go language. Be able to use HTTP. Handlefunc ("/trans", trans) //Set up an access route

make your server visible to the outside As you do with server above, you will notice that the server is only able to access it from your own computer. And you can't access it from any other computer on the network.

This is the default value, because in debug mode, the user of the application is able to execute arbitrary python code on the computer.

Assume that you disable the debugger or trust users on the network. It is possible to 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 generally where CSS and JavaScript files are placed.

Simply create a folder named static in the package or in the Modules folder, which will be available at the/static of the application.

For example, I created a static folder under the D:\Python\flask folder, put some static files under it, and then through http://127.0.0.1:5000/static/index.html, I was able to access it.


Let's say so much first, it's really easier 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

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.