This is the second day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021

preface

Python is an object-oriented high-level programming language with simple syntax, free open source, compile-free and high scalability. It can also be embedded into C/C++ programs and rich third-party libraries. Python is used in big data analysis, artificial intelligence, web backend and other application scenarios.

Python is currently the main popular Web frameworks: Flask, Django, Tornado

What’s the difference between a framework and a library?

Frameworks have similar functionality to libraries, but are more comprehensive in one area. With frameworks, developers can reduce the need to reinvent the wheel by simply calling its classes or functions.

Flask framework (flask) : Flask framework (flask) : Flask framework (Flask) : Flask framework (Flask) : Flask framework (Flask

1. Flask Framework Overview

Flask Framework is a lightweight Web framework based on WSGI. Flask is a simple and extensible flask, as we all know how to do in a small sparrow.

Flask framework advantages:

  • Based on WSGI applications, explicit instantiation must be used
  • Automatic sorting routing using the Werkzeug routing system
  • Use Jinja2 template engine for quick and easy use of templates
  • Use thread-local variables for fast access to weBY applications
  • Support for asynchronous wait and ASCI (Async-first)
  • Connect unit tests, and developers quickly check the tests
  • Built-in development server, no need to use other third-party network services

Flask framework acquisition

  • Download Flask using PIP

    pip install flask
    Copy the code
  • After the 🔔 Flask package is downloaded, the dependent libraries are automatically downloaded

    • Werkzeug library: Implements WSGI, the Python interface between the front-end and the server
    • Jinjia library: template language that displays Web pages
    • MarkupSafe library: Comes with Jinjia, which is used to escape untrusted input from render templates
    • ItsDangerous: Protects the cookies of flask sessions
    • Click library: Used to write command line frameworks

  • 📢 Flask Optional dependent libraries, which can be downloaded as required

    • Blinker library: Provides support for Singals
    • Python-dotenv library: Start dotenv’s environment variables when running commands to support flask
    • Watchdog provides a fast loader for the Flask server

Flask framework use

In code, use from… Import Imports the flask package

from flask import flask
Copy the code

With a long Ctrl press, we can view the Flask source code introduction

2. Flask Demo step

In flask Web Framework, look at the steps you need to take to build a DEMO site:

  • Initialization: Import the Flask library and define the Flask instantiation objects

    • Flask instantiation requires passing in a __name__: the name of the package or module whose purpose is to receive it
    • Ask flask.helpers.get_root_path to get the directory for static and template files
    from flask import Flask
    APP = Flask(__name__)
    Copy the code
  • Define an application method and decorate with the Route decorator

    • Decorates the created application method by calling route() : The purpose is to tell Flask how to access the function

    @APP.route("/")
    def hello() :
        return ("<h1>welcome juejin</h1>")
    Copy the code
  • In main, the Flask instantiation object calls the run() method to run

    • If we are in debug phase, we need to set debug=True

    if __name__ == "__main__":
        
        APP.run(debug=True)
    Copy the code
  • We can see the background run log

  • We go to 🔗, a link generated in the log, to see the parsed text on the web page

Flask Basic functions

Flask simple Web program, mainly use the flask module routing function

  • Routing functions

    • Route () is a decorator that binds urls to functions

      • Rule: Binds the URL rule to access this function
      • Option: The list of parameters to forward to the Rule object
    @app.route(url,optiion)
    Copy the code
    • Run () runs the application on the server

      • Host: indicates the listening host name

      • Port: indicates the host port number

      • Debug: Displays debugging information

      • Options: forward to the underlying Werkzeug server

  • Template to provide

    Flask framework is implemented based on the Jinja2 template engine

    • Create a templates subdirectory in your project, and create demo.html under that directory

    • In the Flask file, calls are required in application methods

      render_template('demo.html', name=name)
      Copy the code

conclusion

In this issue, we have introduced the flask microWeb framework advantages, demo Web application steps, and the routing and template functions in the Flask module.

That’s the content of this episode. Please give us your thumbs up and comments. See you next time