1. Parameter configuration

A profile,

How to configure Flask parameters For the Flask Flask is discussed in this article.

2. Configure parameters

There are many ways to configure the Flask parameters, each of which can achieve the results, and select the appropriate configuration mode in the appropriate scenario.

  • Config file create a new one in file addconfig.cfgConfiguration file. Write the following statement on the configuration file to enable the debugging mode. In this way, when our program fails, we can know how many lines the error is in, so that we can quickly locate the error.
DEBUG = True
Copy the code

Load the configuration file in the program

from flask import Flask

"__name__: Creates a Flask object with the current module name. Flask uses the location of the passed module as its home directory.
app = Flask(__name__)


@app.route('/') #
def hello_world(a):  View function
    print(1/0)
    return 'Hello World! '  # return content


# config file
app.config.from_pyfile('config.cfg')

if __name__ == '__main__':
    app.run()  # run the program
Copy the code

We can see that this program is wrong, because 1/0 in life, 0 is not a divisor, so there will be exceptions. When we visit in the browser, we can see

  • Class configuration
Class configuration
class Config(object):
    DEBUG = True


app.config.from_object(Config)

Copy the code
  • Direct operation Configuration Direct operation applies to scenarios with few parameters.configIt’s essentially a dictionary.
# direct operation
app.config["DEBUG"] = True
Copy the code
  • Configuration on objects
app.debug = True
Copy the code
  • Parameter incoming configuration (Debug parameter only)
app.run(debug=True)  # run the program
Copy the code

3. Obtain the configuration parameters

There are a lot of situations where we want to take configuration parameters and use them in view functions. There are two ways to get it:

from flask import Flask, current_app

"__name__: Creates a Flask object with the current module name. Flask uses the location of the passed module as its home directory.
app = Flask(__name__)


@app.route('/') #
def hello_world(a):  View function
    If you have access to the app object, you can use the app to retrieve the defined configuration parameters
    print(app.config.get('TEST'))
    If you can't get it, you can get it on the current_app agent
    print(current_app.config.get('TEST'))
    return 'Hello World! '  # return content


# direct operation
app.config["DEBUG"] = True
app.config["TEST"] = 'test'

if __name__ == '__main__':
    app.run()  # run the program

Copy the code

Support external access

We modify the method that calls run() to make our server publicly available, as follows:

 # 0.0.0.0 means that any address that represents the machine is accessible
 app.run(host='0.0.0.0', port=5000)  # run the program
Copy the code

This time we can through http://127.0.0.1:5000/ you can visit our website, but can be by their own native IP to visit http://192.168.1.101:5000/. You can run the ifconfig command to view your local IP address.

Welcome to follow my official account: