Python flask Framework

Source: Internet
Author: User
Tags timedelta

Flask is a mini-framework developed based on Python and relies on jinja2 templates and Werkzeug Wsgi services, for the Werkzeug essence is the socket server, which is used to receive HTTP requests and preprocess requests, and then trigger the flask framework. Based on the functionality provided by the Flask framework, the developer processes the request and returns it to the user, and if it is to be returned to the user complex content, it is necessary to use the JINJA2 template to do the processing of the template, namely: render the template and data and return the rendered string to the user's browser.

Micro does not mean that you need to cram the entire WEB application into a single Python file (though it does), and does not imply that the Flask is functionally deficient. Micro-frame "micro" means that Flask is designed to keep the core simple and easy to scale. Flask won't make too many decisions for you--like what kind of database to use. And what Flask chooses--like what template engine to use--is easy to replace. Everything else can be mastered by you. This way, Flask can be a perfect match for you.

By default, Flask does not include the database abstraction layer, form validation, or any other functionality that already has multiple libraries to perform. However, Flask supports the use of extensions to add these features to your app, just as the Flask itself implements. Numerous extensions provide features such as database integration, form validation, upload processing, and a wide variety of open authentication technologies. Flask may be "tiny", but it is ready to be put into use in a demanding production environment.

I. Basic USE
 from Import  = Flask (__name__) @app. Route ('/')def Hello_world ():     return ' Hello world! ' if __name__ ' __main__ ' :    app.run ()
Second, the configuration file
the configuration file in Flask is a Flask.config.Config object (inherited dictionary), which is configured by default: {'DEBUG': Get_debug_flag (default=False), whether debug mode is turned on'Testing': False, whether test mode is turned on'propagate_exceptions': None,'preserve_context_on_exception': None,'Secret_key': None,'Permanent_session_lifetime': Timedelta (days=31),        'Use_x_sendfile': False,'Logger_name': None,'Logger_handler_policy':' always',        'server_name': None,'Application_root': None,'Session_cookie_name':'Session',        'Session_cookie_domain': None,'Session_cookie_path': None,'session_cookie_httponly': True,'session_cookie_secure': False,'session_refresh_each_request': True,'Max_content_length': None,'Send_file_max_age_default': Timedelta (hours=12),        'trap_bad_request_errors': False,'trap_http_exceptions': False,'explain_template_loading': False,'Preferred_url_scheme':'http',        'Json_as_ascii': True,'Json_sort_keys': True,'Jsonify_prettyprint_regular': True,'Jsonify_mimetype':'Application/json',        'Templates_auto_reload': None,} mode one: app.config['DEBUG'] =True PS: Because the Config object is essentially a dictionary, you can also use App.config.update (...) mode two: App.config.from_pyfile ("python file name") such as: settings.py DEBUG=True App.config.from_pyfile ("settings.py") App.config.from_envvar ("environment variable name"the value of the environment variable is the python file name name, internally called the From_pyfile method App.config.from_json ("JSON file name") JSON file name, which must be in JSON format, because Json.loads app.config.from_mapping is executed internally ({'DEBUG': True}) Dictionary format app.config.from_object ("the path of a Python class or class") App.config.from_object ('Pro_flask.settings.TestingConfig') settings.pyclassConfig (object): DEBUG=False Testing=False Database_uri='sqlite://:memory:'             classProductionconfig (Config): Database_uri='Mysql://[email Protected]/foo'             classDevelopmentconfig (Config): DEBUG=TrueclassTestingconfig (Config): Testing=true PS: from Sys.path already exists path to start write PS:settings.py file default path to be placed in program Root_path directory, if Instance_relative_config is True, then is the Instance_path directory .
Third, the routing system
    • @app. Route ('/user/<username> ')
    • @app. Route ('/post/<int:post_id> ')
    • @app. Route ('/post/<float:post_id> ')
    • @app. Route ('/post/<path:path> ')
    • @app. Route ('/login ', methods=[' GET ', ' POST ')

There are five common routing systems, all routing systems are based on the corresponding relationship to deal with:

Default_converters = {    'default': Unicodeconverter,'string': Unicodeconverter,' any': Anyconverter,'Path': Pathconverter,'int': Integerconverter,'float': Floatconverter,'UUID': Uuidconverter,}
Four, template

Python flask Framework

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.