Statement: the following is my personal understanding. If you find any mistakes or questions, you can contact me for discussion
Flash is a lightweight web development framework written by Armin ronache in 2010 with Python language based on Werkzeug toolbox.
Flash only provides core functions, and almost all other functions need to be expanded. For example, you can operate the database through the expansion of flash Sqlalchemy.
- Flask-SQLalchemy: operation database
- Flask-script: insert script
- Flask-migrate: manage migration database
- Flash session: session storage mode specification
sudo pip install flask==1.0.2
touch helloflask.py vim helloflask.py
Write the following code in the file
#Import the flash class
Exit edit mode and run the program
You can see the following results
This starts a very simple built-in server. This server should be enough for testing, but it may not be enough for production. Now open in the browser127.0.0.1:5000/, you should see Hello flag! word.
The first step to create a flag project is to instantiate the flag object. The following is a detailed explanation of the parameters required to instantiate the object
import_ Name: the package (module) where the flag program is located
static_ url_ Path: static file access path, can not be transferred, the default is:
/ + static_folder
static_ Folder: static file storage folder, can not be transferred, the default is
static_ Host: use the address of the remote host to store the static file. The default is none. When host_ When matching is true, you can use static_ Folder configures the folder where static files are stored
host_ Matching: setting
url_map.host_matchingProperty, which is false by default
subdomain_ Matching: when matching routes, consider the subdomains associated with: data: ` server_ NAME’。 The default is false.
template_ Folder: the folder where the template file is stored. It can not be transferred. The default value is
instance_ Path: by default, the alternate instance path of the application assumes the folder “instance” next to the package or module as the instance path.
instance_ relative_ Config: if the relative file name used to load the configuration is set to true, it is assumed that the file name is relative to the instance path rather than the application root.
root_ Path: by default, flag automatically calculates the application root path. In some cases, this is not possible (for example, if the package is a python 3 namespace package) and needs to be defined manually.
Before running the flash program, we can set the relevant configuration for the flash, such as env (application running in what environment), debug (enable debugging mode) and testing (enable test mode). There are two common ways to achieve this
Load from configuration file:
Create a configuration file
touch config.ini vim config.ini
Write configuration information
DEBUG = True
Select load from configuration file
#Create a flash class object to point to the name of the package where the program is located
Load from configuration object: app.config.from_ object()
Create a configuration class and pass in the class name
In this demo, the entry to start the flash project is app. Run(). The common configuration options are host, port and debug. They are to set the IP address and port number of the running host, and whether to open the debugging mode. When there are debugs in the configuration file and here, the debug here will prevail.
In web development, routing is a program that forwards URLs to corresponding views.
This work adoptsCC agreementReprint must indicate the author and the link of this article