By Alex Mungai Muchiri, Alibaba Cloud Community Blog author
Today developers can choose from many different Python frameworks. Among them, Flask, Django and Pyramid are the most popular, but others include Falcon, Pecan, Bottle, Tornado and Diesel. Given all of these choices, you may feel a bit confused when selecting which framework will work best for particular case.
When it comes down to it, choosing the right framework is just as important as choosing a reliable service like Alibaba Cloud's Elastic Compute Service (ECS). The outcome of your project depends on a lot of which framework you end up going with. In this blog, we'll explore the differences between these three Python frameworks and help you make sure you make the right choice for your application.
Typically, the project that you deploy on an Alibaba Cloud ECS instance may fall under three main categories:
To go over all of these quickly, microframeworks are rather simple projects without very complex requirements. Flexibility in the frameworks means that you've got the ability to choose the database, structure of addresses, how you choose to template things, among many other aspects of your application.
The top three frameworks that we'll discuss in this top meet these three main requirements rather differently. For instance, Django has a rather inclusive approach as it includes multiple object-relational mapping (ORM), while Flask and Pyramid are a bit flexible. Based on the three categories above, you can learn more about the differences among these three frameworks.
Let's first look at Django, which, you could say, comes with multiple stuff right out of the box to make the work of developers easier. But there's a catch. You need to be really good with Python already. The included components also mean that your application will be quite heavy for your server and thus a bit slower as a result. Nonetheless, all of this means that this framework does away with the need to bother with infrastructure decisions and minimizes work in routing, templating, authentication and some of the aspects of database management.
Next, we have Pyramid, which at its core is a much lighter alternative to Django. It includes routing and authentication features, but most of the other requirements such as templating and database management will need external libraries. Most developers consider Pyramid as an alternative to the other two frameworks. You will benefit from a substantial number of third-party plugins and libraries such as dashboards and admin panels. When working with large systems that require flexibility, you will find Pyramid more palatable than Django. Since you are at liberty to choose the ORM, templates, and forms, you end up with a slightly lighter application, which translates to less workload for your server. However, be prepared to do some heavy lifting before getting used to it, especially when used to Django. Mozilla and Yelp are some examples of organizations that use Pyramid.
Our last consideration is Flask, a relatively young framework launched in 2010. Despite it being a recent debut, there is already a large user base from the developer community, especially in microservices and small projects. When handling projects that necessitate the use of non-standard ORM, both Flask and Pyramid can be great alternatives, the difference being on the application architecture.
So, what should guide the choice of a framework for use on your Alibaba Cloud ECS?
There are four main considerations to make here:
Alibaba Cloud is very reliable and responsive and with little latency. It also allows autoscaling to meet computational needs of your app. Nonetheless, the performance of your deployed application also depends on how it is built. The overall performance depends on all the components wired together to deliver on user requests. How these components interface and connect between each other varies in complexity and risk. Python framework relies greatly on third-party plugins and as such, failure and response are of great significance. Failure and service slowdown in most cases is as a result of interfacing between components.
This is where Flask is a clear winner. When adapted for use in a lightweight manner, such as in microservices, you are likely to get a very fast response and good performance. Pyramid is also a good contender in this category owing to its light nature, but when the application requires wiring together of multiple plugins and components, the chances of failure increase exponentially. When fewer components are used in an application, failure decencies reduce and performance improves. Django comes pre-integrated with stable components out of the box and would be preferred for overly complex applications that do not need much manipulation.
Here, we go with Flask for smaller applications and microservices, Pyramid for flexible large applications and Django for larger and lesser flexible applications that require a large number of components. Overly, Flask applications offer better performance than Pyramid or Django.
Application complexity is one of the most important contributors to the cost of infrastructure. An overly complex application needs the use of more computing power, which translates to higher costs per unit time. If the number of users is constant, the cost of infrastructure required depends on the number of individual moving parts in a system. A framework that incorporates unnecessary components to attain business objective is obviously going to have an impact on the cost of infrastructure.
Again, Flask and Pyramid's stripped-down approach is beneficial in this respect. The Django approach tends to have many components in it, which do not necessarily have much business value but which nonetheless increase the complexity. However, this matter will depend on if you are dealing with a microservices architecture and small applications, complex enterprise systems or larger monolithic systems.
For the microsystem approach, Flask is the clear winner, with light-weight and fast response to save on infrastructure costs massively. Kubernetes is one of the ways to handle microservices on Alibaba Cloud. You want the microservices to be as simple as possible, a solution you can only have with Flask and to some extent, Pyramid.
In enterprise systems that require complex wiring of components and a less flexible deployment, Pyramid is preferred due to more libraries and plugins, but still a less need for unnecessary components. Django is the preferred option for larger and monolithic systems that do not require much tampering with the pre-set integrations that come out of the box. It is noteworthy that microservices architecture is very great for optimal system performance and efficient use of server resources.
Many developers today are understanding the importance and advantages of microservices architecture for optimal availability and failure mitigation. With these considerations, it seems that there are several considerations you need to think about before deciding on the particular framework for use. Your Alibaba Cloud ECS instance will be as affordable as ever, but keep these things in mind if you're also trying to mind your costs. Largely, when scaling services for a large number of users, that is when a significant difference in costs arise. Generally, therefore, it is better to adopt a microservices architecture and Flask is your clear winner. Pyramid can still be a contender if your larger application requirements cannot be met by Flask.
For any open source framework, community support is pretty crucial. New libraries, plugins, and documentation come from the community. In this regard, Django is leading the pack with about 80,000 questions on Stack Overflow alone, all of which are a contribution towards the development of the framework. It is also leading the pack with the number of extensions available and you are likely to get nearly all you need from the community. Flask and Pyramid, on the other hand, do not boast of such large communities. Nonetheless, the contributions on GitHub are comparably well developed. If you are a new developer, you are probably keen on great support from the community and this may inform your choice of framework. In this category, Django is the clear winner.
Bootstrapping is the ability to get an application up and running with the use of external libraries and plugins. So, you have your ECS up and running and now you want to write an application fast. Django has many built-in tools, which can enable you to get started immediately but the learning curve is much steeper. Pyramid has fewer tools but still a handful of tools and thus does not depend much on extensions to get up and running. For these reasons, Flask is very easy to code and the learning curve is quite low. Flask blueprints are used to separate components in an application.
There is a major difference in approach between Django and the lesser loaded Flask and Pyramid. The former treats an application as componentized necessitating a wide knowledge base. In Flask and Pyramid, a project is treated as an entire application.
Now let us write a simple Hello World app in the three frameworks:
Flask by and large is the simplest of the three:
from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello World!" if __name__ == "__main__": app.run()
The python structure is as shown below:
|----Flask App |---------Flask App |--------------__init__.py |--------------static |--------------templates
Evidently, you can get hacking immediately with little Python experience and thus there is no need for complex bootstrapping tools in Flask. With several different components, use the blueprints. As an example, user roles could be handled by
roles.py while configuration roles are handled in
config.py. It is easy to see why Flask is great for small applications and microservices.
Pyramid has an built-in bootstrapping tool named pcreate that will generate a skeleton for your project. The skeleton comes with configurations, a template and application packaging files for upload the app to Python Package Index.
The process commences with the command below:
$ pcreate -s starter hello_pyramid
Below is a really flexible template by Pyramid:
hello_pyramid ©À©¤©¤ CHANGES.txt ©À©¤©¤ development.ini ©À©¤©¤ MANIFEST.in ©À©¤©¤ production.ini ©À©¤©¤ hello_pyramid ©¦ ©À©¤©¤ __init__.py ©¦ ©À©¤©¤ static ©¦ ©¦ ©À©¤©¤ pyramid-16x16.png ©¦ ©¦ ©À©¤©¤ pyramid.png ©¦ ©¦ ©À©¤©¤ theme.css ©¦ ©¦ ©¸©¤©¤ theme.min.css ©¦ ©À©¤©¤ templates ©¦ ©¦ ©¸©¤©¤ mytemplate.pt ©¦ ©À©¤©¤ tests.py ©¦ ©¸©¤©¤ views.py ©À©¤©¤ README.txt ©¸©¤©¤ setup.py
django-admin is the primary bootstrapping tool which is initiated like so:
django-admin startproject hello_django django-admin startapp howdy
As we already mentioned, the project will be structured into individual applications.
hello_django ©À©¤©¤ hello_django ©¦ ©À©¤©¤ __init__.py ©¦ ©À©¤©¤ settings.py ©¦ ©À©¤©¤ urls.py ©¦ ©¸©¤©¤ wsgi.py ©À©¤©¤ howdy ©¦ ©À©¤©¤ admin.py ©¦ ©À©¤©¤ __init__.py ©¦ ©À©¤©¤ migrations ©¦ ©¦ ©¸©¤©¤ __init__.py ©¦ ©À©¤©¤ models.py ©¦ ©À©¤©¤ tests.py ©¦ ©¸©¤©¤ views.py ©¸©¤©¤ manage.py
Django makes it very easy to build applications with little coding. The main drawback with this model is that the learning curve for even a small app is quite steep. Also, structuring an individual application is largely the work of an individual.
In this article, we have evaluated the three major Python frameworks and their suitability for use in your ECS instance. We have seen how to get started in all three and also learned that Flask is the lightest among the three and Django the heaviest.
If you are not an experienced developer it may be a bit hard to choose the right tool for the job. But, overall, flask is very good for microservices architectures and small applications. Pyramid, on the other hand, is great if you want a larger application that requires flexible wiring of components. Finally, Django comes with a lot of tools out of the box to handle those repetitive tasks.
Without a doubt, Django really helps with handling extra tasks. The rule is to always understand what components handle respective functionalities. In the end, it may be better to choose Flask if in doubt since you will not have to deal with complex setups and plugins. It is also lighter on your computing resources than the two alternatives. Pyramid is a sort of crossbreed of the two frameworks.
In summary, you can get hacking immediately without much Python experience when working with Flask. Nonetheless, Django is king when you want to save yourself the trouble of importing multiple plugins and writing code snippets to integrate them.
Don't have an Alibaba Cloud account? Interested in purchasing ECS instances and figuring out how to use one of these frameworks? Sign up for an account and try over 40 products for free worth up to $1200. Get Started with Alibaba Cloud to learn more.
Alibaba Clouder - February 26, 2019
Alibaba Clouder - December 17, 2018
Alibaba Clouder - October 11, 2018
Alibaba Clouder - March 18, 2019
Alibaba Clouder - August 26, 2020
Alibaba Clouder - December 11, 2018
Explore Web Hosting solutions that can power your personal website or empower your online business.Learn More
Explore how our Web Hosting solutions help small and medium sized companies power their websites and online businesses.Learn More
Web App Service allows you to deploy, scale, adjust, and monitor applications in an easy, efficient, secure, and flexible manner.Learn More
A cloud firewall service utilizing big data capabilities to protect against web-based attacksLearn More
More Posts by Alex