A web framework is a collection of packages or modules which allow developers to write web applications or services quickly, without getting into the details of network protocols or network socket programming. Web frameworks encapsulate what developers have learned over the past 20 years while programming sites and applications for the web. Frameworks make it easier to reuse code for common HTTP operations and structure the projects so that other developers can quickly build the projects with the knowledge of the same framework.
Web framework common functionality include:
- URL Routing
- Input form handling and validation
- HTML, XML and JSON and other output formats with a templating engine
- Database connection configuration and persistent data manipulation through Object Relational Mapper
- Web security against CSRF(cross-site request forgery), SQL injections and other malicious attacks
- Session storage and retrievals
Do I have to use a web framework?
How are these web frameworks different from LAMP? LAMP is a software stack containing Linux, Apache, MySQL, PHP/Python/Perl. Apache is a web server program which is a piece of software which actually runs the web applications. This piece of software is part of the web frameworks too. But the frameworks help to build the applications quickly by aligning with the project development structure, templating systems etc, so that developers focus on logic rather than packaging multiple components from scratch.
Web frameworks solve the big two: Routing and Templates
Of all the questions surrounding building a web application, two stand out:
- How do we map a requested URL to the code that is meant to handle it?
- How do we create the requested HTML dynamically, injecting calculated values or information retrieved from a database?
The above issues are solved in the python web frameworks using Routing libraries and Templating libraries. For Eg: Flask provides route() call and Jinga2 templating and Django provides URL dispatcher module and Django Template mechanism.
Popular Python web frameworks:
Django always tops the list of python frameworks because it makes web development ridiculously fast and scalable, and boasts a pragmatic design. It is a framework for perfectionists with deadlines. It lets you build high performing, elegant web applications quickly, focusing on automating as much as possible and adhering to the DRY (Don’t Repeat Yourself) principle. It supports many database engines like MongoDB, MySQL, PostgreSQL, SQLite3
Pyramid is an open source python based web application framework. Its main goal is to do as much as possible with minimum complexity. The most striking feature of Pyramid is its ability to work well with both small and large applications. Some of the great Pyramid’s features are URL generation, extensible configuration, all-embracing templating and asset specifications, flexible authentication and authorization, view predicates and function decorators.
TurboGears is an open source, data-driven full-stack web application framework. It comes with user-friendly templating and a powerful and flexible ORM. Some of the distinctive features include support for SQLAlchemy, MVC Style architecture, Multi-Database support, Validations with FormEncode, ToscaWidgets, an application library that simplifies coordination of front-end design and server development.
Web2py is an open source scalable full stack python framework. It does not support Python 3. No requirements for installation and configurations. Ability to run on Google App Engine, Amazon EC2 and any web hosting that support either Python 2.5-2.7 apart from Windows, Mac and Linux. Other important features are Role-based access control and support for internationalization.
Flask is a Micro-web framework in Python. It was inspired by Sinatra Ruby framework. It completely depends on the WSGI toolkit and Jinga2 Templates. The main idea behind Flask is to help build a solid web application foundation, and use any other extensions on the need basis for an application. Flask’s lightweight and module design makes it easily adaptable to a developer’s needs. It includes a number of useful out of the box features like built-in development server, fast debugger, integrated support for unit testing, RESTful request dispatching, Jinga2 Templating.
There are many more frameworks in python to name a few, Cherry Py, Bottle, Sanic, Tornado, Dash.
Things to consider while choosing the framework:
When deciding which framework to use, look at the size and complexity of the project. If there is a need to develop a large system packed with features and requirements, a full stack framework might be the right choice, if the app is smaller and simpler, consider a microframework. A final decision should come from your own understanding of the project and the tasks to be simplified.