This is the 27th day of my participation in the More Text Challenge. For more details, see more text Challenge


preface

Familiar with Flask project creation and operation and some configuration information.

Before introducing Flask, what kind of development environment do we need to prepare


Preparing the development Environment

Name of the environment version
Python 3.7.9
Pycharm pro 2020.2.3 (Professional Edition)
Flask 1.1.2


Refer to the article

  • Python environment installation tutorial

  • PyCharm Professional edition download and perfect use

We will use this environment for the rest of Flask development, and any new libraries or environments will be explained in this article.


Create the 01-FlaskUse project

Open PyCharm Pro by selecting File -> New Project and then select Flask Project in the window that pops up.


It is recommended that you create a new directory for your Flask project.


Flask Project Structure


catalogue

Directory/file role
static Storing static files
templates Saving a template file
app.py Flask program


runFlaskproject

Flask integrates with the server for development debugging, so we can run the Flask program directly on the development server.


Pycharm run



The default runs at http://127.0.0.1:5000/. Hold down the Ctrl key and click on the url with the mouse to jump to the browser to show the results.


The Flask program is running, and some information is printed in Pycharm, so let’s take a look

FLASK_APP = app.py
FLASK_ENV = development
FLASK_DEBUG = 0In folder C:/Users/Administrator/Desktop/FlaskDemo/01-FlaskUse D:\Hui\VirtualEnv\01-FlaskUse\Scripts\python.exe -m flask  run * Serving Flask app"app.py"
 * Environment: development
 * Debug mode: off
 * Running on http://127.0. 01.:5000/ (Press CTRL+C to quit)
127.0. 01.- -22/Apr/2021 22:15:55] "The GET/HTTP / 1.1" 200 -

Copy the code


information instructions
FLASK_APP = app.py The Flask app launcher is app.py
FLASK_ENV = development Flask is in a development environment
FLASK_DEBUG = 0 Debug Indicates that the debugging mode is disabled. 0 indicates that the debugging mode is disabled. 1 indicates that the debugging mode is enabled

These are the environment variables that Pycharm professional uses to create a Flask project.


# Flask project file directory
In folder C:/Users/Administrator/Desktop/FlaskDemo/01-FlaskUse
    
Run flask using the Python interpreter
D:\Hui\VirtualEnv\01-FlaskUse\Scripts\python.exe -m flask run
Copy the code

The Python -m argument means to run the Python module in the library as a script.


Script instruction run

So we can also start the flask program in the Pycharm terminal using Python -m flask run or flask run.


The Python -m flask run script instruction runs the flask program that is working in the production environment, and it raises a warning

WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
Copy the code

This is a development server. Do not use it in a production deployment. Switch to a production WSGI server.

Flask provides a Web server that is not suitable for use in a production environment.


The Flask application is created in a normal project

Since Pycharm Professional can directly select the creation of a Flask project, it automatically installs the environment required by the Flask application.

Check the environment required for Flask with the PIP freeze command

It turns out that we’re using Flask version 1.1.2

So we can build Flask applications in normal Python projects as well

  • Prepare the Python virtual environment first, or use the one provided by Pycharm.

  • Install Flask frame

    pip3 install flask==1.12.
    Copy the code
  • Create a hello. Py

# hello.py

from flask import Flask


Create flask application
app = Flask(__name__)


Create view function
@app.route('/')
def index() :
    return 'index page! '


@app.route('/hello')
def hello() :
    return '<h1> Hello Flask! </h1>'


if __name__ == '__main__':
    # Flask app running
    app.run()

Copy the code

App = Flask(__name__) An instance of the Flask class is created with only one parameter that must be specified, the name of the program’s main module or package.

A function like hello() decorated with app.route() is called a view function. App.route (‘/’) takes a path that the Web program accesses, and the response returned by the view function can be a simple string containing HTML or a complex form.

A program that handles the relationship between a URL and a function is called a route

In the same way as before, right click to run, the script command python -m flask run. After the successful operation of the browser, respectively input

http://127.0. 01.:5000

http://127.0. 01.:5000/hello
Copy the code


The source code

The source code has been uploaded to Gitee HuiDBK/FlaskBasic – Code Cloud – Open Source China (gitee.com), welcome to visit.

✍ code word is not easy, still hope you heroes support ❤️.


The public,

Create folder X

It took nature tens of billions of years to create our real world, and programmers hundreds of years to create a completely different virtual world. We hammer out bricks with keyboards and build everything with brains. People see 1000 as the authority, we do the opposite and defend 1024. We are not keyboard heroes, we are just extraordinary builders of the ordinary world.