Flask learning framework (a)

flask frame (a)

First, first met the Flask (one of the three main frameworks: django, flask, tornado)

    a:web服务(wsgi)   b:模板     c:orm
django:属于同步框架
    a:wsgiref自己写的  b:自己写的  c:自己写的
flask:同步框架
    a:werkzeug       b:jinja2自己写的   c:别人写的
tornado:属于异步框架
    a:自己写的         b:自己写的    c:自己写的

1. What is the Flask: the Flask is written in a python web framework, just a kernel, default dependence two external libraries: jinja2 template engine and WSGI toolset --Werkzeug.

2. Install the flask:

pip install flask

3. Create a program Flask

img

Detailed project directory:

static folder : static files used to store various css, js, images, etc.

templates folder : html template files for existence

app.py : our main file, start the project needs to start the file

Note: The name of the file app.py we can freely name

Master file app.py file code

from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello World!'


if __name__ == '__main__':
    app.run()

Code is split into three parts:

The first part :

from flask import Flask

app = Flask(__name__)

We introduced flask installed package, package by introducing flask Flask class, is the core class Flask Flask, examples of this class Flask obtain an instance object app.

__name__ this special parameters: python corresponding values ​​can be imparted, depending on their variable __name__ module, to our program is (app.py), this value is app.

The second part :

@app.route('/') # 路由
def hello_world():
    return 'Hello World!'

@ App.route () is used to match routes in the flask with decorators to achieve, which is a write-routing way back we will introduce another way.

Here is the route with the view function will be executed on the route matches the view function.

Part III :

if __name__ == '__main__':
    app.run()

app.run () to achieve the flask is running in the development environment, the default ip and port are 127.0.0.1:5000

It took the three parts of the code

Import Flask core classes to instantiate an object app, and then use the app as a decorator match url distributed to the following view function, and then execute the page will trigger the app to call run () method runs the entire project.

Note emphasized: (******)

We'll create a flask Do not create a project that comes with pycharm flask shortcuts real production environment recommends creating an empty python project directly.

Guess you like

Origin www.cnblogs.com/chmily/p/12163424.html