Home > Backend Development > Python Tutorial > A detailed discussion of the five commonly used Python web frameworks

A detailed discussion of the five commonly used Python web frameworks

Y2J
Release: 2017-05-04 14:20:05
Original
1266 people have browsed it

There are so many frameworks in Python, and not many people can play them all. Frankly speaking, I have only used three of them to develop projects, and have a little contact with others, so I can only talk about it briefly here. Welcome to know more. Friends added

Speaking of Web Framework, the world of Ruby is dominated by Rails, while Python is a world where a hundred flowers bloom, with countless micro-frameworks and frameworks. For an incomplete list, see:

wiki.python.org/moin/WebFrameworks

Although another major scripting language, PHP, also has many frameworks, they are far less exaggerated than Python. It is precisely because of the Python Web Framework (Python Web Development Framework, There are too many Python frameworks (hereinafter referred to as Python frameworks), so there are always topics about which Python framework is better and worse in the Python community, and the discussion spans even as long as 3-5 years.

There are so many frameworks in Python, and not many people can play them all. Frankly speaking, I have only used three of them to develop projects, and have a little contact with others, so I can only talk about it briefly here. , knowledgeable friends are welcome to add.

Django

A detailed discussion of the five commonly used Python web frameworks

Although the Python framework is said to be blooming, there is still one that is the biggest. It's Django. To say that Django is the best among the Python frameworks, some people agree and others firmly disagree. However, if Django has the most complete documentation, the highest market share, and the most job openings, I guess everyone has no objection. The main things that Django is praised for include:

Perfect documentation. I think a large part of Django's success is due to Django's nearly perfect official documentation (including Django book).

Full set of solutions, Django, like Rails, provides a full set of solutions (full-stack framework + batteries included), basically everything you need is available (for example: cache, session, feed, orm, geo, auth), and Django makes them all by itself. Django has basically prepared all the tools for website development for you, so the development efficiency is needless to say, and it is easy to find problems if they arise. , not in your code but in Django’s source code.

Powerful URLRoutingConfiguration, Django allows you to design a very elegant URL. In Django, you can basically say goodbye to ugly GET parameters.

Self-service management backend, admin interface is one of the more eye-catching contribs in Django, allowing you to have a complete backend management interface without writing a single line of code.

The system is tightly coupled. If you feel that a certain function built into Django is not very good, it will be difficult to replace it with your favorite third-party library, such as the ORM and Template mentioned below. It's almost impossible to use SQLAlchemy or Mako in Django, and even with some patches, it will make you feel very, very awkward.

Template function is relatively weak and cannot insert Python code. To write more complex logic, you need to use Python to implement Tag or Filter. There has been a lot of debate about templates. Recently, there are two interesting articles about Python templates for reference:

1 pydanny.blogspot.com/2010/12/stupid-template-languages. html (FQ required)
2 techspot.zzzeek.org/2010/12/04/in-response-to-stupid-template-languages/

Although the URL configuration is powerful, it all needs to be written by hand. This is completely inconsistent with the concept of Convention over configuration of Rails. The URLs assigned by experts and those who are new to Django will be very different.

The database schema has been set for you, so problems arise. For example, many websites require email addresses to be unique, but the value of this field in the schema is not unique, so it is necessary to struggle. .

In general, Django has a lot to offer, and it is very good to use it to quickly develop some Web applications. If you follow Django's design philosophy, you will find Django easy to use and the more you use it, the easier it will become. On the contrary, if you cannot integrate or accept Django's design philosophy, it will be painful for you to use Django. It is best to give up as soon as possible. So in the eyes of some people, Django is no different from an elixir, but to some people it is poison and highly toxic.

Pylons & TurboGears & repoze.bfg

A detailed discussion of the five commonly used Python web frameworks

Besides Django, the other big one is Pylons, because TurboGears2.x is based on Pylons, and repoze.bfg has also been incorporated into this large project in the Pylons project, and will not be discussed separately later. TurboGears and repoze.bfg.

The design concepts of Pylons and Django are completely different. Pylons itself only has about two thousand lines of Python code, but it also comes with some third-party modules that are almost used by Pylons. Pylons only provides one shelf and optional solutions. You can freely choose components such as Template, ORM, form, and auth according to your own preferences. The system is highly customizable. We often say that Python is a glue language, so we can definitely say that Pylons is a glue framework designed with glue language.

Choosing Pylons is mostly about choosing its freedom. Choosing freedom also means you choose nightmare:

Learning nightmare, Pylons relies on many third-party libraries, they are not Pylons When you learn Pylons, you also have to learn how to use these libraries. The key is that sometimes you don’t know what you want to learn. The learning curve of Pylons is much higher than that of Django, and the official documentation of Pylons has always been the target of criticism. Fortunately, the book The Definitive Guide to Pylons was published later. This situation has changed. For this reason, Pylons was once known as a Python framework only suitable for experts.

Debugging

A nightmare, because there are many modules involved, and once an error occurs, it is more difficult to locate the problem. It may be the fault of the program you wrote, or it may be that Pylons is wrong, or SQLAlchemy is wrong, or maybe there is a bug in the formencode, it is very messy anyway. This problem can only be solved if you are familiar with it. The integration of Pylons and repoze.bfg may give birth to the next framework that can challenge Django's status.

Tornado

& web.py

##Tornado is a Web server (This article will not elaborate on this), and it is also a micro-framework similar to web.py. As a framework, the idea of ​​Tornado mainly comes from Web.py. You can also see the boss of Tornado on the homepage of Web.py website. Bret Taylor said this (the framework he mentioned here for FriendFeed can be regarded as the same thing as Tornado): A detailed discussion of the five commonly used Python web frameworks

“[web.py inspired the] Web framework we use at FriendFeed [and] the webapp framework that ships with App Engine…”

Because of this relationship, Tornado will not be discussed separately later.

The advantage of streamlining a framework is that you can focus on the business logic without having to worry too much about the framework itself or being interfered by the framework. At the same time, the disadvantages are also obvious. You have to do many things yourself.

I personally prefer this kind of streamlined framework, because you can easily understand the working mechanism of the entire framework by reading the source code. If that part of the framework is not very satisfactory, I can completely Monkey patch it according to my own requirements. Come.

Bottle

& Flask

##Bottle and As a representative of the new generation of Python frameworks, Flask is very interesting in that it uses the decorator method to configure URL routing, such as:

from bottle import route, run
 
@route('/:name')
def index(name='World'):
  return &#39;<b>Hello %s!</b>&#39; % name
 
run(host=&#39;localhost&#39;, port=8080)
Copy after login
A detailed discussion of the five commonly used Python web frameworksBottle. Like web.py, Flask is very streamlined. Bottle and even all codes All in that .py file with two thousand lines. In addition, Flask, like Pylons, can be combined well with Jinja2, SQLAlchemy and the like.

However, there are still very few successful cases of either Bottle or Flask.

Quixote

The reason why I want to talk about Quixote in particular is because "Douban.com", the largest website developed in Python in China, was developed using Quixote. I just briefly looked through the source code. I have not done any research and will not comment. I will add more if I have experience. I'm just thinking that if Douban were developed now, there would be more choices.

Others

(web2py, uliweb, Karrigell, Werkzeug...)

Last misunderstanding about framework selection

In the framework When it comes to choice, many people easily fall into the following two misunderstandings without knowing it: 1. Which framework is the best - there is no best framework in the world, only the most suitable and suitable for you A framework for your team.

Programming language

The choice is also the same. If your team is most familiar with Python, use Python. If you are most familiar with Ruby, then use Ruby. Programming languages ​​and frameworks are just tools. They can be used more and faster. , good and economical, it is a good thing after finishing the work.

2. Pay too much attention to performance - In fact, most people don’t need to care too much about the performance of the framework, because the website you develop is simply a small website. There are not many websites that can support 10,000 IPs, and 10 Thousands are even rarer. It doesn't make much sense to talk about performance without a certain amount of visits, because your CPU and memory are always idle. Moreover, languages ​​and frameworks are generally not performance bottlenecks. Performance problems most commonly occur in database access and file reading and writing. PHP's Zend Framework is notoriously slow, but Zend Framework also has big websites, such as digg.com; can't Ruby and Rails, which are often said to have performance problems, still be able to develop twitter? Furthermore, the current cost of hardware and bandwidth is actually very low. Especially with the advent of cloud computing platforms, labor costs are the most expensive. If you don’t have tens of thousands of IPs, you don’t have to worry too much about performance issues. If the traffic increases, spend some money to buy some. The server space is good, and performance problems can be solved simply and quickly.

Note: Some netizens questioned my statement that "Quora was developed using Pylons" is not objective. Let me explain. The website A mentioned here is developed using B, which only means that A mainly or Part of it was developed by B, so don’t worry about whether A uses C.


【Related Recommendations】

1. Free tutorial for getting started with Python

2. Python Study Manual

3. Python Object-Oriented Video Tutorial

The above is the detailed content of A detailed discussion of the five commonly used Python web frameworks. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template