In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about how to simply analyze the Flask framework, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
We know that the Web framework abstracts the common parts of different Web applications and provides a series of common interfaces, thus preventing developers from doing repetitive work and focusing on business-related implementations.
Next, let's take a look at a specific Web framework. Flask is chosen here because it is a young and dynamic micro-framework with many supporters, well-documented and highly active community. Our forum project uses this framework.
Flask framework
To understand how the Flask framework abstracts out the common parts of Web development, let's first look at the general flow of Web applications. For Web applications, when the client wants to obtain dynamic resources, it will initiate a HTTP request (such as accessing a URL with a browser), and the Web application will carry out corresponding business processing in the background, take out the data needed by the user (from the database or perform some calculations, etc.) and generate the corresponding HTTP response (of course, if you access the static resource, you can directly return the resource No business processing is required. The whole process is shown in the following figure:
In practical applications, different requests may invoke the same processing logic. Here, HTTP requests with the same business processing logic can be identified by a class of URL. For example, in a forum site, all requests for Topic content can be represented by URL such as topic//, where the topic_id is used to distinguish between different topic. Then a get_topic (topic_id) function is defined in the background to obtain the corresponding data of topic. In addition, it is necessary to establish an one-to-one correspondence between URL and the function. This is called route distribution in Web development, as shown in the following figure:
The underlying layer of Flask uses werkzeug for routing and distribution. The code is very simple to write, as follows:
App.route ('/ topic//') def get_topic (topic_id): # Do some cal or read from database # Get the data we need.
After getting the data through the business logic function, the next step is to generate the HTTP response based on the data (for Web applications, the HTTP response is usually a HTML file). The common practice in Web development is to provide a HTML template file, then pass the data into the template, and render to get the final required HTML response file.
A more common scenario is that although the request is different, the data in the response is presented in the same way. Still take the forum as an example, for different topic, although the specific topic content is different, but the way the page is displayed is the same, there are title blocks, content columns and so on. That is, for topic, we only need to provide a HTML template, and then pass in different topic data, that is, we get a different HTTP response. This is called template rendering, as shown in the following figure:
Flask uses Jinja2 template rendering engine to render templates. The code is as follows:
App.route ('/ topic//') def get_topic (topic_id): # Do some cal or read from database # Get the data we need. Return render_template ('path/to/template.html', data_needed)
To sum up, the process for Flask to process a request is to first decide which function to handle according to URL, and then operate in the function to get the required data. Then transfer the data to the corresponding template file, and Jinja2 is responsible for rendering the HTTP response content, and then Flask returns the response content.
The above is how to simply analyze the Flask framework. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.