How to do a simple Flask program, and how to do static and template directories. This article will go into the basics of how to configure flask parameters.

App initialization parameters

In the last article, we mentioned that static exists by default in the root directory of the current module, if we don’t add any decorations to the page. But if we need to display some static files on a web page, we need to create a static folder to hold these static files, which should be in the same directory as the current module.

We can also access the HTML file in the static folder static and display the contents of the HTML file directly to the client. The first thing we need to do is to create an HTML file in the static folder static. You can fill in the content as you like.

Open a browser and enter the url in the root /static/.html format to access the corresponding HTML file.

But we can also set some initialization parameters when we instantiate the app object:

  • Import_name: Find the static directory and template directory location
  • Static_url_path: specifies the prefix of the URL used to access static files
  • 3. Static_folder: indicates a static file directory. The default is static
  • Template_floder: template file directory. The default is templates

The import_name argument, if passed __name__, looks for static and template directories in the current module directory. Other arguments can be passed as well, but __name__ is recommended. The second argument is prefixed with a custom static file URL, which defaults to static, but if we change this argument in the app object:

app = Flask(__name__,
            static_url_path='/index')
Copy the code

When accessing the same static file, you must change static to index in the URL to successfully access:

This prefix is also important because we said that the same effect can be achieved with view functions. This prefix is also proof that you are accessing a static file, not a view function.

Leaving the remaining two parameters unchecked will look for folders called static and templates in the current module directory, and if it does, will look for the corresponding static and templates directories in the directory you set.

Debug Parameter Configuration

We usually use the Debug function to Debug the code. When we write Flask program, if there is an error in the code part, the client will give a very general error message, such as HTTP status code. If we add a division by zero error to the code, the client page will look like this:

This makes it harder to find bugs when we know there are errors in the code, but not where they are. In Flask, the DEBUG parameter is also available. What we need to do is to configure this parameter, and the methods are roughly as follows:

  • 1. Use the configuration file
  • 2. Through object configuration
  • 3. Set it in the Config dictionary
  • 4. Set in the run method

Methods a

First we create a file named config.cfg in the same directory as the file and add the following statement:

DEBUG = True
Copy the code

Then go back to the code file and configure it on the app object as follows:

app.config.from_pyfile('config.cfg')
Copy the code

Method 2

Since everything in Python is an object and a class is an object, we can create a class and set DEBUG to a property in that class:

class Config() :
    DEBUG = True
Copy the code

Then configure the app object as well, but change the configuration from file to object:

app.config.from_object(Config)
Copy the code

Methods three

The config in the app application object can be interpreted as a dictionary object. We can also configure the debug parameter directly on this dictionary:

app.config["DEBUG"] = True
Copy the code

It is important to note that this method can be used with a small number of parameters, while too many parameters can lead to too much code, which is complex to write, and reduces the readability of the code.

For the config dictionary object, we can also query the corresponding value based on the known key in the configuration parameter:

print(app.config.get("Known bonds"))
Copy the code

Methods four

The fourth method is probably the simplest, the run() method is used to run the flask program, which also has a debug parameter, which is False by default. When we set it to True, the debug function is enabled:

app.run(debug=True)
Copy the code

The run() method also has other parameters to configure, such as the main path, port number, and so on, which I won’t go into here. You can consult the official documentation if you need it.

After setting the DEBUG parameter in one of the four methods, we run the program again. The information in the Pycharm running bar tells us that the DEBUG function is enabled:

Then go back to the browser and refresh the page. The client will give you the exact code error, telling you that this is a division by zero error, so we just need to find the code:

In summary, this paper mainly introduces two important parameters in flask: initial parameters and debug parameters of app application objects, as well as four basic methods of configuring debug parameters.

References for this article:

[1]. The Flask introductory tutorial. Li Huizhu [2]. www.bilibili.com/video/BV1yt. [3].Flask in English and Chinese

If you are interested in this series, please follow the public account “Buttermilk Cat” for the first time to follow up the follow-up updates ~