Flask Web Development-flask plug-in mechanism &flask-script

Source: Internet
Author: User
Tags passthrough

This section is taken from the second half of part I Chapter 2, skipping over the details of the specific interaction design for request and response. Mainly through the Flask-script plug-in Let the reader for the plug-in system has a simple understanding.

Flask is focused on extensibility, there are many plugins available in the community, and you can use the Python standard library or a variety of other libraries as well.

Flask-script

Flask-script this plugin, which is used to increase the command line parameters of the Flask application, has its own common options and supports custom commands. This feature may be similar to argparse in the Python standard library.

As mentioned before, flask comes with a server for the development environment, which supports some boot parameter configurations, but these configurations can only be passed in as parameters at startup app.run() . This is very inconvenient and the ideal way to configure it is to pass in the script command line arguments. This function is implemented by Flask-script.

Installation is simple:

$ pip Install Flask-script

The following adds some command line arguments to the previously written hello.py script, omitting some code:

From flask.ext.script Import Managermanager = Manager (APP) # ... if __name__ = = ' __main__ ':     manager.run ()

The flask plug-in is ready to be introduced through flask.ext this namespace after it is installed. Flask-script can provide a Manager class that flask.ext.script can be introduced through.

Initializes an instance of the plug-in by passing the application instance to the constructor, such as above Manager , which is the common initialization of the flask plugin. The instances obtained from the constructor can be used in a different normal situation. Now that the app is manager.run() started, the command-line arguments are resolved here.

Note: The current code is on GitHub on the 2c version, switch tag can be viewed.

Run hello.py , you can see that there are some basic command-line arguments applied:

$ Python hello.pyusage:hello.py [-h] {shell,runserver}...positional arguments:    {shell,runserver}     Shell              Runs a Python shell inside Flask application context.     Runserver          Runs The Flask development server i.e. App.run () Optional arguments:     -H,--help show this help         me Ssage and exit

shellcommand to open a Python shell in the context of the app, where you can handle some maintenance tasks or tests.

runservercommand to start the server, start debug mode by default, and many more parameters to choose from:

$ python hello.py runserver--helpusage:hello.py runserver [-h] [-t HOST] [-P PORT] [--threaded]                 [--processes PROCESS ES] [--passthrough-errors] [-d]                 [-r]runs the Flask development server i.e. App.run () optional arguments:-h,--help
   show this help message and Exit-t HOST,--host host-p PORT,--port port--threaded--processes      Processes--passthrough -errors-d,--no-debug-r,--no-reload

--hostThe parameter can specify the connection that the server listens on, the default listener localhost (typically 127.0.0.1). Try to modify:

$ python hello.py runserver--host 0.0.0.0* Running on http://0.0.0.0:5000/* restarting with Reloader

Now any computer in the same network can access this server through access, which is the IP of the http://a.b.c.d:5000 a.b.c.d computer running server in this network.

Note: The next is the template mechanism of the description, focus! The response returned by flask is entirely dependent on this mechanism.

Flask Web Development-flask plug-in mechanism &flask-script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.