Full Stack Python

# Web Frameworks

A web application framework is a collection of libraries that provide functionality to accomplish common operations for the web. These common operations include:

1. URL routing
2. HTML, XML, JSON, and other output format templating
3. Database manipulation
4. Security against Cross-site request forgery (CSRF) and other attacks

Not all web frameworks include functionality for all of the above functionality. Frameworks fall somewhere between simply executing a single use case and attempting to be everything to every developer with increased complexity.

For example, the Django web application framework includes an Object-Relational Mapping (ORM) layer that abstracts relational database read, write, query, and delete operations. However, Django's ORM cannot work without significant modification on non-relational databases such MongoDB and Riak. Other web frameworks such as Flask and Pyramid are generally easier to use with non-relational databases by incorporating external Python libraries. There is a spectrum between minimal functionality with easy extensibility and including everything in the framework with tight integration.

## Django

Django is a widely used Python web application framework with a "batteries-included" philosophy. The principle behind batteries-included is that the common functionality for building web applications should come with the framework instead of as a separate library. For example, URL routing, a templating system, object-relational mapper, and a database schema migrations (as of version 1.7) are all included with the Django library.

### Django resources

2 Scoops of Django by Daniel Greenfield and Audrey Roy is well worth the price of admission if you're serious about learning how to correctly develop Django websites.

Effective Django and Tango with Django are a great free introductions to using the most popular Python web framework.

DjangoCon US videos from 2013, 2012, 2011, as well as earlier US and DjangoCon EU conferences are all available free of charge.

The Django subreddit often has links to the latest resources for learning Django.

Lincoln Loop wrote a Django Best Practices guide for the community.

Steve Losh wrote an incredibly detailed Django Advice guide.

## Flask

Flask is a Python microframework deliberately built with a small core and easy extensibility philosophy. Flask is generally considered more "Pythonic" than Django because Flask web application code is often more explicit. Flask was also written several years after Django and therefore learned from the Python community's reactions as the framework evolved. Jökull Sólberg wrote a great piece articulating to this effect in his experience switching between Flask and Django.

### Flask resources

The 18 post series Flask mega tutorial is an absolutely amazing starting resource:

- Part 1: Hello World
- Part 2: Templates
- Part 3: Web Forms
- Part 4: Database
- Part 5: User Logins
- Part 6: Profile Page and Avatars
- Part 7: Unit Testing
- Part 8: Followers, Contacts, and Friends
- Part 9: Pagination
- Part 10: Full Text Search
- Part 11: Email Support
- Part 12: Facelift
- Part 13: Dates and Times
- Part 14: I18n and L10n
- Part 15: Ajax
- Part 16: Debugging, Testing and Profiling
- Part 17: Deployment on Linux
- Part 18: Deployment on the Heroku Cloud

Yes, there are many parts to the series. However, each post is focused on a single topic to contain the complexity. The whole series is well worth an in-depth read-through. The author is also writing the O'Reilly Flask Web Development book so consider picking that up as well.

The Flask Extensions Registry is a curated list of the best packages that extend Flask. It's the first location to look through when you're wondering how to do something that's not in the core framework.

Great post by Jeff Knupp on Productionizing a Flask App

The Plank & Whittle blog has two posts, one on Packaging a Flask web app and another on Packaging a Flask app in a Debian package once you've built an app and want to deploy it.

The tuts+ Flask tutorial is another great walkthrough for getting started with the framework.

## Bottle

Bottle is a WSGI-compliant single source file web framework with no external dependencies except for the standard library included with Python.

### Bottle resources

Digital Ocean provides an extensive introductory post on Bottle.

This post provides a short tutorial on getting started with Bottle.

Here's a short code snippet for creating a REST API with Bottle and MongoDB.

## Web Framework Logging

Logging is a common mechanism for monitoring web applications written with a web framework. Runtime exceptions that prevent code from running are important to log to investigate and fix the source of the problems. Informational and debugging logging also helps to understand how the application is performing even if code is working as intended.

Logging is often grouped into several categories:

1. Information
2. Debug
3. Warning
4. Error

Logging errors that occur while a web framework is running is crucial to understanding how your application is performing. Raven is a Python client for the Sentry exception logging and aggregation application. Raven provides the way to send exceptions to Sentry, which should be deployed on a separate server from your production infrastructure. Raven can also be used by Python scripts to send other log data to Sentry for aggregation. Sentry provides a clean web application interface for viewing the exceptions. Sentry can also be configured with a mail plugin to send emails when exceptions occur.

### Web Framework Resources

Pyramid, Falcon, web.py are the most common Python web frameworks other than Django, Flask and Bottle.

This roundup of 14 minimal Python frameworks contains both familiar and less known Python libraries.

Next read the task queues section.