Python Flask of project development [entry] will learn

 

Flask

Flask module installation

pip install flask
  • 1

Flask create a project, of course, if you did not learn python basis, can not learn the flask. We recommended to go to the small series of Python exchange dress: a long time Wu that while the down stream a thought (digital homonym) conversion can be found, there are new Python tutorial projects to be had, only to learn python basis in order to learn flask, can not skip!

Create a file app.py

from flask import Flask  # 导入Flask包

app = Flask(__name__)  # 获取Flask对象,以当前模块名为参数


# 路由默认为(127.0.0.1:5000) @app.route('/') # 装饰器对该方法进行路由设置,请求的地址 def hello_world(): # 方法名称 return 'Hello World!' # 返回响应的内容 if __name__ == '__main__': app.run()

Run Flask

App.py executed by python file

python app.py
  • 1

Service will default from the 127.0.0.1:5000

 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment. Use a production WSGI server instead. * Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit

Operating parameters

  1. Modify parameters in the file change debug, port, host
app.run(debug=True, port='5000', host='127.0.0.1')
  • 1
  1. Using the sys module, the input parameters in the console change parameters
if __name__ == '__main__':
    import sys
    argvs = sys.argv
    app.run(host=argvs[0], port=argvs[1], debug=argvs[2])
python app.py 127.0.0.1 5000 True
  • 1
  1. Use flask-script module package Manger

Flask-Script installation package

pip install flask-script
  • 1

app.py file

from flask import Flask
from flask_script import Manager

app = Flask(__name__)

manager = Manager(app)


@app.route('/') def hello_world(): return 'Hello World' if __name__ == '__main__': manager.run()

Be started in the service console (-h host -p port number -d address whether debug mode, add to True, not add to False)

python app.py runserver -h 127.0.0.1 -p 5000 -d
  • 1

Console debug web pages

When we started the project by flask-script package, you can enter the bug after appearing on page PIN code and then debug, it is very convenient.

Let's add a divide by zero error in the code manually.

from flask import Flask
from flask_script import Manager

app = Flask(__name__)

manager = Manager(app)

@app.route('/') def hello_world(): name = 'TengTengCai' print(1/0) return 'Hello World' if __name__ == '__main__': manager = run()

Run the server

python app.py runserver -h 127.0.0.1 -p 5000 -d
 * Serving Flask app "app" (lazy loading) * Environment: production WARNING: Do not use the development server in a production environment. Use a production WSGI server instead. * Debug mode: on * Restarting with stat * Debugger is active! * Debugger PIN: 157-852-630 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Access Server

Access Server

When we mouse over each line of code will appear a small icon console

Console button

PIN value Click the button, enter the server at startup, display

Enter your PIN

Debugging

Debugging

Guess you like

Origin www.cnblogs.com/chengxuyuanaa/p/12020648.html