Getting Started Guide and Advanced Flask - Flask first chapter Basics --Python Web Development Overview

        The first chapter of this book Flask Basics, introduces the basics of learning Flask development, Chapter 1, do Web development Let's talk about the Python language, introduced several principles of the current mainstream Web framework, as well as the selection of the Python framework proposal Chapter 2 further explanation began Flask development environment to build, the basics of developing a Python Flask of follow-up after the Chapter 3 explains knowledge Flask development of practical application, including views and routing, Flask template, Flask form, Flask database, Flask blueprint and testing.

       Each pair of corresponding sections do concepts and terminology are explained refining, and provide a sample program or core code, along with comments and explanations, it will be focused at the point marked, to help the student clearer understanding.

       Benpian section is more suitable for school entry or developer weak foundation's learning.

    Python Web Development Overview

Why do I start from the chapter Web development with Python, what are the advantages and features, there are a lot of Python Web framework, we have listed the mainstream Python Web framework, and lists the advantages and disadvantages of each, a reference to the reader selection purposes.

1.1 Why do Python Web Development

Current Web development is very popular, there are many programming languages ​​to choose from, Why Python it?

In February 2019, in the programming language popularity index (PopularitY of Programming Language, referred PYPL) published in the February list, Python language ranked first, beyond the most popular Java programming language, Python is now seen how popular. Whether now or in the PC end mobile Internet Web development work on products that do a variety of attempts to come up with models faster and iterate, start-up companies is even worse. Python language to better meet the needs of the times, it has also been more and more attention, more and more people accept Python, and use it in a production environment. Personally I think Python is very suitable for Web development for the following reasons:

(1) Python is an elegant and robust programming language, it inherits the powerful and versatile traditional compiled languages, drawing on ease of use and simple scripting language interpreted. Python is very suitable for rapid prototyping. Performance issues in many scenes ++ can write Python extensions by using C / C etc. optimization solution.

(2) extensive Python applications, big data, algorithms, operation and maintenance and other fields have good correspondence tools and libraries, can effectively reduce the technical barriers between different positions in product flow, communication and team members easier, solve problems more fast.

(3) Python standard libraries and third-party libraries is very strong, there are a lot of well-known projects are written in Python.

(4) Since 2005 Django open source, open source in 2008 Reddit, by the year 2010 Flask open source, Python has been used as a Web developer has 10 years of history, domestic watercress, Sohu, foreign Reddit, YouTube, Instagram, Pinterest, Bitbucket, Disqus, Dropbox and other companies have chosen Python as a Web development language. Do not worry Python reliability and performance issues, because it has been subjected to large-scale events and user concurrent access test.

1.2 Python Web framework introduced mainstream

Python Web framework is very large, each frame has its own developers use their own familiar with the use of the framework will feel good that the debate which frame each good or bad, Web development engineers faced frame selection, often very troubled, especially for middle-class engineers, there is no specific experience, do not know how to choose. We have to introduce the current mainstream Web framework features and application scenarios.

Mainstream Web framework are the following:

  1. Django

Django was originally developed to manage Lawrence Publishing Group with the following content-based news website, which Belgian gypsy jazz guitarist Django Reinhardt to name it and Flask is the most widely used Python Web framework, Django can so well-known largely because provides a very complete official documents. It provides one-stop solutions, including caching, ORM, management background, validation, form processing. It enables the development of complex database-driven Web site becomes very simple. But precisely because it insists its own framework for understanding the Web, the coupling system is too high, replace the built-in functionality often takes some effort, so the learning curve is quite steep.

  1. Flask

Flask is a lightweight Web application framework, which is based implementation Werkzeug WSGI and Jinja2 template engine. Flask is the author Armin Ronacher, originally it was just a joke of the April Fool's Day, but after the open-source Python programmer has been the favorite, the current number Star on GitHub has exceeded Django. It's a different design philosophy and Django; keep only core to add other features by extension mechanism. Flask used are dependent Pocoo team development. Pocoo team other projects as well Pygments, Sphinx, and Iodgedit. Flask extension environment is very prosperous, basically every aspect of the Web application has a corresponding extension available. Even if there is no corresponding expansion can easily implement its own.

  1. Pyramid

Pyramid in the domestic popularity is not high, mainly due to the lack of Chinese document, its advanced usage needs to get inspiration by reading the source code, although obscured by Django and Flask light, but its high performance than Flask, Pyramid inspired by Zope , Pylons1.0heDjango. In our traditional view, the framework is usually at the expense of small features large frame, and vice versa. But in fact we can not really control the application of a final development program, and Pyramid in the framework of the efforts in moving in the direction of application of different levels of competence. Although it uses the Chameleon and Mako template by default, but can easily switch to Jinja2, Shenzhen can make a variety of template engine coexist, identified by questionnaire extension. The same order of magnitude and the code Flask appreciation and watercress watercress wallet and other products is based on this framework to achieve the performance slightly higher than the Flask.

  1. Bottle

Bottle is a lightweight Web framework. It is characterized by single-file, the code uses only the Python standard library, without the need to rely on other additional third-party libraries. It is more in line with the definition of micro framework, as of today, only more than 4,100 lines of code.

  1. Tornado

Tornado full name Tornado Web Server, originally developed by non-blocking Web server FriendFeed development, now we see that after the acquisition Fackbook out of the open source version. It has a frame and the other major obvious difference; it is non-blocking server, and very fast. Thanks to its non-blocking mode of operation and the epoll, can handle thousands of connections per second Tornado. This means that for a long polling, WebSocket and other Web services for real-time, Tornado is an ideal Web framework.

  1. Web.py

Web.py also a micro-framework, co-founder of Reddit, RSS specifications cooperation creator of the famous computer hacker Aaron Swartz development. Web.py using class-based view. Easy to learn but powerful.

  1. Niche Web framework
  1. Quixote

Quixote is a lightweight Web framework by the National Engineers Association Innovation Research (Corporation for National Research Initiatives, CNRI) of AMKuchling, NeilSchemenauer and Greg Ward development. It is simple, efficient and concise code. Most users watercress products use a customized version of Quixote as a Web framework, although it has historical reasons (when Django / Flask framework has not yet appeared, etc.). But also proved that this framework began in 2000 can withstand the test of time. It uses the directory type the URL distribution rules, written in Python template. PTL template is more suitable for programmers, but not for artists involved in the preparation and modification of the front-end code, watercress using Mako alternative PTL in development. Quixote is not recommended to use in a production environment.

  1. Klein

Klein is out of the open-source organization Twisted framework based on micro and twisted web of werkzeug. Flask is very good, but the asynchronous non-blocking mode program can not be used, and it is the root cause of Django, Pyramid, like, are based on WSGI, and the WSGI interface is a synchronous blocking. Klein usage very much like the way Flask, but you can use asynchronous development of Web applications.

Select principle of Web 1.3 framework proposal

Introduce so many frameworks, so how to choose work in it? The following is a summary of some of the principles the author:

(1) choose a more mainstream framework. Because they are more complete documentation, technical accumulate more and more prosperous communities can be better supported.

(2) active attention of the framework. Update frequency response of the concerned projects, Issue and Pull Request of. If a project has not been updated for a long time, or have a bunch of problems to be solved but did not get a response, it should not be placed in the framework of a production environment.

If (3) to confirm the selection frame sufficient to meet demand. There is no better framework, only the most suitable framework. Your choice of Web framework not only meet current needs, but also give full consideration to the situation after a period of development of the project. That is forward-looking. If there is such a personal preference factors need to make sure they have the ability to provide support for Web framework of choice, avoid blind choice and lead to future weight to push to make choices at the time.

(4) Note that media timeliness. Make a choice, you might refer to some articles on the web, but note the time of publication. For example, I read a 2012 blog, which said it should choose A rather than B, and gave a number of reasons. C. appeared better after long B through the efforts have done better or 2012; and now the situation may have occurred a number

(5) an objective look at the media point of view. Media point of view is not necessarily correct (or not all correct). If it is not the official explanation, we should be skeptical and cautious, its essence to its dregs, not to make a decision after fully ism, should really be proven.

For the above Web frame, a few suggestions can refer to the following:

(1) If it is to demonstrate for or do not consider the long-term development, but need to use CURD database operations, some simple applications like REST API, select the frame is very loose, you can use the handy.

(2) If you are a beginner Web framework, the proposed framework as an entry selected Flask, Django learning curve in terms of relatively much lower. Etc. Once you are familiar Flask re-learning Django, it will be much easier.

(3) Pyramid and Django are for large-scale applications, Pyramid and more flexible, many developers large space, it is worth considering.

(4) website performance problem occurs, the problem often lies in not only Web framework and programming language, when to choose according to the actual product in the environment for some time to test the logical conclusion would, but do not just take to see some performance comparison article, in particular, do not do meaningless Hello World level of testing.

1.4 Flask framework for the development characteristics

Flask is very popular Python Web framework, it is so popular, the main reasons are as follows:

(1) there is a very complete official documents, very easy to get started.

(2) has a very good extension mechanism and third-party extensions environment, work in common software, there will be a corresponding expansion. Yourself achieve the expansion is also very easy.

(3) community activity is very high.

(4) in the form of micro-frames to developers greater choice.

(5) Pocoo team produced, Flask and its dependencies (Jinja2, Werkzeug) design is very good. Such decorator routing configuration, modular, request / application context, etc. by Blueprint.

Flask rely mainly on three libraries:

(1) Jinja2: default template engine.

(2) Werkzeug: contains WSGI, routing, debugging toolset.

(3) Itsdangerous: Based Django signature signature module (http://bit.ly/28QV7Fb) of.

Flask itself try to keep the core of streamlining, which is designed not to do too much decision-making for developers, and even Flask have to help you make a choice can be easily replaced. For two examples:

(1) Web and database procedures to deal with unavoidable, use SQLAlchemy, MongoEngine, without ORM (object-relational mapping) directly based on MySQL-Python driver underlying this development are possible, the choice is entirely in your hands.

(2) replace the default template engine to Mako Jinja2 or other template engines are very easy.

1.5 Summary

Be the first chapter from the popular Python language talk, to do about the Python Web development framework of the mainstream, and include many of the current Python Web framework, respectively, reviews, and how to compare the selection Web framework are some suggestions and then about the features and benefits Flask do Web development framework.

Published 179 original articles · won praise 16 · views 220 000 +

Guess you like

Origin blog.csdn.net/yan_dk/article/details/104086194