SoFunction
Updated on 2024-11-19

An introduction to the Python flask framework

 Preface:

Python Object-oriented high-level programming language, with its simple syntax, free and open source, high compilation-free extensibility, but also can be embedded into the C / C + + program and rich third-party libraries, Python used to big data analysis, artificial intelligence, web back-end and other application scenarios.

Python The main popular web frameworks at the momentflaskDjangoTornado

So the difference between a framework and a library?

Frames (framework) and libraries have similar functions, but frameworks are more comprehensive in a certain area. The use of frameworks will reduce the developer to repeat the creation of wheels, directly call its class or function to achieve the required functionality.

So, in this installment, we are going to learn one of the web frameworks provided by Python - flask framework related methods to learn, Let's go~!

1. flask framework overview

flask framework is a lightweight web framework based on WSGI .flask As the familiar saying "a small sparrow has all the essentials", flask is characterized by its simplicity and expandability.

1.1 Advantages of the flask framework

  • WSGI-based applications must be explicitly instantiated using the
  • utilizationWerkzeugThe routing system performs automatic sorting of routes
  • utilizationJinja2Template engine for quick and easy use of templates
  • Using thread-localized variables for fast accesswebyapplication
  • Support for asynchronous waiting andASCI(async-first)
  • Bridging unit tests and quick test checking by developers
  • Self-contained development server, eliminating the need for other third-party web services

1.2flask framework acquisition

Use pip to download flask

pip install flask

Once the flask package is downloaded, the dependencies will be downloaded automatically

  • Werkzeug library:Implementing WSGI, the Python interface between the front-end and server-side
  • Jinjia Library:Template language, display web page
  • MarkupSafe library:Comes with Jinjia, which is used to escape untrusted input for rendering templates.
  • itsDangerous:Protecting cookies for flask sessions
  • Click library:For writing command-line frameworks

flask Optional dependency libraries, as needed

  • Blinker Library:Provide support for Singals
  • Python-dotenv library:Started when running a commanddotenvenvironment variable to support flask
  • Watchdog provides a fast loader for flask servers.

1.3 Using the flask framework

In the code, use from... .import to import the flask package

from flask import flask

Long press Ctrl, we can see the Flask source code introduction

2. flask demo steps

In the flask web framework, let's see what steps we need to do to build a DEMO website:

Initialization:Importing the Flask library and defining Flask instantiated objects

  • When Flask is instantiated, you need to pass in the__name__: The purpose is to receive the name of the package or module as a parameter
  • have sb do sth.get_root_pathin order to get the directory of static and template files
from flask import Flask

APP = Flask(__name__)

Define an application method and route decorator to decorate the

  • call (programming)route()method decorates the application method created: the purpose is to tell flask how to access the function
@("/")

def hello():

    return ("<h1>welcome juejin</h1>")

In main, the flask instantiated object calls the run() method to run the

  • If we are in the debugging phase, we need to set thedebug=True
if __name__ == "__main__":

    (debug=True)

Backend Runtime Log Link

We visit the chain generated in the log to see the parsed text on the page

3. flask basic functions

flask The above implementation of a simple web program, the main use of the flask module routing functions

3.1 Routing Functions

route() is the decorator that binds the URL to the function

  • rule: bind the url rules for accessing the function
  • option: a list of parameters to be forwarded to the Rule object
@(url,optiion)

  • run()Is running the application on a server
  • host:Hostname to listen on
  • port:Host port number
  • debug:: Provide debugging information
  • options: To be forwarded to the underlyingWerkzeugserver (computer)

3.2 Template provision

flask The framework is based on theJinja2Template engine implementation

  • Create a subdirectory in the projecttemplatesand then in its directory create the
  • In the flask file, the application method needs to call the
render_template('', name=name)

4. Summary

In this issue, we are interested in theflask ancient Chinese state near present day ChongqingwebFramework advantages and features, demo web application program steps and a basic understanding of the flask module routing and templates two functions.

To this article on the Python flask framework is introduced to this article, more related to Python flask framework content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!