4 min read

A Beginner's Guide to Python Web Frameworks

Aug 17, 2020 4:53:31 AM

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:

  1. URL Routing
  2. Input form handling and validation
  3. HTML, XML and JSON and other output formats with a templating engine
  4. Database connection configuration and persistent data manipulation through Object Relational Mapper
  5. Web security against CSRF(cross-site request forgery), SQL injections and other malicious attacks
  6. Session storage and retrievals

 

Do I have to use a web framework? 

Web developers agree that frameworks make their life easier by allowing for quicker customizations with less code and more focus on logic. Frameworks enable developers to rapidly develop and deploy applications, when it comes to web applications there is always more to do in terms of server-side logic, front-end design and interaction with server programs than just coding the application. We need to understand the server side architecture and then there will be an application running on the user’s browser which uses javascript. So it depends on the application requirement for web development whether to use web framework on not.

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:

  1. How do we map a requested URL to the code that is meant to handle it?
  2. 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:

  1. Django:

 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

 

  1. Pyramid:

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.

 

  1. TurboGears:

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.

 

  1. Web2py:

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.

 

  1. Flask:

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.

 

Want to expertise Python and It's Frameworks? Checkout Byte Academy's Python Bootcamp and Intro courses.

 

Written by lakshmi b

Featured