flask-Fundamentals and Core Knowledge

Source: Internet
Author: User
Tags virtual environment
Virtual Environments
    1. Using Pipenv to create a virtual environment and project bindings, install:E:\py\qiyue\flask>python3 -m pip install pipenv
    2. 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 ,
    3. Common pipenv commands, exit exit , enter pipenv shell , install pipenv install 包名 , uninstall pipenv uninstall 包名 , view the dependencies of the installation package pipenv graph ,
    4. 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
    1. 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()
    1. 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
    2. 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
    1. 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
    2. 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)
    1. But the usual case is to use the adorner.
App.run related parameters and flask configuration file
    1. 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)
    1. 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
    1. 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
    1. 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)
    1. 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
    1. The view function returns is actually a response, if this write, at this time there is nothing on the page, the defaultcontent-type = text/html
@app.route('/hello')def hello():    return '
    1. 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('
    1. 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 '

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.