Flask learning flask Getting Started

One. Flask of brief

    Flask is based Python development and dependent jinja2 templates and Werkzeug the WSGI a miniature frame and services, for Werkzeug essentially Socket server for receiving http requests and requests preprocessing, then triggers Flask framework, developers based Flask frame It provides functionality to request the appropriate treatment, and returned to the user, if you want to return complex content to the user, it needs jinja2 templates to implement processing template, namely: the template and data rendering, character after rendering string is returned to the user's browser.

    " Micro " (micro) does not mean you need to put the entire Web application into a single Python file (although indeed), it does not mean Flask somewhat lacking in features. Micro framework " micro " means Flask designed to keep the core simple and easy to expand. Flask will not make too many decisions for you - such as which database to use. Those Flask selected - such as how to use a template engine - it is very easy to replace. By everything other than by your hands. Thus, the Flask can perfect match with you.

  By default, the Flask contains no database abstraction layer, form validation, or any other variety of libraries capable of existing functions. However, Flask support to add these features to the application with the extension, as is the Flask , like itself to achieve. Numerous extensions provide database integration, form validation, upload handling, various open authentication and other functions. Flask might be " small " , but it is ready to demand complex production environments and put into use.

two. Flask of its dependencies

1. dependence

When installing Flask , the following software packages will be automatically installed.

  • Werkzeug used to implement the WSGI standard between, applications and services Python interface.

  • Jinja for template language to render the page.

  • MarkupSafe and Jinja share, when rendering the page to avoid untrusted input to prevent injection attacks.

  • ItsDangerous ensure data integrity, data security signs to protect Flask of the session cookie.

  • Click is a command-line application framework. For providing flask commands, and allows you to add custom management command.

2. Optional dependent

The following software packages will not be installed automatically. If installed, Flask will detect the software.

  • Blinker provide support for the signal.

  • SimpleJSON is a quick JSON implementation is compatible with Python's json module. If this software is installed, it will take precedence

Use this software to JSON operate.

  • python-dotenv when running flask command through dotenv provide support to set environment variables.

  • Watchdog provides fast and efficient for the development of heavy-duty server.

Three .flask installation

Use the following command to install in a virtual environment the Flask :

pip install flask

Times [ latest version of the flask Code ]

If you want to use the latest before the official release Flask development version, you can use the following command to install or update the code branch from the primary:

$pip  install  -U https://github.com/pallets/flask/archive/master.tar.gz

four. The easiest flask Applications

Create a hello_world.py file:

from Flask Import the Flask 

App = the Flask ( the __name__ ) 


@ app.route ( " / " )
 DEF the hello_world ():
     return  " ! Hello World " 


IF  the __name__ == ' __main__ ' : 
    app.run (Host = ' 0.0.0.0 ' , = 5000 Port, Debug = True)
     # run hello_world.py Python 
# using the following url to access the results can be printed 
# http://127.0.0.1:5000/

 

Access results:

The simplest explanation of the application:

1. First, we imported the Flask class. Instances of the class will be our WSGI applications.

2. Then we create an instance of the class. The first parameter is the name of the application or the module package. If you use a single module (as in this case), you should use __name__ , because the name will change or as a module imports occur depending on the module by using the application mode (probably '__main__' , it may be practical to import name). This parameter is required, such Flask to know where to find templates and static files and other things where.

3. Then we use the route () decorator to tell Flask trigger function of the URL of .

4. The function name is associated for generating the URL . Finally, the function returns the information to be displayed in the user's browser.

 

Write a simple exercise Index Page :

code show as below:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = "hsz"

from flask import Flask

app = Flask(__name__)


@app.route("/index")
def index():
    return "Index Page!"


if __name__ == "__main__":
    app.run(host='127.0.0.1', port=5000)
  #运行后使用下面url访问
  # http://127.0.0.1:5000/index

Guess you like

Origin www.cnblogs.com/hszstudypy/p/12099232.html
Recommended