Python Web Frameworks

What is a web framework

A web software framework is a server-side application framework that is designed to support the development of dynamic websites, web applications, web services and web resources. The framework aims to alleviate the overhead associated with common activities performed in web development.

In Python, this means a Python module imported into your project. A Python web framework is a code library that makes the life of a web application developer much easier for building flexible, scalable and maintainable web applications.

Most common web framework functions

Almost all web frameworks provide the most basic functionality in their code or through extensions to perform common operations required to run web applications. These common operations include:

  1. URL routing
  2. HTML, XML, JSON, and other output format templating
  3. Database access, retrieval and manipulation
  4. Security against Cross-site request forgery (CSRF) and other malicious Internet attacks
  5. User session storage and authentication

Not all web frameworks include all of the necessary code for all of the above functions to work right out of the box. Some frameworks take the "kitchen-sink" approach to their framework feature set offering. So much so that everything possible comes pre-bundled with the framework while others more minimalist frameworks have a much simpler core package that works well with extensions and methods provided by other Python modules.

Django includes an ORM abstraction layer for SQLite3, MySQL and PostgreSQL, all relational databases for the most basic read, write, query, and delete operations. Django, with very little effort can map your database model to your administrative backend in a matter of minutes. It's pretty cool to see how quickly you can add most features.

Flask and Bottle are easy to use with their minimalist feature set and simple routing and straight-forward approach to templating. In Bottle, your app is self contained in a bottle.py file. Routing, Templating, the WSGI server, everything you need, packaged into a single file. It's simple to add a module from the Python Package Index and get stuff done, real fast.

I like to use Bottle with Requests for simple Web APIs. That will be discussed in great detail in later posts.

Why I Chose Django

Django is a widely-used Python web framework that bundles everything you will possible need for a web project.

Django, a web framework for perfectionists with deadlines

Django modules include management for Authentication, URL routing, a templating system, an object-relational mapper (ORM), and database schema migrations are all included with the Django framework. Compare that included functionality to the Flask framework which requires a separate library such as Flask-Login to perform user authentication.

From what I understand, several very popular websites like Pinterest and Instagram are Django projects.

Why Django is a good Python web framework choice?
The Django project's performance, flexibility, extensibility and huge user community has grown rapidly over the past 10 years since the framework's introduction.

There are a number of tutorials and Django best practices available on the Internet and in e-books. The framework continues to add new features with each new release.


>>> import django;print(django.get_version())
1.8.6

Django is a very good web framework as a starting place for new Python web developers.

Happy 10th birthday, Django.

Bottle and Flask

Flask is a Python web framework built with a very small core package and an extremely easy-to-extend philosophy.

Why is Flask a good web framework choice?
Flask is considered more Pythonic than Django because Flask's code is the most concise and explicit. Flask is simple and it's easy to get started as a newbie because there is so little starting code for getting a basic web app up and running quickly.

For example, here's a Hello, Monty web application with Flask:


from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, Monty!'

if __name__ == '__main__':
    app.run()

It doesn't get any more elegant than that.

Can't leave without a short Bottle example.


from bottle import route, run, template

@route('/hello/')
def index(name):
    return template('Hello {{name}}!', name=name)

run(host='localhost', port=8080)

Yes, that's it. The Bottle server starts and listens for connections on port 8080. Just navigate to http://localhost:8080/hello/Monty

The browser will display:
Hello Monty!

For a full list of Python web frameworks, follow this link to the Python Wiki.

Link

Craig Derington

Full Stack Developer. Linux, Docker, Python, Celery, Flask, Django, Go, MySQL, MongoDB and Git. Modern, secure, high-performance applications capable of processing millions of transactions a day.

comments powered by Disqus