Flash from zero to one virtual environment and the first flash program


There are about 20 days of vacation time left. In these 20 days, I am going to follow up a flash introductory series, which will be divided into 10-12 articles. Although I don’t want to do development in the future, the popularity of web development is still very high, so I spent some time learning it. There will also be courses related to web in the next semester. If you are interested in flask, you can continue to follow up. I hope this series can help you.

The first article mainly includes two aspects: one is the preparation before learning flag, such as configuring the corresponding environment and downloading some tools; on the other hand, it is to write the first flash simple program.

Flask supports both versions of Python 2 and python 3, but it was updated to 3.6 when I contacted python, so I didn’t pay attention to Python 2 either. I believe that most of our partners are using Python 3, so this series will be based on Python 3, and the latest version of flash can be used.

Here is only a brief introduction to flash: flash is a micro framework, which is equivalent to a kernel, and only retains the core functions: request response processing and template rendering. These two functions are implemented by Werkzeug and Jinja respectively. The two dependencies are also packaged by flask itself. Then, flask also has many expansion packages. Users can import expansion packages according to their own requirements to realize the corresponding functions. This is also the reason why the framework of flask is flexible.

Git Download

First of all, it is recommended to download git as a tool for Windows users. Although many functions can be realized on the terminal, it is more convenient to use git commands in some aspects, and it can also be used to record the changes of source code and files of the program. For git download and installation tutorial, please refer to this blog: Windows system installation tutorial.

After downloading and installing, find git bash in the search bar, and enter git — version at runtime. If the corresponding version information appears, the installation is successful.

Flash from zero to one virtual environment and the first flash program

Virtual environment configuration

Virtual environment is a Python interpreter environment independent of the python global environment. For example, the interpreter version in one virtual environment can be Python 2 and the interpreter version of another can be Python 3. They will not interfere with each other and will not be affected by the python environment in your computer.

It is necessary to configure the virtual environment, because different programs depend on different versions of the language. However, if you download multiple versions of Python interpreter in a computer, the global environment will be disordered. The virtual environment can solve this problem well and manage our program easily.

The built-in venv module in Python 3 can create a virtual environment. First, enter the specified file through the CD instruction in the CMD of the system. It should be noted that this path cannot contain Chinese, and then use the following instructions to create a virtual environment.

python -m venv env

Flash from zero to one virtual environment and the first flash program

Env is the name of the virtual environment, which can be drawn up by yourself. At this time, a virtual environment folder containing the Python interpreter will be generated in the current directory, and then there are two. Bat files under the scripts file, which control the activation and exit of the virtual environment.

The virtual environment can be activated and exited by the following command:

Env / scripts / activate
Env / scripts / deactivate ා exit

Or you can directly enter the scripts directory and directly enter activate or deactivate. When the following bracket appears in front of the directory, it means that the activation of the virtual environment is successful.


Then you can install flash in the virtual environment through pip. You don’t need to specify the version, and the latest version is installed by default.

 pip install flask

The first flag program

It is very simple to write a flash program, which can be realized by only a few lines of code. The process can be roughly divided into the following five steps:

  • 1. Import the flag class from the flash package
  • 2. Instantiate the flag class to create an application object
  • 3. Define view functions
  • 4. Add decorators to view functions (define routes)
  • 5. Start the flag program
#Import the flag class from the flash package
from flask import Flask
#Create application object of flag
app = Flask(__name__)

def hello():
    "Define view functions"
    return 'Hello NaiTangMao'

if __name__ == "__main__":
    #Start the flag program

Running the above program will give the following results. We will first pay attention to the address of the red letter part, and we will find that this is the IP address of our local host. After running a flash program, it will listen to the port 5000 of the host by default.

Flash from zero to one virtual environment and the first flash program
Copy the web address and open it in the browser, and the information returned in our view function will appear:

Flash from zero to one virtual environment and the first flash program

The process of this flag program is as follows:

  • 1. First, the user starts the program and visits the corresponding website.
  • 2. The server parses the request and identifies the URL bound in the decorator.
  • 3. Match the URL and call the corresponding view function.
  • 4. Gets the return value of the view function and returns it to the client.

For the above lines of code, you need to master several knowledge points. First of all, you should make sure that the app creates an application object after instantiating the flag class, but the__ name__ What’s the effect?

Wei name__ For the current module name, because the flash program needs static files and template files to assist when running, when you set__ name__ After that, flash will take the directory where the current module (file) is located as the root directory. By default, the static directory in this directory is the static directory, and templates is the template directory. Because it exists by default, it will not be reflected in the root directory. It doesn’t matter if you don’t understand it. These two aspects of knowledge will be involved later.

The view function does not need to be introduced too much for the moment, but the decorator on the view should be noted app.route (), this decorator can bind a URL for the view function. When the user accesses this URL, the corresponding view function will be triggered, where ‘/’ represents the root address. We can also set additional paths after the root address, such as app.route (‘/hello’)。

At this time, if we visit the original website, there will be an error prompt of not found, and we must visit it at this timehttp:// trigger the view function.
Flash from zero to one virtual environment and the first flash program
A view function can also bind multiple URLs at the same time. This is achieved by adding multiple decorators to the view function

def hello():
    "Define view functions"
    return 'Hello NaiTangMao'

That is to say, no matter what we visit nowhttp:// : 5000 / or :5000/helloCan trigger this view function.

To sum up, it is the first part of the flare introduction series, which mainly introduces the configuration of virtual environment and the brief analysis of the components of simple flash program.

Reference materials of this paper:
[1] Introduction to flask, written by Li Hui
[3] . flag Chinese and English documents

If you are interested in this series, please pay attention to the official account.