Virtual Environments
- Using Pipenv to create a virtual environment and project bindings, install:
E:\py\qiyue\flask>python3 -m pip install pipenv
- and project bindings: Into the project directory
pipenv install
, and then pipenv shell
into the virtual environment, then you can install a variety of packages, for example pipenv install flask
,
- Common pipenv commands, exit
exit
, enter pipenv shell
, install pipenv install 包名
, uninstall pipenv uninstall 包名
, view the dependencies of the installation package pipenv graph
,
- With Pycharm binding, in the virtual environment
pipenv --venv
, a corresponding directory will be displayed, and then select the environment in the pycharm when binding on it, so that the virtual environment is running.
Flask minimum prototype and unique URL principle
- The smallest prototype of the Flaks, at which point the
http://127.0.0.1:5000/hello
return value can be seen.
# -*- coding: utf-8 -*-from flask import Flask# 实例化app = Flask(__name__)@app.route('/hello')def hello(): return 'hello'app.run()
- The only URL principle, at this time if the user access
http://127.0.0.1:5000/hello/
to the URL, because more than a slash, then this page can not be found, if @app.route('/hello/')
there is more than one slash here, you will be able to access the
- However, it is also possible to access without slashes at this time, because a redirect was made at the bottom of the flask
Another way to register a route
- Because every time you modify the code to restart the server to see the effect, so this time to open the debug mode can be
app.run(debug=True)
added on the line, so it is more convenient
- Another way to register a route is to use the app call
add_url_rule()
method.
# -*- coding: utf-8 -*-from flask import Flask# 实例化app = Flask(__name__)def hello(): return 'hello'# 路径,视图函数app.add_url_rule('/hello',view_func=hello)app.run(debug=True)
- But the usual case is to use the adorner.
App.run related parameters and flask configuration file
- At this point in
app.run(debug=True)
this case, only through the 127.0.0.1:5000
access, even if the input of the native IP is not accessible, so this time can be added to this method parameter, at this time in the address bar input http://192.168.2.14:81/hello
is also accessible
# host:指定ip地址,输入本机ip也可以访问了,port是改变默认的端口app.run(host='0.0.0.0',debug=True,port=81)
- If the project on-line, then debug mode can not be opened, because to maintain the consistency of the source code, so at this time try not to delete this debug, this time to write a configuration file, in the directory of a new config.py
# -*- coding: utf-8 -*-DEBUG = True
- This time in the file, first load this file, and then read, note the parameters to capitalize
# -*- coding: utf-8 -*-from flask import Flaskapp = Flask(__name__)# 载入这个配置文件,这里要注意路径,app.config.from_object('config')@app.route('/hello')def hello(): return 'hello'# 读取配置文件中信息app.run(host='0.0.0.0',debug=app.config['DEBUG'],port=81)
Role of If Name
- With this in mind, this run will only execute if this file is used as a portal file.
if __name__ == '__main__': app.run(host='0.0.0.0',debug=app.config['DEBUG'],port=81)
- In the build environment usually does not use the flask comes with the server, but uses
nginx+uwsgi
, at this time this file is not the import file, this time Uwsgi also is a server, if does not add the judgment will have two servers simultaneously, therefore this adds the judgment to be necessary
Response
- The view function returns is actually a response, if this write, at this time there is nothing on the page, the default
content-type = text/html
@app.route('/hello')def hello(): return '
- There are so few important parameters returned,
status code
which content-type
can also be customized, which is returned at this time , because the text parsing used, of course, can also return JSONapplication/json
from flask import Flask,make_response@app.route('/hello')def hello(): # status code 200,404,301,状态码只是一个标识 # content-type http headers,指定了客户端在接收到数据后用什么方式进行解析 # content-type = text/html 默认的 # Response对象 headers = { 'content-type':'text/plain',#文本解析 } response = make_response('
- At this point can also be redirected, just need to change the status code to 301, add a location in the headers can be, but also can write the concise write, so that you can
@app.route('/hello')def hello(): headers = { 'content-type':'text/plain', 'location':'http://www.bing.com' } return '