[Flask] Flask environment and application

Flask environment

Flask installation conditions

Installing Flask usually requires Python 2.6 or higher. Although Flask and its dependencies are suitable for Python 3 (Python 3.3 and above), many Flask extensions cannot support it correctly. Therefore, it is recommended to install Flask on Python 2.7.

Install virtualenv for development environment

virtualenv is a virtual Python environment builder. It can help users create multiple Python environments in parallel. Therefore, it can avoid compatibility issues between libraries of different versions.

The following command is used to install virtualenv:

pip install virtualenv

This command requires administrator rights. You can add  sudo  before  pip  on Linux/Mac OS . If you are using Windows, please log in as an administrator. On Ubuntu, virtualenv can be installed using its package manager.

Sudo apt-get install virtualenv

After installation, a new virtual environment will be created in the folder.

mkdir newproj
cd newproj
virtualenv venv

To activate the corresponding environment on  Linux/OS X  , use the following command:

venv/bin/activate

To activate the corresponding environment on  Windows  , you can use the following command:

venv\scripts\activate

We are now ready to install Flask in this environment:

pip install Flask

The above commands can be run directly and do not require a system-wide virtual environment.

Flask application

In order to test the  Flask  installation, enter the following code into  Hello.py in the editor :

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
   return 'Hello World’

if __name__ == '__main__':
   app.run()

The Flask module must be imported into the project. One object of the Flask class is our WSGI application.

The Flask constructor uses the name of the current module (__name__) as a parameter.

The route() function of the Flask class is a decorator that tells the application which URL should call the related function.

app.route(rule, options)
  • The rule  parameter indicates the URL binding to the function.

  • options  is a list of parameters to be forwarded to the base Rule object.

In the above example, the'/' URL is bound to the hello_world() function. Therefore, when the homepage of the web server is opened in a browser, the output of this function will be presented.

Finally, the run() method of the Flask class runs the application on the local development server.

app.run(host, port, debug, options)

All parameters are optional

Serial number Parameters and description
1

host

The host name to be monitored. The default is 127.0.0.1 (localhost). Set to "0.0.0.0" to make the server available externally

2

port

The default value is 5000

3

debug

The default is false. If set to true, provide debugging information

4

options

To be forwarded to the underlying Werkzeug server.

The Python script given above is executed from the Python shell.

Python Hello.py

Messages in the Python shell inform you:

* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Open the above URL (localhost:5000) in a browser . The "Hello World" message will be displayed .

 

Note here that if you use python's built-in idle operation, the following error may be reported: 

Traceback (most recent call last): 

File “C:/learn/python/xuexi/web/demoflask/app.py”, line 27, in 

app.run(); 

File “C:\Users\zhang\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py”, line 938, in run 

cli.show_server_banner(self.env, self.debug, self.name, False) 

File “C:\Users\zhang\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\cli.py”, line 629, in show_server_banner 

click.echo(message) 

File “C:\Users\zhang\AppData\Local\Programs\Python\Python36\lib\site-packages\click\utils.py”, line 217, in echo 

file = _default_text_stdout() 

File “C:\Users\zhang\AppData\Local\Programs\Python\Python36\lib\site-packages\click_compat.py”, line 621, in func 

rv = wrapper_func() 

File “C:\Users\zhang\AppData\Local\Programs\Python\Python36\lib\site-packages\click_compat.py”, line 385, in get_text_stdout 

rv = _get_windows_console_stream(sys.stdout, encoding, errors) 

File “C:\Users\zhang\AppData\Local\Programs\Python\Python36\lib\site-packages\click_winconsole.py”, line 261, in _get_windows_console_stream 

func = _stream_factories.get(f.fileno()) 

io.UnsupportedOperation: fileno

 

As long as there is no idle execution, there will be no errors, and subsequent use will not be affected. It can be successful if you switch to Python execution under cmd or pycharm.

Debug mode

Start the Flask application by calling the run() method . However, when the application is under development, it should be restarted manually for each change in the code. To avoid this inconvenience, please enable debugging support . If the code changes, the server will reload itself. It will also provide a useful debugger to track errors in the application (if any).

Before running or passing debugging parameters to the run() method, enable Debug mode by setting the debug attribute of the application object to True .

app.run(debug = True)

Guess you like

Origin blog.csdn.net/u013066730/article/details/108070761