Fork me on GitHub

Introduction

You're knee deep in learning the Python programming language. The syntax is starting to make sense. The first few "ahh-ha!" moments are hitting you as you're learning conditional statements, for loops and classes while playing around with the open source libraries that make Python such an amazing language.

Now you want to take your initial Python knowledge and make something real, like an application that's available on the web that you can show off or sell as a service to other people. That's where Full Stack Python comes in. You've come to the right place to learn everything you need to create, deploy and run a production Python web application.

Learning Programming

Learning to program is about understanding how to translate thoughts into source code that can be executed on computers to achieve one or more goals.

There are many steps in learning how to program, including

  1. setting up a development environment
  2. selecting a programming language, such as Python
  3. understanding the syntax and commands for the language
  4. writing code in the language, often using pre-existing code libraries and frameworks
  5. executing the program
  6. debugging errors and unexpected results
  7. deploying an application so it can run for intended users

How should I learn programming?

There are several schools of thought on how a person should start learning to program. One school of thought is that a lower-level programming language such as Assembly or C are the most appropriate languages to start with because they force new developers to write their own data structures, learn about pointers and generally work their way through the hard problems in computer science.

There's certainly wisdom in this "low-level first" philosophy because it forces a beginner to gain a strong foundation before moving on to higher level topics such as web and mobile application development. This philosophy is the one most commonly used in university computer science programs.

The atomic units of progress in the "low-level first" method of learning are

  1. aspects of programming language understood (type systems, syntax)
  2. number of data structures coded and able to be used (stacks, queues)
  3. algorithms in a developer's toolbelt (quicksort, binary search)

Another school of thought is that new developers should bootstrap themselves through working on projects in whatever programming language interests them enough to keep working through the frustrations that will undoubtably occur.

In this "project-based" line of thinking, the number of projects completed that expand a programmer's abilities are the units of progress. Extra value is placed on making the projects open source and working with experienced mentors to learn what he or she can improve on in their programs.

Should I learn Python first?

Python is good choice in the project-based approach because of the extensive availability of free and low cost introductory resources, many of which provide example projects to build upon.

Note that this question of whether or not Python is a good first language for an aspiring programmer is highly subjective and these approaches are not mutually exclusive. Python is also widely taught in universities to explain the fundamental concepts in computer science, which is in line with the "low-level first" philosophy than the projects-first method.

In a nutshell, whether Python is the right first programming language to learn is up to your own learning style and what feels right. If Ruby or Java seem like they are easier to learn than Python, go for those languages. Programming languages, and the ecosystems around them, are human-made constructs. Find one that appears to match your personal style and give it a try, knowing that whatever you choose you'll need to put in many long days and nights to really get comfortable as a software developer.

Why Use Python?

Python's expansive library of open source data analysis tools, web frameworks, and testing instruments make its ecosystem one of the largest out of any programming community.

Python is an accessible language for new programmers because the community provides many introductory resources. The language is also widely taught in universities and used for working with beginner-friendly devices such as the Raspberry Pi.

If you're learning about why to use Python you should also take a look at the best Python resources and read what "full stack" means.

Python's programming language popularity

Several programming language popularity rankings exist. While it's possible to criticize that these guides are not exact, every ranking shows Python as a top programming language within the top ten, if not the top five of all languages.

Most recently, the RedMonk January 2015 ranking had Python at #4.

The TIOBE Index, a long-running language ranking, has Python steady at #8.

The PopularitY of Programming Language (PYPL), based on leading indicators from Google Trends search keyword analysis, shows Python at #3.

GitHut, a visualization of GitHub language popularity, pegs Python at #3 overall as well.

These rankings provide a rough measure for language popularity. They are not intended as a precise measurement tool to determine exactly how many developers are using a language. However, the aggregate view shows that Python remains a stable programming language with a growing ecosystem.

Why does the choice of programming language matter?

Programming languages have unique ecosystems, cultures and philosophies built around them. You will find friction with a community and difficulty in learning if your approach to programming varies from the philosophy of the programming language you've selected.

Python's culture values open source software, community involvement with local, national and international events and teaching to new programmers. If those values are also important to you and/or your organization then Python may be a good fit.

The philosophy for Python is so strongly held that it's even embedded in the language as shown when the interpreter executes "import this" and displays The Zen of Python.

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

More perspectives on why to use Python

Python 2 or 3?

The Python programming language is currently in the midst of a long-term transition from version 2 to version 3. New programmers typically have many questions about which version they should learn. It's confusing to hear that Python 3, which was originally released in 2008, is still not the default installation on many operating systems.

Here's the good news: you can't go wrong starting with either version. While there are differences in unicode and syntax, for the most part if you start with Python 2 and then learn Python 3 you won't be starting from scratch. Likewise, you'll be able to read and write Python 2 code if you started with Python 3.

My personal recommendation for new programmers as of right now is to start with Python 3. There are enough great resources out there that teach version 3 from the ground up.

However, if you are interested in DevOps-type work with configuration management tools such as Ansible or Fabric, then you'll have to stick to Python 2 because they have yet to upgrade to support Python 3. If you know there are libraries you must use in a project, check the Python Walls of Superpowers. If you're using Django, there is also a wall specifically for Python 3 compatibility of popular Django packages.

Python 2 to 3 resources

Enterprise Python

One of the misconceptions around Python and other dynamically-typed languages is that they cannot be reliably used to build enterprise-grade software. However, almost all commercial and government enterprises already use Python in some capacity, either as glue code between disparate applications or to build the applications themselves.

What is enterprise software?

Enterprise software is built for the requirements of an organization rather than the needs of an individual. Software written for enterprises often needs to integrate with legacy systems, such as existing databases and non-web applications. There are often requirements to integrate with authentication systems such as the Lightweight Directory Access Protocol (LDAP) and Active Directory (AD).

Organizations develop enterprise software with numerous custom requirements to fit the specific needs of their operating model. Therefore the software development process often becomes far more complicated due to disparate factions within an organization vying for the software to handle their needs at the expense of other factions.

The complexity due to the many stakeholders involved in the building of enterprise software leads to large budgets and extreme scrutiny by non-technical members of an organization. Typically those non-technical people place irrational emphasis on the choice of programming language and frameworks when otherwise they should not make technical design decisions.

Why are there misconceptions about Python in enterprise environments?

Traditionally large organizations building enterprise software have used statically typed languages such as C++, .NET and Java. Throughout the 1980s and 1990s large companies such as Microsoft, Sun Microsystems and Oracle marketed these languages as "enterprise grade". The inherent snub to other languages was that they were not appropriate for CIOs' difficult technical environments. Languages other than Java, C++ and .NET were seen as risky and therefore not worthy of investment.

In addition, "scripting languages" such as Python, Perl and Ruby were not yet robust enough in the 1990s because their core standard libraries were still being developed. Frameworks such as Django, Flask and Rails (for Ruby) did not yet exist. The Web was just beginning and most enterprise applications were desktop apps built for Windows. Python simply wasn't made for such environments.

Why is Python now appropriate for building enterprise software?

From the early 2000s through today the languages and ecosystems for many dynamically typed languages have greatly improved and often surpassed some aspects of other ecosystems. Python, Ruby and other previously derided languages now have vast, well-maintained open source ecosystems backed by both independent developers and large companies including Microsoft, IBM, Google, Facebook, Dropbox, Twilio and many, many others.

Python's open source libraries, especially for web development and data analysis, are some of the best maintained and fully featured pieces of code for any language.

Meanwhile, some of the traditional enterprise software development languages such as Java have languished due to underinvestment by their major corporate backers. When Oracle purchased Sun Microsystems in 2009 there was a long lag time before Java was enhanced with new language features in Java 7. Oracle also bundles unwanted adware with the Java installation, whereas the Python community would never put up with such a situation because the language is open source and does not have a single corporate controller.

Other ecosystems, such as the .NET platform by Microsoft have fared much better. Microsoft continued to invest in moving the .NET platform along throughout the early part of the new millennium.

However, Microsoft's enterprise products often have expensive licensing fees for their application servers and associated software. In addition, Microsoft is also a major backer of open source, especially Python, and their Python tools for Visual Studio provide a top-notch development environment.

The end result is that enterprise software development has changed dramatically over the past couple of decades. CIOs and technical executives can no longer ignore the progress of Python and the great open source community in the enterprise software development landscape if they want to continue delivering business value to their business side customers.

Open source enterprise Python projects

  • Collab by the U.S. government's Consumer Financial Protection Bureau (CFPB) agency is a corporate intranet and collaboration platform for large organizations. The project is currently running and in-use by thousands of CFPB employees.

  • Pants is a build system for software projects with many distinct parts and built with many different programming languages as is often the case in large organizations.

Enterprise Python software development resources

Best Python Resources

The Python community is amazing at sharing detailed resources and helping beginners learn to program with the language. There are so many resources out there though that it can be difficult to know how to find them.

This page aggregates the best general Python resources with descriptions of what they provide to readers.

If you prefer to learn by watching videos check out the best Python videos. You may also be interested in reading about development environments to use for coding.

New to programming

If you're learning your first programming language these books were written with you in mind. Developers learning Python as a second or later language should skip down to the next section for "experienced developers".

Experienced developers new to Python

  • Learn Python in y minutes provides a whirlwind tour of the Python language. The guide is especially useful if you're coming in with previous software development experience and want to quickly grasp how the language is structured.

  • Python for you and me is an approachable book with sections for Python syntax and the major language constructs. The book also contains a short guide at the end to get programmers to write their first Flask web application.

  • Kenneth Reitz's The Hitchhiker’s Guide to Python contains a wealth of information both on the Python programming language and the community.

  • How to Develop Quality Python Code is a good read to begin learning about development environments, application dependencies and project structure.

Beyond the basics

  • The Python Ecosystem: An Introduction provides context for virtual machines, Python packaging, pip, virutalenv and many other topics after learning the basic Python syntax.

  • The Python Subreddit rolls up great Python links and has an active community ready to answer questions from beginners and advanced Python developers alike.

  • Good to Great Python Reads is a collection of intermediate and advanced Python articles around the web focused on nuances and details of the Python language itself.

  • The blog Free Python Tips provides posts on Python topics as well as news for the Python ecosystem.

  • Python Books is a collection of freely available books on Python, Django, and data analysis.

  • Python IAQ: Infrequently Asked Questions is a list of quirky queries on rare Python features and why certain syntax was or was not built into the language.

Videos, screencasts and presentations

Videos from conferences and meetups along with screencasts are listed on the best Python videos page.

Curated Python packages lists

  • awesome-python is an incredible list of Python frameworks, libraries and software. I wish I had this page when I was just getting started.

  • easy-python is like awesome-python although instead of just a Git repository this site is in the Read the Docs format.

Podcasts

  • Talk Python to Me focuses on the people and organizations coding on Python. Each episode features a different guest interviewee to talk about his or her work.

  • Podcast.__init__ is another podcast on "about Python and the people who make it great".

Newsletters

  • Python Weekly is a free weekly roundup of the latest Python articles, videos, projects and upcoming events.

  • PyCoder's Weekly is another great free weekly email newsletter similar to Python Weekly. The best resources are generally covered in both newsletters but they often cover different articles and projects from around the web.

  • Import Python is a newer newsletter than Python Weekly and PyCoder's Weekly. So far I've found this newsletter often pulls from different sources than the other two. It's well worth subscribing to all three so you don't miss anything.

Best Python Videos

If you prefer to learn Python programming by watching videos then this is the resource for you. I've watched hundreds of live technical talks and combed through videos to pick out the ones with great speakers who'll teach you the most about the language and ecosystem.

This page links to the best free videos as well as other video lists so you can do your own searching through the huge backlog of conference and meetup talks from the past several years.

Be sure to read the best Python resources for links to books and articles as well as web frameworks to learn more about web development.

Livecoding

Web development with Django, Flask and other frameworks

Core Python language videos

  • Jessica McKellar's Building and breaking a Python sandbox is a fascinating walk through the lower layers of the Python interpreter.

  • Brandon Rhodes' All Your Ducks In A Row: Data Structures in the Std Lib and Beyond goes through how data structures are implemented, how to select a data structure appropriate to your application and how the list and dictionary can be used in many situations.

  • The talk Python Descriptors by Simeon Franklin explains the what and why of this core Python language feature.

  • David Beazley gives an amazing live coded performance to show Python concurrency using threads, event loops and coroutines. David makes the live coding look easy but a whole lot of work must've gone into that talk.

Screencasts and class recordings

  • Google's Python Class contains lecture videos and exercises for learning Python.

  • While there aren't always live streams online, it's worth checking out the Python category on Livecoding.tv to see if anyone is streaming or has a recording of working on a Python project. Even experienced developers can learn a whole lot from watching other developer's work on their projects. I also stream a couple times per week so if you follow me on my username you'll get an alert when I go online.

Video compilations

  • PyVideo organizes and indexes thousands of Python videos from both major conferences and meetups.

  • Incredible Technical Speakers is a repository I put together that features software developer speakers talking about programming language agnostic topics. The list is intended to emphasize professional software developers who also have the ability to engage an audience of peers with an exciting talk.

Development Environments

A development environment is a combination of a text editor and the Python interpreter. The text editor allows you to write the code. The interpreter provides a way to execute the code you've written. A text editor can be as simple as Notepad on Windows or more complicated as a complete integrated development environment (IDE) such as PyCharm which runs on any major operating system.

Why is a development environment necessary?

Python code needs to be written, executed and tested to build applications. The text editor provides a way to write the code. The interpreter allows it to be executed. Testing to see if the code does what you want can either be done manually or by unit and functional tests.

While you're learning about development environments be sure to check out information on Vim and Emacs.

A development environment example

Here's what I (the author of Full Stack Python, Matt Makai) use to develop most of my Python applications. I have a Macbook Pro with Mac OS X as its base operating system. Ubuntu 14.04 LTS is virtualized on top with Parallels. My code is written in vim and executed with the Python 2.7.x interpreter via the command line. I use virtualenv to create separate Python interpreters with their own isolated application dependencies and virtualenvwrapper to quickly switch between the interpreters created by virtualenv.

That's a common set up but you can certainly write great code with a much less expensive set up or a cloud-based development environment.

Open source text editors

  • vim is my editor of choice and installed by default on most *nix systems.

  • emacs is another editor often used on *nix.

  • Atom is an open source editor built by the GitHub team.

Proprietary (closed source) editors

  • Sublime Text versions 2 and 3 (currently in beta) are popular text editors that can be extended with code completion, linting, syntax highlighting and other features using plugins.

  • Komodo is a cross-platform text editor and IDE for major languages including Python, Ruby, JavaScript, Go and more.

Python-specific IDEs

  • PyCharm is a Python-specific IDE built on JetBrains' platform. There are free editions for students and open source projects.

  • Wing IDE is a paid development environment with integrated debugging and code completion.

  • PyDev is a Python IDE plug in for Eclipse.

Hosted development environment services

In the past couple of years several cloud-based development environments have popped up. These can work great for when you're learning or stuck on a machine with only a browser but no way to install your own software. Most of these have free tiers for getting started and paid tiers as you scale up your application.

Development environment resources

Vim

Vim, short for Vi IMproved, is a configurable text editor often used as a Python development environment. Vim proponents commonly cite the numerous plugins, Vimscript and logical command language as major Vim strengths.

Why is Vim a good Python development environment?

Vim's philosophy is that developers are more productive when they avoid taking their hands off the keyboard. Code should flow naturally from the developer's thoughts through the keyboard and onto the screen. Using a mouse or other peripheral is a detriment to the rate at which a developer's thoughts become code.

Vim has a logical, structured command language. When a beginner is learning the editor she may feel like it is impossible to understand all the key commands. However, the commands stack together in a logical way so that over time the editor becomes predictable.

If you're interested in Vim you'll also want to read about development environments for coding and learn what makes Python a great programming language.

Configuring Vim with a Vimrc file

The Vimrc file is used to configure the Vim editor. A Vimrc file can range from nothing in it to very complicated with hundreds or thousands of lines of configuration commands.

Here's a short, commented example .vimrc file I use for Python development to get a feel for some of the configuration statements:

" enable syntax highlighting
syntax enable

" show line numbers
set number

" set tabs to have 4 spaces
set ts=4

" indent when moving to the next line while writing code
set autoindent

" expand tabs into spaces
set expandtab

" when using the >> or << commands, shift lines by 4 spaces
set shiftwidth=4

" show a visual line under the cursor's current line
set cursorline

" show the matching part of the pair for [] {} and ()
set showmatch

" enable all Python syntax highlighting features
let python_highlight_all = 1

Here is how these configuration options look with a dark background on Mac OS X while editing the markdown for this webpage (how meta!).

Vim with basic configuration options on a dark background.

Take a look at another example using these configuration options, this time with a light background and editing Python code from my Choose Your Own Adventures Presentations project.

Vim with basic configuration options on a white background.

The Vimrc file lives under the home directory of the user account running Vim. For example, when my user account is 'matt', on Mac OS X my Vimrc file is found at /Users/matt/.vimrc. On Ubuntu Linux my .vimrc file can be found within the /home/matt/ directory.

If a Vimrc file does not already exist, just create it within the user's home directory and it will be picked up by Vim the next time you open the editor.

Vim tutorials

Vim has a reputation for a difficult learning curve, but it's much easier to get started with these tutorials.

  • Vim Adventures is a cute, fun browser-based game that helps you learn Vim commands by playing through the adventure.

  • Learn Vim Progressively is a wonderful tutorial that follows the path I took when learning Vim: learn just enough to survive with it as your day-to-day editor then begin adding more advanced commands on top.

  • A vim Tutorial and Primer is an incredibly deep study in how to go from beginner to knowledgeable in Vim.

  • Vim as a Language explains the language syntax and how you can build up over time to master the editor.

  • How to install and use Vim on a cloud server along with How to use Vim for advanced editing of code on a VPS are two detailed Digital Ocean guides for getting up and running with Vim, regardless of whether you're using it locally or on a cloud server.

  • In Vim: revisited the author explains his on-again off-again relationship with using Vim. He then shows how he configures and uses the editor so it sticks as his primary code editing tool.

Vimrc resources

These are a few resources for learning how to structure a .vimrc file. I recommend adding configuration options one at a time to test them individually instead of going whole hog with a Vimrc you are unfamiliar with.

Vim installation guides

These installation guides will help you get Vim up and running on Mac OS X, Linux and Windows.

  • Upgrading Vim on OS X explains why to upgrade from Vim 7.2 to 7.3+ and how to do it using Homebrew.

  • The easiest way to install Vim on Windows 7+ is to download and run the gvim74.exe file.

  • On Linux make sure to install the vim package with sudo apt-get install vim.

  • If you're using PyCharm as your IDE you won't need to install Vim as a separate text editor - instead use the IdeaVim PyCharm plugin to get Vim keybindings, visual/insert mode, configuration with ~/.ideavimrc and other Vim emulation features.

Using Vim as a Python IDE

Once you get comfortable with Vim as an editor, there are several configuration options and plugins you can use to enhance your Python productivity. These are the resources and tutorials to read when you're ready to take that step.

  • VIM and Python - a Match Made in Heaven details how to set up a powerful VIM environment geared towards wrangling Python day in and day out.

  • The python-mode project is a Vim plugin with syntax highlighting, breakpoints, PEP8 linting, code completion and many other features you'd expect from an integrated development environment.

  • Vim as Your IDE discusses how to set up Vim for greater productivity once you learn the initial Vim language for using the editor.

  • Vim as a Python IDE goes through the steps necessary to make Vim into a more comfortable environment for Python development.

  • Setting up Vim for Python has a well written answer on Stack Overflow for getting started with Vim.

  • If you're writing your documentation in Markdown using Vim, be sure to read this insightful post on a Vim setup for Markdown.

Vim Plugin Managers

  • Vundle comes highly recommended as a plugin manager for Vim.

  • Pathogen is a widely used used plugin manager.

  • Vim-plug bills itself as a minimalistic Vim plugin manager.

Vim Plugin resources

Emacs

Emacs is an extensible text editor that can be customized by writing Lisp code.

Why is Emacs a good choice for Python coding?

Emacs is designed to be customized via the built-in Lisp interpreter and package manager. The package manager, named package.el, has menus for handling installation. The largest Lisp Package Archive is Melpa, which provides automatic updates from upstream sources.

Macros are useful for performing repetitive actions in Emacs. A macro is just a recording of a previous set of keystrokes that can be replayed to perform future actions.

Hooks, which are Lisp variables that hold lists of functions to call, provide an extension mechanism for Emacs. For example, kill-emacs-hook runs before exiting Emacs so functions can be loaded into that hook to perform necessary actions before the exiting completes.

While you're reading about coding Python in Vim be sure to also learn about which web frameworks to use as well as how to deploy an application.

General Emacs resources

Notable Elisp Packages

  • Magit allows the user to inspect and modify Git repositories from within Emacs.

  • company-mode creates a modular in-buffer completion framework.

  • Flycheck provides syntax checking.

  • anaconda-mode is specific to Python development and allows code navigation, documentation lookup and code completion. The jedi library is used under the hood.

  • Tern is a stand-alone code-analysis engine for JavaScript. It can be integrated within a Django project via the tern-django package.

Popular user configurations

  • Prelude is an enhanced Emacs version 24 distribution.

  • A reasonable Emacs config shows a batteries-includes Emacs configuration bundle.

  • Emacs settings is a repository of configurations used in the Emacs Rocks screencasts.

  • Spacemacs mashes together Emacs' extensibility and Vim's ergonomic text editing features.

Generators

Generators are a Python core language construct that allow a function's return value to to behave as an iterator. A generator can allow more efficient memory usage by allocating and deallocating memory during the context of a large number of iterations. Generators are defined in PEP255 and included in the language as of Python 2.2 in 2001.

Python generator resources

Comprehensions

Comprehensions are a Python language construct for concisely creating data in lists, dictionaries and sets. List comprehensions are included in Python 2 while dictionary and set comprehensions were introduced to the language in Python 3.

Why are comprehensions important?

Comprehensions are a more clear syntax for populating conditional data in the core Python data structures. Creating data without comprehensions often involves nested loops with conditionals that can be difficult for code readers to properly evaluate.

Example code

List comprehension:

>>> double_digit_evens = [e*2 for e in range(5, 50)]
>>> double_digit_evens
[10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]

Set comprehension:

>>> double_digit_odds = {e*2+1 for e in range(5, 50)}
{11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99}

Dictionary comprehension:

>>> {e: e*10 for e in range(1, 11)}
{1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60, 7: 70, 8: 80, 9: 90, 10: 100}

Comprehension resources

Web Development

Web development is the umbrella term for conceptualizing, creating, deploying and operating web applications and application programming interfaces for the Web.

Why is web development important?

The Web has grown a mindboggling amount in the number of sites, users and implementation capabilities since the first website went live in 1989. Web development is the concept that encompasses all the activities involved with websites and web applications.

How does Python fit into web development?

Python can be used to build server-side web applications. While a web framework is not required to build web apps, it's rare that developers would not use existing open source libraries to speed up their progress in getting their application working.

Python is not used in a web browser. The language executed in browsers such as Chrome, Firefox and Internet Explorer is JavaScript. Projects such as pyjs can compile from Python to JavaScript. However, most Python developers are write their web applications in a combination of Python, which is executed on the server side and JavaScript, which is downloaded to the web browser client and executed there.

Web development resources

  • Web application development is different and better provides some context for how web development has evolved from writing static HTML files into the complex JavaScript client-side applications produced today.

  • The Evolution of the Web visualizes how web browsers and related technologies have changed over time as well as the overall growth of the Internet in the amount of data transferred. Note that the visualization unfortunately stops around the beginning of 2013 but it's a good way to explore what happened in the first 24 years.

  • Three takeaways for web developers after two weeks of painfully slow Internet is a must-read for every web developer. Not everyone has fast Internet service, whether because they are in a remote part of the world or they're just in a subway tunnel. Optimizing sites so they work in those situations is important for keeping your users happy.

Web frameworks

A web framework is a code library that makes a developer's life easier when building reliable, scalable and maintainable web applications.

Why are web frameworks necessary?

Web frameworks encapsulate what developers have learned over the past twenty years while programming sites and applications for the web. Frameworks make it easier to reuse code for common HTTP operations and to structure projects so other developers with knowledge of the framework can quickly build and maintain the application.

Common web framework functionality

Frameworks provide 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 manipulation
  4. Security against Cross-site request forgery (CSRF) and other attacks
  5. Session storage and retrieval

Not all web frameworks include code for all of the above functionality. Frameworks fall on the spectrum from executing a single use case to providing every known web framework feature to every developer. Some frameworks take the "batteries-included" approach where everything possible comes bundled with the framework while others have a minimal core package that is amenable to extensions provided by other packages.

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 as MongoDB.

Some other web frameworks such as Flask and Pyramid are easier to use with non-relational databases by incorporating external Python libraries. There is a spectrum between minimal functionality with easy extensibility on one end and including everything in the framework with tight integration on the other end.

Comparing web frameworks

Are you curious about how the code in a Django project is structured compared with Flask? Check out this Django web application tutorial and then view the same application built with Flask.

There is also a repository called compare-python-web-frameworks where the same web application is being coded with varying Python web frameworks, templating engines and object-relational mappers.

While you're learning about web frameworks you should also study web application deployment and web APIs.

Do I have to use a web framework?

Whether or not you use a web framework in your project depends on your experience with web development and what you're trying to accomplish. If you are a beginner programmer and just want to work on a web application as a learning project then a framework can help you understand the concepts listed above, such as URL routing, data manipulation and authentication that are common to the majority of web applications.

On the other hand if you're an experienced programmer with significant web development experience you may feel like the existing frameworks do not match your project's requirements. In that case, you can mix and match open source libraries such as Werkzeug for WSGI plumbing with your own code to create your own framework. There's still plenty of room in the Python ecosystem for new frameworks to satisfy the needs of web developers that are unmet by Django, Flask, Pyramid, Bottle and many others.

In short, whether or not you need to use a web framework to build a web application depends on your experience and what you're trying to accomplish. Using a web framework to build a web application certainly isn't required, but it'll make most developers' lives easier in many cases.

Web framework resources

Web frameworks learning checklist

  1. Choose a major Python web framework (Django or Flask are recommended) and stick with it. When you're just starting it's best to learn one framework first instead of bouncing around trying to understand every framework.

  2. Work through a detailed tutorial found within the resources links on the framework's page.

  3. Study open source examples built with your framework of choice so you can take parts of those projects and reuse the code in your application.

  4. Build the first simple iteration of your web application then go to the deployment section to make it accessible on the web.

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 separate libraries.

Official Django logo. Trademark Django Software Foundation.

For example, authentication, URL routing, a templating system, an object-relational mapper (ORM), and database schema migrations (as of version 1.7) 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.

The batteries-included and extensibility philosophies are simply two different ways to tackle framework building. Neither philosophy is inherently better than the other one.

Why is Django a good web framework choice?

The Django project's stability, performance and community have grown tremendously over the past decade since the framework's creation. Detailed tutorials and good practices are readily available on the web and in books. The framework continues to add significant new functionality such as database migrations with each release.

I highly recommend the Django framework as a starting place for new Python web developers because the official documentation and tutorials are some of the best anywhere in software development. Many cities also have Django-specific groups such as Django District, Django Boston and San Francisco Django so new developers can get help when they are stuck.

There's some debate on whether learning Python by using Django is a bad idea. However, that criticism is invalid if you take the time to learn the Python syntax and language semantics first before diving into web development.

While you're learning about Django you should also read up on web frameworks and check out the Bottle framework.

Django books and tutorials

There are a slew of free or low cost resources out there for Django. Since Django was released over 10 years ago and has had a huge number of updates since then, when you're looking for an up-to-date Django book check out the list below or read this post showing current Django books as of Django 1.7.

Django videos

Looking for Django videos instead of just articles? There's a special section for Django and web development on the Best Python Videos page.

Django migrations

  • Paul Hallett wrote a detailed Django 1.7 app upgrade guide on the Twilio blog from his experience working with the django-twilio package.

  • Real Python's migrations primer explores the difference between South's migrations and the built-in Django 1.7 migrations as well as how you use them.

  • Andrew Pinkham's "Upgrading to Django 1.7" series is great learning material for understanding what's changed in this major release and how to adapt your Django project. Part 1, part 2 and part 3 and part 4 are now all available to read.

  • Django migrations without downtimes shows one potential way of performing on-line schema migrations with Django.

Django testing

Django with Angular (Djangular) resources

Django ORM resources

Django comes with its own custom object-relational mapper (ORM) typically referred to as "the Django ORM". Learn more about the Django ORM on the Python object-relational mappers page that includes a section specifically for the Django ORM as well as additional resources and tutorials.

Static and media files

Deploying and handling static and media files can be confusing for new Django developers. These resources along with the static content page are useful for figuring out how to handle these files properly.

Open source Django example projects

  • Browser calls with Django and Twilio shows how to build a web app with Django and Twilio Client to turn a user's web browser into a full-fledged phone. Pretty awesome!

  • Txt 2 React is a full Django web app that allows audiences to text in during a presentation with feedback or questions.

  • Openduty is a website status checking and alert system similar to PagerDuty.

  • Courtside is a pick up sports web application written and maintained by the author of PyCoder's Weekly.

  • These two Django Interactive Voice Response (IVR) system web application repositories part 1 and part 2 show you how to build a really cool Django application. There's also an accompanying blog post with detailed explanations of each step.

  • Taiga is a project management tool built with Django as the backend and AngularJS as the front end.

Django project templates

Django learning checklist

  1. Install Django on your local development machine.

  2. Work through the initial "polls" tutorial.

  3. Build a few more simple applications using the tutorial resources found in the "Django resources" section.

  4. Start coding your own Django project with help from the official documentation and resource links below. You'll make plenty of mistakes which is critical on your path to learning the right way to build applications.

  5. Read 2 Scoops of Django to understand Django good practices and learn better ways of building Django web applications.

  6. Move on to the deployment section to get your Django project on the web.

Flask

Flask is a Python web framework built with a small core and easy-to-extend philosophy. Official Flask logo. Flask Artwork License.

Why is Flask a good web framework choice?

Flask is considered more Pythonic than Django because Flask web application code is in most cases more explicit. Flask is easy to get started with as a beginner because there is little boilerplate code for getting a simple app up and running.

For example, here's a valid "hello world" web application with Flask (the equivalent in Django would be significantly more code):

from flask import Flask
app = Flask(__name__)

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

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

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.

If you're learning about Flask you should also understand web frameworks and read how to deploy web applications.

Flask resources

Open source Flask example projects

Flask project templates

Flask framework learning checklist

  1. Install Flask on your local development machine.

  2. Work through the 18-part Flask tutorial listed first under "Flask resources" above.

  3. Read through Flask Extensions Registry to find out what extensions you'll need to build your project.

  4. Start coding your Flask app based on what you learned from the 18 part Flask tutorial plus open source example applications found below.

  5. Move on to the deployment section to get your initial Flask project on the web.

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

Open source Bottle example projects

  • Pattle is a pastebin clone built with Bottle.

  • Decanter is a library for structuring Bottle projects.

Bottle framework learning checklist

  1. Download Bottle or install via pip with pip install bottle on your local development machine.

  2. Work through the official Bottle tutorial.

  3. Start coding your Bottle app based on what you learned in the official tutorial plus reading open source example applications found above.

  4. Move on to the deployment section to get your initial Bottle application on the web.

Pyramid

Pyramid is an open source WSGI web framework based on the Model-View-Controller (MVC) architectural pattern.

Pyramid web framework logo.

Open source Pyramid example apps

Pyramid resources

Morepath

Morepath is a micro web framework with a model-driven approach to creating web applications and web APIs.

Morepath's framework philosophy is that the data models should drive the creation via the web framework. By default the framework routes URLs directly to model code, unlike for example Django which requires explicit URL routing by the developer.

Why is Morepath an interesting web framework?

Simple CRUD web applications and APIs can be tedious to build when they are driven straight from data models without much logic between the model and the view. Learn more about how Morepath compares with other web frameworks from the creator.

With the rise of front end JavaScript frameworks, many Python web frameworks are first being used to build RESTful APIs that return JSON instead rendering HTML via a templating system. Morepath appears to have been created with the RESTful API model approach in mind and cuts out the assumption that templates will drive the user interface.

Morepath resources

Other Web Frameworks

Python has a significant number of web frameworks outside the usual Django, Flask, Pyramid and Bottle suspects.

TurboGears2

TurboGears2 born as a full stack layer on top of Pylons is now a standalone web framework that can act both as a full stack solution (like Django) or as a micro framework.

Falcon

Falcon is a minimalist web framework designed with web application speed as a top priority.

web.py

web.py is a Python web framework designed for simplicity in building web applications.

web2py

Web2py is a batteries-included philosophy framework with project structure based on model-view-controller patterns.

CherryPy

CherryPy is billed as a minimalist web framework, from the perspective of the amount of code needed to write a web application using the framework. The project has a long history and made a major transition between the second and third release. There's an interesting recent discussion about moving the project forward, especially the number of open outstanding issues that exist in the tracker.

Muffin

Muffin is a web framework built on top of the asyncio module in the Python 3.4+ standard library. Muffin takes inspiration from Flask with URL routes defined as decorators upon view functions. The Peewee ORM is used instead of the more common SQLAlchemy ORM.

Other web framework resources

Other frameworks learning checklist

  1. Read through the web frameworks listed above and check out their project websites.

  2. It's useful to know what other web frameworks exist besides Django and Flask. However, when you're just starting to learn to program there are significantly more tutorials and resources for Django and Flask on the web. My recommendation is to start with one of those two frameworks then expand your knowledge from there.

Web Design

Web design is the creation of a web application's style and user interaction using CSS and JavaScript.

Why is web design important?

You wouldn’t use a web application that looked like the following screenshot, would you?

HTML with no CSS or JavaScript.

Creating web pages with their own style and interactivity so users can easily accomplish their tasks is a major part of building modern web applications.

Responsive design

Separating the content from the rules for how to display the content allows devices to render the output differently based on factors such as screen size and device type. Displaying content differently based on varying screen attributes is often called responsive design. The responsiveness is accomplished by implementing media queries in the CSS.

For example, a mobile device does not have as much space to display a navigation bar on the side of a page so it is often pushed down below the main content. The Bootstrap Blog example shows that navigation bar relocation scenario when you resize the browser width.

Design resources

  • Web Design Repo is a one stop shop for links to blogs, podcasts, inspiration, tutorials and tools related to web design.

  • Frontend Guidelines is an amazing write up of good practices for HTML, CSS and JS.

  • The Bootstrapping Design book is one of the clearest and concise resources for learning design that I've ever read. Highly recommended especially if you feel you have no design skills but need to learn them.

  • Learn Design Principles is a well thought out clear explanation for how to think about design according to specific rules such as axis, symmetry, hierarchy and rhythm.

  • Kuler is a complementary color picker by Adobe that helps choose colors for your designs.

  • If you want to learn more about how browsers work behind the scenes, here's a blog post series on building a browser engine that will show you how to build a simple rendering engine.

Cascading Style Sheets (CSS)

Cascading Style Sheet (CSS) files contain rules for how to display and lay out the HTML content when it is rendered by a web browser.

Why is CSS necessary?

CSS separates the content contained in HTML files from how the content should be displayed. It is important to separate the content from the rules for how it should be rendered primarily because it is easier to reuse those rules across many pages. CSS files are also much easier to maintain on large projects than styles embedded within the HTML files.

How is CSS retrieved from a web server?

The HTML file sent by the web server contains a reference to the CSS file(s) needed to render the content. The web browser requests the CSS file after the HTML file as shown below in a screenshot captured of the Chrome Web Developer Tools network traffic.

Google Chrome Web Developer Tools shows how CSS is separate from the HTML content.

That request for the fsp.css file is made because the HTML file for Full Stack Python contains a reference to theme/css/fsp.css which is shown in the view source screenshot below.

View source screenshot for the fsp.css file in index.html.

CSS preprocessors

A CSS preprocessor compiles a processed language into plain CSS code. CSS preprocessing languages add syntax such as variables, mixins and functions to reduce code duplication. The additional syntax also makes it possible for designers to use these basic programming constructs to write maintainable front end code.

  • Sass is currently the favored preprocessor in the design community. Sass is considered the most powerful CSS preprocessor in terms of advanced language features.

  • LESS is right up there with Sass and has an ace up its sleeve in that the Bootstrap Framework is written in LESS which brings up its popularity.

  • Stylus is often cited as the third most popular CSS preprocessing language.

CSS preprocessor resources

  • The Advanced Guide to HTML and CSS book has a well-written chapter on preprocessors.

  • Sass vs LESS provides a short answer on which framework to use then a longer more detailed response for those interested in understanding the details.

  • How to choose the right CSS preprocessor has a comparison of Sass, LESS and Stylus.

  • Musings on CSS preprocessors contains helpful advice ranging from how to work with preprocessors in a team environment to what apps you can use to aid your workflow.

CSS frameworks

CSS frameworks provide structure and a boilerplate base for building a web application's design.

CSS resources

CSS learning checklist

  1. Create a simple HTML file with basic elements in it. Use the python -m SimpleHTTPServer command to serve it up. Create a <style></style> element within the <head> section in the HTML markup. Play with CSS within that style element to change the look and feel of the page.

  2. Check out front end frameworks such as Bootstrap and Foundation and integrate one of those into the HTML page.

  3. Work through the framework's grid system, styling options and customization so you get comfortable with how to use the framework.

  4. Apply the framework to your web application and tweak the design until you have something that looks much better than generic HTML.

JavaScript

JavaScript is a small scripting programming language embedded in web browsers to enable dynamic content and interaction.

Why is JavaScript necessary?

JavaScript executes in the client and enables dynamic content and interaction that is not possible with HTML and CSS alone. Every modern Python web application uses JavaScript on the front end.

Front end frameworks

Front end JavaScript frameworks move the rendering for most of a web application to the client side. Often these applications are informally referred to as "one page apps" because the webpage is not reloaded upon every click to a new URL. Instead, partial HTML pages are loaded into the document object model or data is retrieved through an API call then displayed on the existing page.

Examples of these front end frameworks include:

Front end frameworks are rapidly evolving. Over the next several years consensus about good practices for using the frameworks will emerge.

How did JavaScript originate?

JavaScript is an implementation of the ECMAScript specification which is defined by the Ecma International Standards Body.

JavaScript resources

  • Frontend tooling in 2015 shows the results of a survey for what frontend developers are using for CSS pre- and post-processing and other build steps.

  • How Browsers Work is a great overview of both JavaScript and CSS as well as how pages are rendered in a browser.

  • A re-introduction to JavaScript by Mozilla walks through the basic syntax and operators.

  • Coding tools and JavaScript libraries is a huge list by Smashing Magazine with explanations for each tool and library for working with JavaScript.

  • Superhero.js is an incredibly well designed list of resources for how to test, organize, understand and generally work with JavaScript.

  • Unheap is an amazing collection of reusable JQuery plugins for everything from navigation to displaying media.

  • The State of JavaScript in 2015 is an opinion piece about favoring small, single-purpose JavaScript libraries over larger frameworks due to churn in the ecosystem.

  • The Modern JavaScript Developer’s Toolbox provides a high-level overview of tools frequently used on the client and server side for developers using JavaScript in their web applications.

JavaScript learning checklist

  1. Create a simple HTML file with basic elements in it. Use the python -m SimpleHTTPServer command to serve it up. Create a <script type="text/javascript"></script> element at the end of the <body> section in the HTML page. Play with JavaScript within that element to learn the basic syntax.

  2. Download JQuery and add it to the page above your JavaScript element. Start working with JQuery and learning how it makes basic JavaScript easier.

  3. Work with JavaScript on the page. Incorporate examples from open source projects listed below as well as JQuery plugins. Check out Unheap to find a large collection of categorized JQuery plugins.

  4. Check out the JavaScript resources below to learn more about advanced concepts and open source libraries.

  5. Integrate JavaScript into your web application and check the static content section for how to host the JavaScript files.

WebSockets

A WebSocket is a standard protocol for two-way data transfer between a client and server. The WebSockets protocol does not run over HTTP, instead it is a separate implementation on top of TCP.

Why use WebSockets?

A WebSocket connection allows full-duplex communication between a client and server so that either side can push data to the other through an established connection. The reason why WebSockets, along with the related technologies of Server-sent Events (SSE) and WebRTC data channels, are important is that HTTP is not meant for keeping open a connection for the server to frequently push data to a web browser. Previously, most web applications would implement long polling via frequent Asynchronous JavaScript and XML (AJAX) requests as shown in the below diagram.

Long polling via AJAX is incredibly inefficient for some applications.

Server push is more efficient and scalable than long polling because the web browser does not have to constantly ask for updates through a stream of AJAX requests.

WebSockets are more efficient than long polling for server sent updates.

While the above diagram shows a server pushing data to the client, WebSockets is a full-duplex connection so the client can also push data to the server as shown in the diagram below.

WebSockets also allow client push in addition to server pushed updates.

The WebSockets approach for server- and client-pushed updates works well for certain categories of web applications such as chat room, which is why that's often an example application for a WebSocket library.

Implementing WebSockets

Both the web browser and the server must implement the WebSockets protocol to establish and maintain the connection. There are important implications for servers since WebSockets connections are long lived, unlike typical HTTP connections.

A multi-threaded or multi-process based server cannot scale appropriately for WebSockets because it is designed to open a connection, handle a request as quickly as possible and then close the connection. An asynchronous server such as Tornado or Green Unicorn monkey patched with gevent is necessary for any practical WebSockets server-side implementation.

On the client side, it is not necessary to use a JavaScript library for WebSockets. Web browsers that implement WebSockets will expose all necessary client-side functionality through the WebSockets object.

However, a JavaScript wrapper library can make a developer's life easier by implementing graceful degradation (often falling back to long-polling when WebSockets are not supported) and by providing a wrapper around browser-specific WebSocket quirks. Examples of JavaScript client libraries and Python implementations are found below.

JavaScript client libraries

  • Socket.io's client side JavaScript library can be used to connect to a server side WebSockets implementation.

  • web-socket-js is a Flash-based client-side WebSockets implementation.

Python implementations

  • Autobahn uses Twisted or asyncio to implement the WebSockets protocol.

  • Crossbar.io builds upon Autobahn and includes a separate server for handling the WebSockets connections if desired by the web app developer.

Nginx WebSocket proxying

Nginx officially supports WebSocket proxying as of version 1.3. However, you have to configure the Upgrade and Connection headers to ensure requests are passed through Nginx to your WSGI server. It can be tricky to set this up the first time.

Here are the configuration settings I use in my Nginx file as part of my WebSockets proxy.

# this is where my WSGI server sits answering only on localhost
# usually this is Gunicorn monkey patched with gevent
upstream app_server_wsgiapp {
  server localhost:5000 fail_timeout=0;
}

server {

  # typical web server configuration goes here

  # this section is specific to the WebSockets proxying
  location /socket.io {
    proxy_pass http://app_server_wsgiapp/socket.io;
    proxy_redirect off;

    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_read_timeout 600;
  }
}

The following resources are also helpful for setting up the configuration properly.

Open source Python examples with WebSockets

  • The python-websockets-example contains code to create a simple web application that provides WebSockets using Flask, Flask-SocketIO and gevent.

  • The Flask-SocketIO project has a chat web application that demos sending server generated events as well as input from users via a text box input on a form.

General WebSockets resources

  • The official W3C candidate draft for WebSockets API and the working draft for WebSockets are good reference material but can be tough for those new to the WebSockets concepts. I recommend reading the working draft after looking through some of the more beginner-friendly resources list below.

  • WebSockets 101 by Armin Ronacher provides a detailed assessment of the subpar state of HTTP proxying in regards to WebSockets. He also discusses the complexities of the WebSockets protocol including the packet implementation.

  • The "Can I Use?" website has a handy WebSockets reference chart for which web browsers and specific versions support WebSockets.

  • Mozilla's Developer Resources for WebSockets is a good place to find documentation and tools for developing with WebSockets.

  • websocketd is a WebSockets server aiming to be the "CGI of WebSockets". Worth a look.

Python-specific WebSockets resources

  • Real-time in Python provides Python-specific context for how the server push updates were implemented in the past and how Python's tools have evolved to perform server side updates.

  • websockets is a WebSockets implementation for Python 3.3+ written with the asyncio module (or with Tulip if you're working with Python 3.3).

  • The Choose Your Own Adventure Presentations tutorial uses WebSockets via gevent on the server and socketio.js for pushing vote count updates from the server to the client.

  • Adding Real Time to Django Applications shows how to use Django and Crossbar.io to implement a publish/subscribe feature in the application.

  • Async with Bottle shows how to use greenlets to support WebSockets with the Bottle web framework.

  • If you're deploying to Heroku, there is a specific WebSockets guide for getting your Python application up and running.

  • The Reddit thread for this page has some interesting comments on what's missing from the above content that I'm working to address.

Template Engines

Template engines process template files, which provide an intermediate format between your Python code and a desired output format, such as HTML or PDF.

Why are template engines important?

Template engines allow developers to generate a desired content type, such as HTML, while using some of the data and programming constructs such as conditionals and for loops to manipulate the output. Template files that are created by developers and then processed by the template engine consist of prewritten markup and template tag blocks where data is inserted.

For example, look at the first ten source lines of HTML of this webpage:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="author" content="Matt Makai">
    <meta name="description" content="Template engines provide programmatic output of formatted content such as HTML, XML or PDF.">
    <link rel="shortcut icon" href="//static.fullstackpython.com/fsp-fav.png">

Every one of the HTML lines above is standard for each page on Full Stack Python, with the exception of the <meta name="description"... line which provides a unique short description of what the individual page contains.

The base.html Jinja template used to generate Full Stack Python allows every page on Full Stack Python to have consistent HTML but dynamically generate the pieces that should change between pages when the static site generator executes. The below code from the base.html template shows that the meta description is up to child templates to create.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="author" content="Matt Makai">
    {% block meta_header %}{% endblock %}
    <link rel="shortcut icon" href="//static.fullstackpython.com/fsp-fav.png">

In a typical WSGI application, the template engine would generate the HTML output response when an HTTP request comes in for a particular URL.

Python template engines

There are several popular Python template engines. Template engines have to walk the spectrum between allowing arbitrary code execution and granting only a limited set of capabilities via template tags.

Jinja

Jinja, also known as "Jinja2" is a popular Python template engine written as an independent open source project, unlike some template engines that are provided as part of a larger web framework.

Major Python open source applications such as the configuration management tools Ansible and SaltStack as well as the static site generator Pelican use the Jinja template engine by default for generating output files.

Django templating

Django comes with its own template engine in addition to supporting (as of Django 1.9) drop-in replacement with other template engines such as Jinja.

Mako

Mako is the default templating engine for the Pyramid web framework and has wide support as a replacement template engine for many other web frameworks.

Template engine resources

Web Application Security

Website security must be thought about while building every level of the web stack. However, this section includes topics that deserve particular treatment, such as cross-site scripting (XSS), SQL injection, cross-site request forgery and usage of public-private keypairs.

Security open source projects

HTTPS resources

General security resources

Web security learning checklist

  1. Read and understand the major web application security flaws that are commonly exploited by malicious actors. These include cross-site request forgery (CSRF), cross-site scripting (XSS), SQL injection and session hijacking. The OWASP top 10 web application vulnerabilities list is a great place to get an overview of these topics.

  2. Determine how the framework you've chosen mitigates these vulnerabilities.

  3. Ensure your code implements the mitigation techniques for your framework.

  4. Think like an attacker and actively work to break into your own system. If you do not have enough experience to confidently break the security consider hiring a known white hat attacker. Have her break the application's security, report the easiest vulnerabilities to exploit in your app and help implement protections against those weaknesses.

  5. Recognize that no system is ever totally secure. However, the more popular an application becomes the more attractive a target it is to attackers. Reevaluate your web application security on a frequent basis.

Static Site Generator

A static website generator combines a markup language, such as Markdown or reStructuredText, with a templating engine such as Jinja, to produce HTML files. The HTML files can be hosted and served by a web server or content delivery network (CDN) without any additional dependencies such as a WSGI server.

Why are static site generators useful?

Static content files such as HTML, CSS and JavaScript can be served from a content delivery network (CDN) for high scale and low cost. If a statically generated website is hit by high concurrent traffic it will be easily served by the CDN without dropped connections.

For example, when Full Stack Python was on the top of Hacker News for a weekend, GitHub Pages was used as a CDN to serve the site and didn't have any issues even with close to 400 concurrent connections at a time, as shown in the following Google Analytics screenshot captured during that traffic burst.

Example of how static websites scale with a CDN based on Full Stack Python on Hacker News front page traffic.

How do static website generators work?

Static site generators allow a user to create HTML files by writing in a markup language and coding template files. The static site generator then combines the markup language and templates to produce HTML. The output HTML does not need to be maintained by hand because it is regenerated every time the markup or templates are modified.

For example, as shown in the diagram below, the Pelican static site generator can take in reStructuredText files and Jinja2 template files as input then combine them to output a set of static HTML files.

Example of how static site generators work with a markup language and templates.

What's the downside to using static site generators?

The major downside is that code cannot be executed after a site is created. You are stuck with the output files so if you're used to building web applications with a traditional web framework you'll have to change your expectations.

Content that is typically powered by a database, such as comments, sessions and user data can only be handled through third party services. For example, if you want to have comments on a static website you'd need to embed Disqus's form and be completely reliant upon their service.

Many web applications simply cannot be built with only a static site generator. However, a static website generator can create part of a site that will be served up by a web server while other pages are handled by the WSGI server. If done right, those web applications have the potential to scale better than if every page is rendered by the WSGI server. The complexity may or may not be worth it for your specific application.

Python implementations

Numerous static website generators exist in many different languages. The ones listed here are primarily coded in Python.

  • Pelican (source code) is a commonly used Python static website generator which is used to create Full Stack Python. The primary templating engine is Jinja and Markdown, reStructuredText and AsciiDoc are supported with the default configuration.

  • MkDocs (source code) uses a YAML configuration file to take Markdown files and an optional theme to output a documentation site. The templating engine is Jinja, but a user doesn't have to create her own templates unless a custom site is desired at which point it might make more sense to use a different static site generator instead.

  • Nikola (source code) takes in reStructuredText, Markdown or Jupyter (IPython) Notebooks and combines the files with Mako or Jinja2 templates to output static sites. It is compatible with both Python 2.7 and 3.3+. Python 2.7 will be dropped in early 2016 while Python 3.3+ will continue to be supported.

  • Acrylamid (source code) uses incremental builds to generate static sites faster than recreating every page after each change is made to the input files.

  • Hyde (source code) started out as a Python rewrite of the popular Ruby-based Jekyll static site generator. Today the project has moved past those "clone Jekyll" origins. Hyde supports Jinja as well as other templating languages and places more emphasis on metadata within the markup files to instruct the generator how to produce the output files. Check out the Hyde-powered websites page to see live examples created with Hyde.

  • Grow SDK (source code) uses projects, known as pods, which contain a specific file and directory structure so the site can be generated. The project remains in the "experimental" phase.

  • Complexity (source code is a site generator for users who like to work in HTML. It uses HTML for templating but has some functionality from Jinja for inheritance. Works with Python 2.6+, 3.3+ and PyPy.

  • Cactus (source code) uses the Django templating engine that was originally built with front-end designers in mind. It works with both Python 2.x and 3.x.

Open source Python static site generator examples

Static site generator resources

Data

Data is an incredibly broad topic but it can be broken down into many subsections, including (in no particular order):

The Python community has built and continues to create open source libraries and tutorials for all of the above topics.

Why is Python a great language choice for data tasks?

Python has a wide array of open source code libraries available and a diverse community of people with different backgrounds who contribute to make those libraries better each day.

In addition, Python data manipulation code can be combined with web frameworks and web APIs to build software that would be difficult to create with a single other language. For example, Ruby is a fantastic language for building web applications but its data analysis and visualization libraries are very limited compared to what is currently available in the Python ecosystem.

How did Python become so widely used for working with data?

Python is a general purpose programming language and can be applied to many problem areas. Over the past couple of decades, Python has become increasingly popular in the scientific and financial communities. Projects such as pandas grew out of a hedge-fund while NumPy and SciPy were created in academic environments then improved by the broader open source community.

The question is: why Python was used to created these projects? The answer is a mix of luck, the growth of the open source community as Python was maturing and wide adoption by people not formally trained as computer scientists. The pragmatic syntax and explicit style helped very intelligent people without programming backgrounds to pick up the language and get their work done with less fuss than other programming languages. Over time the code used in the financial world and scientific community was shared at the same time global open source communities were developing, further spreading their usage among a broader base of software developers.

There's no doubt some of the momentum behind Python's wide adoption for all types of data manipulation was that it happened to be the right language in the right place at the right time. Nevertheless, it was ultimately the hard work of a massive number of engineers and scientists around the world who created the incredible mix of data code libraries available today.

General Python data resources

  • PyData is a community for developer and users of Python data tools. They put on fantastic conferences around the and fund the continued development of open source data-related libraries.

  • Continuum Analytics is one of the leading Python companies that pours a tremendous amount of time and funding into the data community.

  • A crash course in Python for scientists provides an overview of the Python language with iPython Notebook for those in scientific fields.

Databases

A database is an abstraction on top of an operating system's file system to ease creating, reading, updating, and deleting persistent data.

Why are databases necessary?

At a high level web applications store data and present it to users in a useful way. For example, Google stores data about roads and provides directions to get from one location to another by driving through the Maps application. Driving directions are possible because the data is stored in a structured way.

Databases make structured storage reliable and fast. They also give you a mental framework for how the data should be saved and retrieved instead of having to figure out what to do with the data every time you build a new application.

While you're learning about relational databases you should also read up on object-relational mappers (ORMs) and check out the web frameworks page.

Relational databases

The database storage abstraction most commonly used in Python web development is sets of relational tables. Alternative storage abstractions are explained in the NoSQL section of this guide.

Relational databases store all data in a series of tables. Interconnections between the tables are specified as foreign keys.

Databases storage implementations vary in complexity. SQLite, a database included with Python, creates a single file for all data per database. Other databases such as Oracle, PostgreSQL, and MySQL have more complicated persistence schemes while offering additional advanced features that are useful for web application data storage.

PostgreSQL and MySQL are two of the most common open source databases for storing Python web application data.

SQLite is a database that is stored in a single file on disk. SQLite is built into Python but is only built for access by a single connection at a time. Therefore is highly recommended to not run a production web application with SQLite.

PostgreSQL

PostgreSQL is the recommended relational database for working with Python web applications. PostgreSQL's feature set, active development and stability contribute to its usage as the backend for millions of applications live on the Web today.

PostgreSQL resources

MySQL

MySQL is another viable open source database backend option for Python web applications. MySQL has a slightly easier initial learning curve than PostgreSQL. The database is deployed in production at some of the highest trafficked sites such as Twitter, Facebook and many others major organizations. However, since the company focused on MySQL development, MySQL AB, was purchased by Sun Microsystems (which was in turn purchased by Oracle), there have been major defections away from the database by Wikipedia and Google. MySQL remains a viable database option but I always recommend new Python developers learn PostgreSQL if they do not already know MySQL.

MySQL resources

Connecting to a database with Python

To work with a relational database using Python, you need to use a code library. The most common libraries for relational databases are:

SQLite support is built into Python 2.7+ and therefore a separate library is not necessary. Simply "import sqlite3" to begin interfacing with the single file-based database.

Object-relational Mapping

Object-relational mappers (ORMs) allow developers to access data from a backend by writing Python code instead of SQL queries. Each web application framework handles integrating ORMs differently. There's an entire page on object-relational mapping (ORMs) that you should read to get a handle on this subject.

Database third-party services

Numerous companies run scalable database servers as a hosted service. Hosted databases can often provide automated backups and recovery, tightened security configurations and easy vertical scaling, depending on the provider.

  • Amazon Relational Database Service (RDS) provides pre-configured MySQL and PostgreSQL instances. The instances can be scaled to larger or smaller configurations based on storage and performance needs.

  • Google Cloud SQL is a service with managed, backed up, replicated, and auto-patched MySQL instances. Cloud SQL integrates with Google App Engine but can be used independently as well.

  • BitCan provides both MySQL and MongoDB hosted databases with extensive backup services.

General database resources

  • How does a relational database work? is a detailed longform post on the sorting, searching, merging and other operations we often take for granted when using an established relational database such as PostgreSQL.

  • Why I Love Databases is a great read on the CAP Theorem, distributed systems and other topics that are at the core of database theory and implementation. Well worth the time to read.

  • DB-Engines ranks the most popular database management systems.

  • DB Weekly is a weekly roundup of general database articles and resources.

  • A different view provides some perspective on the impedance mismatch between ORMs and traditional SQL queries.

  • Databases integration testing strategies covers a difficult topic that comes up on every real world project.

  • PostgreSQL vs. MS SQL Server is one perspective on the differences between the two database servers from a data analyst.

Databases learning checklist

  1. Install PostgreSQL on your server. Assuming you went with Ubuntu run sudo apt-get install postgresql.

  2. Make sure the psycopg2 library is in your application's dependencies.

  3. Configure your web application to connect to the PostgreSQL instance.

  4. Create models in your ORM, either with Django's built-in ORM or SQLAlchemy with Flask.

  5. Build your database tables or sync the ORM models with the PostgreSQL instance, if you're using an ORM.

  6. Start creating, reading, updating and deleting data in the database from your web application.

NoSQL Data Stores

Relational databases store the vast majority of web application persistent data. However, there are several alternative classifications of storage representations.

  1. Key-value pair
  2. Document-oriented
  3. Column-family table
  4. Graph

These persistent data storage representations are commonly used to augment, rather than completely replace, relational databases. The underlying persistence type used by the NoSQL database often gives it different performance characteristics than a relational database, with better results on some types of read/writes and worse performance on others.

Key-value Pair

Key-value pair data stores are based on hash map data structures.

Key-value pair data stores

  • Redis is an open source in-memory key-value pair data store. Redis is often called "the Swiss Army Knife of web application development." It can be used for caching, queuing, and storing session data for faster access than a traditional relational database, among many other use cases. Redis-py is a solid Python client to use with Redis.

  • Memcached is another widely used in-memory key-value pair storage system.

Key-value pair resources

Redis resources

Document-oriented

A document-oriented database provides a semi-structured representation for nested data.

Document-oriented data stores

  • MongoDB is an open source document-oriented data store with a Binary Object Notation (BSON) storage format that is JSON-style and familiar to web developers. PyMongo is a commonly used client for interfacing with one or more MongoDB instances through Python code. MongoEngine is a Python ORM specifically written for MongoDB that is built on top of PyMongo.

  • Riak is an open source distributed data store focused on availability, fault tolerance and large scale deployments.

  • Apache CouchDB is also an open source project where the focus is on embracing RESTful-style HTTP access for working with stored JSON data.

Document-oriented data store resources

Column-family table

A the column-family table class of NoSQL data stores builds on the key-value pair type. Each key-value pair is considered a row in the store while the column family is similar to a table in the relational database model.

Column-family table data stores

Graph

A graph database represents and stores data in three aspects: nodes, edges and properties.

A node is an entity, such as a person or business.

An edge is the relationship between two entities. For example, an edge could represent that a node for a person entity is an employee of a business entity.

A property represents information about nodes. For example, an entity representing a person could have a property of "female" or "male".

Graph data stores

  • Neo4j is one of the most widely used graph databases and runs on the Java Virtual Machine stack.

  • Cayley is an open source graph data store written by Google primarily written in Go.

  • Titan is a distributed graph database built for multi-node clusters.

Graph data store resources

NoSQL third-party services

  • MongoHQ provides MongoDB as a service. It's easy to set up with either a standard LAMP stack or on Heroku.

NoSQL data store resources

  • NoSQL databases: an overview explains what NoSQL means, how data is stored differently than in relational systems and what the Consistency, Availability and Partition-Tolerance (CAP) Theorem means.

  • CAP Theorem overview presents the basic constraints all databases must trade off in operation.

  • This post on What is a NoSQL database? Learn By Writing One in Python is a detailed article that breaks the mystique behind what some forms of NoSQL databases are doing under the covers.

  • The CAP Theorem series explains concepts related to NoSQL such as what is ACID compared to CAP, CP versus CA and high availability in large scale deployments.

  • NoSQL Weekly is a free curated email newsletter that aggregates articles, tutorials, and videos about non-relational data stores.

  • NoSQL comparison is a large list of popular, BigTable-based, special purpose, and other datastores with attributes and the best use cases for each one.

NoSQL data stores learning checklist

  1. Understand why NoSQL data stores are better for some use cases than relational databases. In general these benefits are only seen at large scale so they may not be applicable to your web application.

  2. Integrate Redis into your project for a speed boost over slower persistent storage. Storing session data in memory is generally much faster than saving that data in a traditional relational database that uses persistent storage. Note that when memory is flushed the data goes away so anything that needs to be persistent must still be backed up to disk on a regular basis.

  3. Evaluate other use cases such as storing transient logs in a document-oriented data store such as MongoDB.

Object-relational mappers (ORMs)

An object-relational mapper (ORM) is a code library that automates the transfer of data stored in relational databases tables into objects that are more commonly used in application code.

Why are ORMs useful?

ORMs provide a high-level abstraction upon a relational database that allows a developer to write Python code instead of SQL to create, read, update and delete data and schemas in their database. Developers can use the programming language they are comfortable with to work with a database instead of writing SQL statements or stored procedures.

For example, without an ORM a developer would write the following SQL statement to retrieve every row in the USERS table where the zip_code column is 94107:

SELECT * FROM USERS WHERE zip_code=94107;

The equivalent Django ORM query would instead look like the following Python code:

# obtain everyone in the 94107 zip code and assign to users variable
users = Users.objects.filter(zip_code=94107)

The ability to write Python code instead of SQL can speed up web application development, especially at the beginning of a project. The potential development speed boost comes from not having to switch from Python code into writing declarative paradigm SQL statements. While some software developers may not mind switching back and forth between languages, it's typically easier to knock out a prototype or start a web application using a single programming language.

ORMs also make it theoretically possible to switch an application between various relational databases. For example, a developer could use SQLite for local development and MySQL in production. A production application could be switched from MySQL to PostgreSQL with minimal code modifications.

In reality however, it's best to use the same database for local development as is used in production. Otherwise unexpected errors could hit in production that were not seen in a local development environment. Also, it's rare that a project would switch from one database in production to another one unless there was a pressing reason.

While you're learning about ORMs you should also read up on deployment and check out the application dependencies page.

Do I have to use an ORM for my web application?

Python ORM libraries are not required for accessing relational databases. In fact, the low-level access is typically provided by another library called a database connector, such as psycopg (for PostgreSQL) or MySQL-python (for MySQL). Take a look at the table below which shows how ORMs can work with different web frameworks and connectors and relational databases.

Examples of how varying Python ORMs can work with different connectors and backends.

The above table shows for example that SQLAlchemy can work with varying web frameworks and database connectors. Developers can also use ORMs without a web framework, such as when creating a data analysis tool or a batch script without a user interface.

What are the downsides of using an ORM?

There are numerous downsides of ORMs, including

  1. Impedance mismatch
  2. Potential for reduced performance
  3. Shifting complexity from the database into the application code

Impedance mismatch

The phrase "impedance mismatch" is commonly used in conjunction with ORMs. Impedance mismatch is a catch-all term for the difficulties that occur when moving data between relational tables and application objects. The gist is that the way a developer uses objects is different from how data is stored and joined in relational tables.

This article on ORM impedance mismatch does a solid job of explaing what the concept is at a high level and provides diagrams to visualize why the problem occurs.

Potential for reduced performance

One of the concerns that's associated with any higher-level abstraction or framework is potential for reduced performance. With ORMs, the performance hit comes from the translation of application code into a corresponding SQL statement which may not be tuned properly.

ORMs are also often easy to try but difficult to master. For example, a beginner using Django might not know about the select_related() function and how it can improve some queries' foreign key relationship performance. There are dozens of performance tips and tricks for every ORM. It's possible that investing time in learning those quirks may be better spent just learning SQL and how to write stored procedures.

There's a lot of hand-waving "may or may not" and "potential for" in this section. In large projects ORMs are good enough for roughly 80-90% of use cases but in 10-20% of a project's database interactions there can be major performance improvements by having a knowledgeable database administrator write tuned SQL statements to replace the ORM's generated SQL code.

Shifting complexity from the database into the app code

The code for working with an application's data has to live somewhere. Before ORMs were common, database stored procedures were used to encapsulate the database logic. With an ORM, the data manipulation code instead lives within the application's Python codebase. The addition of data handling logic in the codebase generally isn't an issue with a sound application design, but it does increase the total amount of Python code instead of splitting code between the application and the database stored procedures.

Python ORM Implementations

There are numerous ORM implementations written in Python, including

  1. The Django ORM
  2. SQLAlchemy
  3. Peewee
  4. PonyORM
  5. SQLObject

There are other ORMs, such as Canonical's Storm, but most of them do not appear to currently be under active development. Learn more about the major active ORMs below.

Django's ORM

The Django web framework comes with its own built-in object-relational mapping module, generally referred to as "the Django ORM" or "Django's ORM".

Django's ORM works well for simple and medium-complexity database operations. However, there are often complaints that the ORM makes complex queries much more complicated than writing straight SQL or using SQLAlchemy.

It's technically possible to drop down to SQL but it ties the queries to a specific database implementation. The ORM is coupled closely with Django so replacing the default ORM with SQLAlchemy is currently a hack workaround. Note though that some of the Django core committers believe it is only a matter of time before the default ORM is replaced with SQLAlchemy. It will be a large effort to get that working though so it's likely to come in Django 1.9 or later.

Since the majority of Django projects are tied to the default ORM, it's best to read up on advanced use cases and tools for doing your best work within the existing framework.

SQLAlchemy

SQLAlchemy is a well-regarded Python ORM because it gets the abstraction level "just right" and seems to make complex database queries easier to write than the Django ORM in most cases. SQLAlchemy is typically used with Flask as the database ORM via the Flask-SQLAlchemy extension.

Peewee

Peewee is a Python ORM written to be "simpler, smaller and more hackable" than SQLAlchemy. The analogy used by the core Peewee author is that Peewee is to SQLAlchemy as SQLite is to PostgreSQL. An ORM does not have to work for every exhaustive use case in order to be useful.

Pony

Pony ORM is another Python ORM with a slight twist in its licensing model. The project is multi-licensed. Pony is free for use on open source projects but has a commercial license that is required for commercial projects. The license is a one-time payment and does not necessitate a recurring fee.

SQLObject

SQLObject is an ORM that has been under active open source development since before 2003.

Schema migrations

Schema migrations, for example when you need to add a new column to an existing table in your database, are not technically part of ORMs. However, since ORMs typically lead to a hands-off approach to the database (at the developers peril in many cases), libraries to perform schema migrations often go hand-in-hand with Python ORM usage on web application projects.

Database schema migrations are a complex topic and deserve their own page. For now, we'll lump schema migration resources under ORM links below.

General ORM resources

  • There's also a detailed overview of what ORMs are on another page of the website.

  • This example GitHub project implements the same Flask application with several different ORMs: SQLAlchemy, Peewee, MongoEngine, stdnet and PonyORM.

  • Martin Fowler addresses the ORM hate in an essay about how ORMs are often misused but that they do provide benefits to developers.

  • If you're confused about the difference between a connector, such as MySQL-python and an ORM like SQLAlchemy, read this StackOverflow answer on the topic.

Django ORM resources

SQLAlchemy resources

Peewee resources

Pony ORM resources

SQLObject resources

Application Programming Interfaces

Application programming interfaces (APIs) provide machine-readable data transfer and signaling between applications.

Why are APIs important?

HTML, CSS and JavaScript create human-readable webpages. However, those webpages are not easily consumable by other machines.

Numerous scraping programs and libraries exist to rip data out of HTML but it's simpler to consume data through APIs. For example, if you want the content of a news article it's easier to get the content through an API than to scrap the text out of the HTML.

Key API concepts

There are several key concepts that get thrown around in the APIs world. It's best to understand these ideas first before diving into the API literature.

  • Representation State Transfer (REST)

  • Webhooks

  • JavaScript Object Notation (JSON) and Extensible Markup Language (XML)

  • Endpoints

Webhooks

A webhook is a user-defined HTTP callback to a URL that executes when a system condition is met. The call alerts the second system via a POST or GET request and often passes data as well.

Webhooks are important because they enable two-way communication initiation for APIs. Webhook flexibility comes in from their definition by the API user instead of the API itself.

For example, in the Twilio API when a text message is sent to a Twilio phone number Twilio sends an HTTP POST request webhook to the URL specified by the user. The URL is defined in a text box on the number's page on Twilio as shown below.

Webhook definition in the Twilio API.

API open source projects

  • Swagger is an open source project written in Scala that defines a standard interface for RESTful APIs.

API resources

APIs learning checklist

  1. Learn the API concepts of machine-to-machine communication with JSON and XML, endpoints and webhooks.

  2. Integrate an API such as Twilio or Stripe into your web application. Read the API integration section for more information.

  3. Use a framework to create an API for your own application. Learn about web API frameworks on the API creation page.

  4. Expose your web application's API so other applications can consume data you want to share.

API Integration

The majority of production Python web applications rely on several externally hosted application programming interfaces (APIs). APIs are also commonly referred to as third party services or external platforms. Examples include Twilio for messaging and voice services, Stripe for payment processing and Disqus for embedded webpage comments.

There are many articles about proper API design but best practices for integrating APIs is less commonly written about. However, this subject continuously grows in importance because APIs provide critical functionality across many implementation areas.

Hosted API testing services

  • Runscope is a service specifically designed for APIs that assists developers with automated testing and traffic inspection.

  • Apiary provides a blueprint for creating APIs so they are easier to test and generate clean documentation.

API Integration Resources

API integration learning checklist

  1. Pick an API known for top notch documentation. Here's a list of ten APIs that are a good starting point for beginners.

  2. Read the API documentation for your chosen API. Figure out a simple use case for how your application could be improved by using that API.

  3. Before you start writing any code, play around with the API through the commandline with cURL or in the browser with Postman. This exercise will help you get a better understanding of API authentication and the data required for requests and responses.

  4. Evaluate whether to use a helper library or work with Requests. Helper libraries are usually easier to get started with while Requests gives you more control over the HTTP calls.

  5. Move your API calls into a task queue so they do not block the HTTP request-response cycle for your web application.

API Creation

Creating and exposing APIs allows your web application to interact with other applications through machine-to-machine communication.

API creation frameworks

  • Django REST framework and Tastypie are the two most widely used API frameworks to use with Django. The edge currently goes to Django REST framework based on rough community sentiment. Django REST framework continues to knock out great releases after the 3.0 release mark when Tom Christie ran a successful Kickstarter campaign.

  • Flask-RESTful is widely used for creating web APIs with Flask. It was originally open sourced and explained in a blog post by Twilio then moved into its own GitHub organization so engineers from outside the company could be core contributors.

  • Flask API is another common library for exposing APIs from Flask web applications.

  • Sandman is a widely used tool to automatically generate a RESTful API service from a legacy database without writing a line of code (though it's easily extensible through code).

  • Cornice is a REST framework for Pyramid.

  • Restless is a lightweight API framework that aims to be framework agnostic. The general concept is that you can use the same API code for Django, Flask, Bottle, Pyramid or any other WSGI framework with minimal porting effort.

  • Eve is a Python REST framework built with Flask, MongoDB and Redis. The framework's primary author Nicola Iarocci gave a great talk at EuroPython 2014 that introduced the main features of the framework.

  • Falcon is a fast and lightweight framework well suited to create RESTful APIs.

  • Hug built on-top of Falcon and Python3 with an aim to make developing Python driven APIs as simple as possible, but no simpler. Hug leverages Python3 annotations to automatically validate and convert incoming and outgoing API parameters.

  • Pycnic is a new JSON-API-only framework designed with REST in mind.

API testing projects

Building, running and maintaining APIs requires as much effort as building, running and maintaining a web application. API testing frameworks are the equivalent of browser testing in the web application world.

  • zato-apitest invokes HTTP APIs and provides hooks for running through other testing frameworks.

Hosted API testing services

  • Runscope is an API testing SaaS application that can test both your own APIs and external APIs that your application relies upon.

  • API Science is focused on deep API testing, including multi-step API calls and monitoring of external APIs.

  • SmartBear has several API monitoring and testing tools for APIs.

API creation resources

Python-specific API creation resources

API creation learning checklist

  1. Pick an API framework appropriate for your web framework. For Django I recommend Django REST framework and for Flask I recommend Flask-RESTful.

  2. Begin by building out a simple use case for the API. Generally the use case will either involve data that users want in a machine-readable format or a backend for alternative clients such as an iOS or Android mobile app.

  3. Add an authentication mechanism through OAuth or a token scheme.

  4. Add rate limiting to the API if data usage volume could be a performance issue. Also add basic metrics so you can determine how often the API is being accessed and whether it is performing properly.

  5. Provide ample documentation and a walkthrough for how the API can be accessed and used.

  6. Figure out other use cases and expand based on what you learned with the initial API use case.

Deployment

Deployment involves packaging up your web application and putting it in a production environment that can run the app.

Why is deployment necessary?

Your web application must live somewhere other than your own desktop or laptop. A production environment is the canonical version of your current application and its associated data.

Deployment topics map

Python web application deployments are comprised of many pieces that need to be individually configured. Here is a map that visually depicts how each deployment topic relates to each other. Click the image to pull up a PDF version.

Full Stack Python site map.

Deployment hosting options

There are four options for deploying and hosting a web application:

  1. "Bare metal" servers

  2. Virtualized servers

  3. Infrastructure-as-a-service

  4. Platform-as-a-service

The first three options are similar. The deployer needs to provision one or more servers with a Linux distribution. System packages, a web server, WSGI server, database and the Python environment are then installed. Finally the application can be pulled from source and installed in the environment.

Note that there are other ways of installing a Python web application through system-specific package management systems. We won't cover those in this guide as they are considered advanced deployment techniques.

Deployment resources

Deployment learning checklist

  1. If you're tight on time look at the platform-as-a-service (PaaS) options. You can deploy a low traffic project web app for free or low cost. You won't have to worry about setting up the operating system and web server compared to going the traditional server route. In theory you should be able to get your application live on the web sooner with PaaS hosting.

  2. Traditional server options are your best bet for learning how the entire Python web stack works. You'll often save money with a virtual private server instead of a platform-as-a-service as you scale up.

  3. Read about servers, operating systems, web servers and WSGI servers to get a broad picture of what components need to be set up to run a Python web application.

Servers

Servers are the physical infrastructure to run all the layers of software so your web application can respond to requests from clients such as web browsers.

Why are servers necessary?

Your web application must live somewhere other than your own desktop or laptop. Servers should ideally be accessible 24 hours a day, 7 days a week, with no unplanned downtime. The servers that host your web application for actual users (as opposed to test users) are known as production servers. Production servers hold real data (again as opposed to test data) and must be secure against unauthorized access.

"Bare metal" servers

The term bare metal refers to purchasing the actual hardware and hooking it up to the Internet either through a business-class internet service provider (ISP) or co-locating the server with other servers. A "business-class" ISP is necessary because most residential Internet service agreements explicitly prohibit running web servers on their networks. You may be able to get away with low traffic volume but if your site serves a lot of traffic it will alert an ISP's filters.

The bare metal option offers the most control over the server configuration, usually has the highest performance for the price, but also is the most expensive upfront option and the highest ongoing maintenance. With bare metal servers the ongoing operating cost is the electricity the server(s) use as well as handling repairs when server components malfunction. You're taking on manual labor working with hardware as well as the rest of the software stack.

Buy actual hardware from a vendor either pre-built or as a collection of components that you assemble yourself. You can also buy pre-configured servers from Dell or HP. Those servers tend to be in smaller case form factors (called "blades") but are correspondingly more expensive than putting off-the-shelf components together yourself in a standard computer case.

Virtualized servers

Virtual private servers (VPSs) are slices of hardware on top of a larger bare metal server. Virtualization software such as Xen and VMWare allow providers such as Linode and prgmr (as well as a many others) to provide fractions of a full server that appear as their own instances. For example, a server with an 8-core Xeon processor and 16 gigabytes of memory can be sliced into 8 pieces with the equivalent of 1-core and 2 gigabytes of memory.

The primary disadvantage of virtualized servers is that there is resource overhead in the virtualization process. In addition, physical constraints such as heavy I/O operations by a single virtualized instance on persistent storage can cause performance bottlenecks for other virtualized instances on the shared server. Choosing virtualized server hosting should be based on your needs for urgency of service ticket requests and the frequency you require for ongoing maintenance such as persistent storage backups.

Virtualized servers resources

Infrastructure-as-a-service

Infrastructure-as-a-service (IaaS) overlaps with virtualized servers because the resources are often presented in the same way. The difference between virtualized servers and IaaS is the granularity of the billing cycle. IaaS generally encourages a finer granularity based on minutes or hours of server usage instead of on monthly billing cycles.

IaaS can be used in combination with virtualized servers to provide dynamic upscaling for heavy traffic. When traffic is low then virtualized servers can solely be used. This combination of resources reduces cost at the expense of greater complexity in the dynamically scaled infrastructure.

The most common IaaS platforms are Amazon Web Services and Rackspace Cloud.

The disadvantage to IaaS platforms is the lock-in if you have to write custom code to deploy, dynamically scale, and generally understand your infrastructure. Every platform has its quirks. For example, Amazon's standard Elastic Block Store storage infrastructure has at least an order of magnitude worse I/O throughput than working with your local disk. Your application's database queries may work great locally but then when you deploy the performance is inadequate. Amazon has higher throughput EBS instances but you will pay correspondingly more for them. EBS throughput is just one of many quirks you need to understand before committing to an IaaS platform.

Infrastructure-as-a-service resources

Servers learning checklist

  1. Sign up for a hosting provider. I recommend getting a Linode VPS to set up your initial infrastructure and deploy your web application there. Digital Ocean and prgrmr are other VPS options. You can change hosting providers later after the deployment process is automated.

  2. Provision your first server. It will be ready but in a shutdown state while awaiting your instructions.

  3. Move to the operating systems section to learn how to load Ubuntu 14.04 LTS as a base OS for Python web applications.

Platform-as-a-service

A platform-as-a-service (PaaS) provides infrastructure and a software layer on which a web application is deployed. Running your web application from a PaaS removes the need to know as much about the underlying servers, operating system, web server, and often the WSGI server.

Note: If you are not interested in deploying to a PaaS you can move ahead to the WSGI servers section.

The PaaS layer defines how the application accesses resources such as computing time, files, and external services. The PaaS provides a higher-level abstraction for working with computing resources than deploying an application to a server or IaaS.

A PaaS makes deployment and operations easier because it forces the developer to conform applications to the PaaS architecture. For example, Heroku looks for Python's requirements.txt file in the base directory of the repository during deployment because that is the file's de facto community standard location.

Traditional LAMP server stack versus a Platform-as-a-Service stack

If you go the PaaS route, you can skip configuring an operating system and web server prebaked into PaaS offerings. PaaS offerings generally start at the WSGI server layer.

Platform-as-a-service responsibilities

Although PaaS offerings simplify setting up and maintaining the servers, operating system, and web server, developers still have responsibilities for other layers of their web stack.

While it's useful to know the operating system that underpins your PaaS, for example Heroku uses Ubuntu 10.04, you will not have to know as much about securing the operating system and server level. However, web applications deployed to a PaaS are just as vulnerable to security breaches at the application level as a standard LAMP stack. It's still your responsibility to ensure the web application framework and your app itself is up to date and secured. See the security section for further information.

Platforms-as-a-service that support Python

Platform-as-a-service resources

Platform-as-a-service learning checklist

  1. Review the potential Python platform-as-a-service options listed above.

  2. Sign up for a PaaS account at the provider that appears to best fit your application needs. Heroku is the PaaS option recommended for starters due to their detailed documentation and walkthroughs available on the web. However, the other options are also viable since their purpose is to make deploying applications as easy as possible.

  3. Check if there are any PaaS-specific configuration files needed for your app to run properly on the PaaS after it is deployed.

  4. Deploy your app to the PaaS.

  5. Sync your application's configuration with the database.

  6. Set up a content delivery network for your application's static content unless your PaaS provider already handles this deployment step for you.

  7. Check if the application's functionality is working and tweak as necessary.

Operating Systems

An operating system runs on the server or virtual server and controls access to computing resources. The operating system also includes a way to install programs necessary for running your Python web application.

Why are operating systems necessary?

An operating system makes many of the computing tasks we take for granted easy. For example, the operating system enables writing to files, communicating over a network and running multiple programs at once. Otherwise you'd need to control the CPU, memory, network, graphics card, and many other components with your own low-level implementation.

Without using an existing operating system like Linux, Mac OS X or Windows, you'd be forced to write a new operating system as part of your web application. It would be impossible to write features for your Python web application because you'd be too busy hunting down a memory leak in your assembly code, if you even were able to get that far.

Fortunately, the open source community provides Linux to the Python world as a rock solid free operating system for running our applications.

Recommended operating systems

The only recommended operating system for production Python web stack deployments is Linux. There are several Linux distributions commonly used for running production servers. Ubuntu Long Term Support (LTS) releases, Red Hat Enterprise Linux, and CentOS are all viable options.

Mac OS X is fine for development activities. Windows and Mac OS X are not appropriate for production deployments unless there is a major reason why you must use them in lieu of Linux.

Ubuntu

Ubuntu is a Linux distribution packaged by the Canonical Ltd company. Ubuntu uses the Debian distribution as a base for packages, including the aptitude package manager. For desktop versions of Ubuntu, GNOME (until the 11.04 release) or Unity (11.10 through current) is bundled with the distribution to provide a user interface.

Ubuntu Long Term Support (LTS) releases are the recommended versions to use for deployments. LTS versions receive five years of post-release updates from Canonical. Every two years, Canonical creates a new LTS release, which allows for an easy upgrade path as well as flexibility in skipping every other LTS release if necessary. As of November 2014, 14.04 Trusty Tahr is the latest Ubuntu LTS release.

Ubuntu Python Packages

There are several Aptitude packages found on Linux servers running a Python stack. These packages are:

Red Hat and CentOS

Red Hat Enterprise Linux (RHEL) and Community ENTerprise Operating System (CentOS) are the same distribution. The primary difference between the two is that CentOS is an open source, liberally licensed free derivative of RHEL.

RHEL and CentOS use a different package manager and command-line interface from Debian-based Linux distributions: RPM Package Manager (RPM) and the Yellowdog Updater, Modified (YUM). RPM has a specific .rpm file format to handle the packaging and installation of libraries and applications. YUM provides a command-line interface for interacting with the RPM system.

Operating system resources

Operating system learning checklist

  1. Choose either a Debian-based Linux distribution such as Ubuntu or a Fedora-based distribution like CentOS.

  2. Harden the security through a few basic steps. Install basic security packages such as fail2ban and unattended-upgrades. Create a new user account with sudo privileges and disable root logins. Disable password-only logins and use a public-private keypair instead. Read more about hardening systems in the resources listed below.

  3. Install Python-specific packages to prepare the environment for running a Python application. Which packages you'll need to install depends on the distribution you've selected.

  4. Read up on web servers as installing one will be the next step in the deployment process.

Web servers

Web servers respond to Hypertext Transfer Protocol (HTTP) requests from clients and send back a response containing a status code and often content such as HTML, XML or JSON as well.

Why are web servers necessary?

Web servers are the ying to the web client's yang. The server and client speak the standardized language of the World Wide Web. This standard language is why an old Mozilla Netscape browser can still talk to a modern Apache or Nginx web server, even if it cannot properly render the page design like a modern web browser can.

The basic language of the Web with the request and response cycle from client to server then server back to client remains the same as it was when the Web was invented by Tim Berners-Lee at CERN in 1989. Modern browsers and web servers have simply extended the language of the Web to incorporate new standards.

Client requests

A client that sends a request to a web server is usually a browser such as Internet Explorer, Firefox, or Chrome, but it can also be a

  • headless browser, commonly use for testing, such as phantomjs
  • commandline utility, for example wget and cURL
  • text-based web browser such as Lynx
  • web crawler.

Web servers process requests from the above clients. The result of the web server's processing is a response code and commonly a content response. Some status codes, such as 204 (No content) and 403 (Forbidden), do not have content responses.

In a simple case, the client will request a static asset such as a picture or JavaScript file. The file sits on the file system in a location the web server is authorized to access and the web server sends the file to the client with a 200 status code. If the client already requested the file and the file has not changed, the web server will pass back a 304 "Not modified" response indicating the client already has the latest version of that file.

Web server and web browser request-response cycle

A web server sends files to a web browser based on the web browser's request. In the first request, the browser accessed the "www.fullstackpython.com" address and the server responded with the index.html HTML-formatted file. That HTML file contained references to other files, such as style.css and script.js that the browser then requested from the server.

Sending static assets (such as CSS and JavaScript files) can eat up a large amount of bandwidth which is why using a Content Delivery Network (CDN) is important when possible (see the content delivery network section for a more detailed explanation).

Web server resources

Nginx resources

Web servers learning checklist

  1. Choose a web server. Nginx is often recommended although Apache is also a great choice.

  2. Create an SSL certificate. For testing use a self-signed certificate and for a production app buy one from Digicert. Configure the web server to serve traffic over SSL. You'll need SSL for serving only HTTPS traffic and preventing security issues that occur with unencrypted user input.

  3. Configure the web server to serve up static files such as CSS, JavaScript and images.

  4. Once you set up the WSGI server you'll need to configure the web server as a pass through for dynamic content.

WSGI Servers

A Web Server Gateway Interface (WSGI) server implements the web server side of the WSGI interface for running Python web applications.

Why is WSGI necessary?

A traditional web server does not understand or have any way to run Python applications. In the late 1990s, a developer named Grisha Trubetskoy came up with an Apache module called mod_python to execute arbitrary Python code. For several years in the late 1990s and early 2000s, Apache configured with mod_python ran most Python web applications.

However, mod_python wasn't a standard specification. It was just an implementation that allowed Python code to run on a server. As mod_python's development stalled and security vulnerabilities were discovered there was recognition by the community that a consistent way to execute Python code for web applications was needed.

Therefore the Python community came up with WSGI as a standard interface that modules and containers could implement. WSGI is now the accepted approach for running Python web applications.

WSGI server invoking a WSGI application.

As shown in the above diagram, a WSGI server simply invokes a callable object on the WSGI application as defined by the PEP 3333 standard.

WSGI's Purpose

Why use WSGI and not just point a web server directly at an application?

  • WSGI gives you flexibility. Application developers can swap out web stack components for others. For example, a developer can switch from Green Unicorn to uWSGI without modifying the application or framework that implements WSGI. From PEP 3333:

    The availability and widespread use of such an API in web servers for Python [...] would separate choice of framework from choice of web server, freeing users to choose a pairing that suits them, while freeing framework and server developers to focus on their preferred area of specialization.

  • WSGI servers promote scaling. Serving thousands of requests for dynamic content at once is the domain of WSGI servers, not frameworks. WSGI servers handle processing requests from the web server and deciding how to communicate those requests to an application framework's process. The segregation of responsibilities is important for efficiently scaling web traffic.

WSGI Server - Web server - Browser

WSGI is by design a simple standard interface for running Python code. As a web developer you won't need to know much more than

  • what WSGI stands for (Web Server Gateway Inteface)

  • that a WSGI container is a separate running process that runs on a different port than your web server

  • your web server is configured to pass requests to the WSGI container which runs your web application, then pass the response (in the form of HTML) back to the requester

If you're using a standard web framework such as Django, Flask, or Bottle, or almost any other current Python framework, you don't need to worry about how frameworks implement the application side of the WSGI standard. Likewise, if you're using a standard WSGI container such as Green Unicorn, uWSGI, mod_wsgi, or gevent, you can get them running without worrying about how they implement the WSGI standard.

However, knowing the WSGI standard and how these frameworks and containers implement WSGI should be on your learning checklist though as you become a more experienced Python web developer.

Official WSGI specifications

The WSGI standard v1.0 is specified in PEP 0333. As of September 2010, WSGI v1.0 is superseded by PEP 3333, which defines the v1.0.1 WSGI standard. If you're working with Python 2.x and you're compliant with PEP 0333, then you're also compliant with 3333. The newer version is simply an update for Python 3 and has instructions for how unicode should be handled.

wsgiref in Python 2.x and wsgiref in Python 3.x are the reference implementations of the WSGI specification built into Python's standard library so it can be used to build WSGI servers and applications.

Example web server configuration

A web server's configuration specifies what requests should be passed to the WSGI server to process. Once a request is processed and generated by the WSGI server, the response is passed back through the web server and onto the browser.

For example, this Nginx web server's configuration specifics Nginx should handle static assets (such as images, JavaScript, and CSS files) under the /static directory and pass all other requests to the WSGI server running on port 8000:

# this specifies that there is a WSGI server running on port 8000
upstream app_server_djangoapp {
    server localhost:8000 fail_timeout=0;
}

# Nginx is set up to run on the standard HTTP port and listen for requests
server {
  listen 80;

  # nginx should serve up static files and never send to the WSGI server
  location /static {
    autoindex on;
    alias /srv/www/assets;
  }

  # requests that do not fall under /static are passed on to the WSGI
  # server that was specified above running on port 8000
  location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    if (!-f $request_filename) {
      proxy_pass http://app_server_djangoapp;
      break;
    }
  }
}

Note that the above code is a simplified version of a production-ready Nginx configuration. For real SSL and non-SSL templates, take a look at the Underwear web server templates on GitHub.

WSGI servers

There is a comprehensive list of WSGI servers on the WSGI Read the Docs page. The following are WSGI servers based on community recommendations.

  • Green Unicorn is a pre-fork worker model based server ported from the Ruby Unicorn project.

  • uWSGI is gaining steam as a highly-performant WSGI server implementation.

  • mod_wsgi is an Apache module implementing the WSGI specification.

  • CherryPy is a pure Python web server that also functions as a WSGI server.

WSGI resources

WSGI servers learning checklist

  1. Understand that WSGI is a standard Python specification for applications and servers to implement.

  2. Pick a WSGI server based on available documentation and tutorials. Green Unicorn is a good one to start with since it's been around for awhile.

  3. Add the WSGI server to your server deployment.

  4. Configure the web server to pass requests to the WSGI server for appropriate URL patterns.

  5. Test that the WSGI server responds to local requests but not direct requests outside your infrastructure. The web server should be the pass through for requests to and responses from the WSGI server.

Source control

Source control, also known as version control, stores software code files with a detailed history of every modification made to those files.

Why is source control necessary?

Version control systems allow developers to modify code without worrying about permanently screwing something up. Unwanted changes can be easily rolled back to previous working versions of the code.

Source control also makes team software development easier. One developer can combine her code modifications with other developers' code through diff views that show line-by-line changes then merge the appropriate code into the main code branch.

Version control is a necessity on all software projects regardless of development time, codebase size or the programming language used. Every project should immediately begin by using a version control system such as Git or Mercurial.

Source control during deployment

Pulling code during a deployment is a potential way source control systems fit into the deployment process.

App deployment uses a server to pull from the source control system.

Note that some developers recommend deployment pipelines package the source code to deploy it and never have a production environment touch a source control system directly. However, for small scale deployments it's often easiest to pull from source code when you're getting started instead of figuring out how to wrap the Python code in a system installation package.

Source control projects

Numerous source control systems have been created over the past several decades. In the past, proprietary source control software offered features tailored to large development teams and specific project workflows. However, open source systems are now used for version control on the largest and most complicated software projects in existence. There's no reason why your project should use anything other than an open source version control system in today's Python development world. The two primary choices are:

  • Git is a free and open source distributed version control system.

  • Mercurial is similar to Git, also a free and open source distributed version control system.

Hosted source control services

Git and Mercurial can be downloaded and run on your own server. However, it's easy and cheap to get started with a hosted version control service. You can transition away from the service at a later time by moving your repositories if your needs change. A couple of recommended hosted version control services are:

  • GitHub is currently the most commonly used source control platform for using Git.

  • BitBucket provides free Git and Mercurial repositories for open projects and private repositories for up to five users. Users pay for hosting private repositories with more than five users.

General source control resources

Git resources

  • Pro Git is a free open source book that walks through all aspects of using the version control system.

  • Git in Six Hundred Words is a clear and concise essay explaining the fundamental concepts of Git.

  • A Hacker's Guide to Git covers the basics as well as more advanced Git commands while explaining each step along the way.

  • A practical git introduction is exactly what the title says it is. This is a well written guide with plenty of code snippets to get you up to speed with Git.

  • Git from the inside out demonstrates how Git's graph-based data structure produces certain behavior through example Git commands. This is a highly recommended read after you've grasped the basics and are looking to go deeper with Git.

  • git ready has a nice collection of blog posts based on beginner, intermediate and advanced Git use cases.

  • git-flow details a Git branching model for small teams.

  • GitHub Flow builds on git-flow, goes over some of the issues that arise with it and presents a few solutions to those problems.

  • Git Workflows That Work is a helpful post with diagrams to show how teams can create a Git workflow that will help their development process.

  • "Our Git Workflow" by Braintree goes over how this payments company uses Git for development and merging source code.

  • Code Sleuthing with Git shows how to review past changes when a deployment goes wrong to figure out what the heck happened.

Source control learning checklist

  1. Pick a version control system. Git is recommended because on the web there are a significant number of tutorials to help both new and advanced users.

  2. Learn basic use cases for version control such as committing changes, rolling back to earlier file versions and searching for when lines of code were modified during development history.

  3. Ensure your source code is backed up in a central repository. A central repository is critical not only if your local development version is corrupted but also for the deployment process.

  4. Integrate source control into your deployment process in three ways. First, pull the project source code from version control during deployments. Second, kick off deployments when code is modified by using webhooks or polling on the repository. Third, ensure you can roll back to a previous version if a code deployment goes wrong.

Application Dependencies

Application dependencies are the libraries other than your project code that are required to create and run your application.

Why are application dependencies important?

Python web applications are built upon the work done by thousands of open source programmers. Application dependencies include not only web frameworks but also libraries for scraping, parsing, processing, analyzing, visualizing, and many other tasks. Python's ecosystem facilitates discovery, retrieval and installation so applications are easier for developers to create.

Finding libraries

Python libraries are stored in a central location known as the Python Package Index (PyPi). PyPi contains search functionality with results weighted by usage and relevance based on keyword terms.

Besides PyPi there are numerous resources that list common or "must-have" libraries. Ultimately the decision for which application dependencies are necessary for your project is up to you and the functionality you're looking to build. However, it's useful to browse through these lists in case you come across a library to solve a problem by reusing the code instead of writing it all yourself. A few of the best collections of Python libraries are

Isolating dependencies

Dependencies are installed separately from system-level packages to prevent library version conflicts. The most common isolation method is virtualenv. Each virtualenv is its own copy of the Python interpreter and dependencies in the site-packages directory. To use a virtualenv it must first be created with the virtualenv command and then activated.

The virtualenv stores dependencies in an isolated environment. The web application then relies only on that virtualenv instance which has a separate copy of the Python interpreter and site-packages directory. A high level of how a server configured with virtualenv can look is shown in the picture below.

How the virtualenv separates dependencies on the server.

Installing Python dependencies

The recommended way to install Python library dependencies is with the pip command when a virtualenv is activated.

Pip and virtualenv work together and have complementary responsibilities. Pip downloads and installs application dependencies from the central PyPi repository.

requirements.txt

The pip convention for specifying application dependencies is with a requirements.txt file. When you build a Python web application you should include requirements.txt in the base directory of your project.

Python projects' dependencies for a web application should be specified with pegged dependencies like the following:

django==1.6
bpython==0.12
django-braces==0.2.1
django-model-utils==1.1.0
logutils==0.3.3
South==0.7.6
requests==1.2.0
stripe==1.9.1
dj-database-url==0.2.1
django-oauth2-provider==0.2.4
djangorestframework==2.3.1

Pegged dependencies with precise version numbers or Git tags are important because otherwise the latest version of a dependency will be used. While it may sound good to stay up to date, there's no telling if your application actually works with the latest versions of all dependencies. Developers should deliberately upgrade and test to make sure there were no backwards-incompatible modifications in newer dependency library versions.

setup.py

There is another type of dependency specification for Python libraries known as setup.py. Setup.py is a standard for distributing and installing Python libraries. If you're building a Python library, such as requests or underwear you must include setup.py so a dependency manager can correctly install both the library as well as additional dependencies for the library. There's still quite a bit of confusion in the Python community over the difference between requirements.txt and setup.py, so read this well written post for further clarification.

Application dependency resources

Open source app dependency projects

  • Autoenv is a tool for activating environment variables stored in a .env file in your projects' home directories. Environment variables aren't managed by virtualenv and although virtualenvwrapper has some hooks for handling them, it's often easiest to use a shell script or .env file to set them in a development environment.

  • Pipreqs searches through a project for dependencies based on imports. It then generates a requirements.txt file based on the libraries necessary to run those dependencies. Note though that while this could come in handy with a legacy project, the version numbers for those libraries will not be generated with the output.

Application dependencies learning checklist

  1. Ensure the libraries your web application depends on are all captured in a requirement.txt file with pegged versions.

  2. An easy way to capture currently installed dependencies is with the pip freeze command.

  3. Create a fresh virtualenv and install the dependencies from your requirements.txt file by using the pip install -r requirements.txt command.

  4. Check that your application runs properly with the fresh virtualenv and only the installed dependencies from the requirements.txt file.

Static content

Some content on a website does not change and therefore should be served up either directly through the web server or a content delivery network (CDN). Examples include JavaScript, image, and CSS files.

Types of static content

Static content can be either assets created as part of your development process such as images on your landing page or user-generated content. The Django framework calls these two categories assets and media.

Content delivery networks

A content delivery network (CDN) is a third party that stores and serves static files. Amazon CloudFront, Akamai, and Rackspace Cloud Files are examples of CDNs. The purpose of a CDN is to remove the load of static file requests from web servers that are handling dynamic web content. For example, if you have an nginx server that handles both static files and acts as a front for a Green Unicorn WSGI server on a 512 megabyte virtual private server, the nginx server will run into resource constraints under heavy traffic. A CDN can remove the need to serve static assets from that nginx server so it can purely act as a pass through for requests to the Green Unicorn WSGI server.

CDNs send content responses from data centers with the closest proximity to the requester.

Static Content Resources

Static content learning checklist

  1. Identify a content delivery network to offload serving static content files from your local web server. I recommend using Amazon S3 with CloudFront as it's easy to set up and will scale to high bandwidth demands.

  2. Update your web application deployment process so updated static files are uploaded to the CDN.

  3. Move static content serving from the www subdomain to a static (or similarly named) subdomain so browsers will load static content in parallel to www HTTP requests.

Task queues

Task queues manage background work that must be executed outside the usual HTTP request-response cycle.

Why are task queues necessary?

Tasks are handled asynchronously either because they are not initiated by an HTTP request or because they are long-running jobs that would dramatically reduce the performance of an HTTP response.

For example, a web application could poll the GitHub API every 10 minutes to collect the names of the top 100 starred repositories. A task queue would handle invoking code to call the GitHub API, process the results and store them in a persistent database for later use.

Another example is when a database query would take too long during the HTTP request-response cycle. The query could be performed in the background on a fixed interval with the results stored in the database. When an HTTP request comes in that needs those results a query would simply fetch the precalculated result instead of re-executing the longer query. This precalculation scenario is a form of caching enabled by task queues.

Other types of jobs for task queues include

  • spreading out large numbers of independent database inserts over time instead of inserting everything at once

  • aggregating collected data values on a fixed interval, such as every 15 minutes

  • scheduling periodic jobs such as batch processes

Task queue projects

The defacto standard Python task queue is Celery. The other task queue projects that arise tend to come from the perspective that Celery is overly complicated for simple use cases. My recommendation is to put the effort into Celery's reasonable learning curve as it is worth the time it takes to understand how to use the project.

  • The Celery distributed task queue is the most commonly used Python library for handling asynchronous tasks and scheduling.

  • The RQ (Redis Queue) is a simple Python library for queueing jobs and processing them in the background with workers. RQ is backed by Redis and is designed to have a low barrier to entry. The intro post contains information on design decisions and how to use RQ.

  • Taskmaster is a lightweight simple distributed queue for handling large volumes of one-off tasks.

  • Huey is a simple task queue that uses Redis on the backend but otherwise does not depend on other libraries. The project was previously known as Invoker and the author changed the name.

Hosted message and task queue services

Task queue third party services aim to solve the complexity issues that arise when scaling out a large deployment of distributed task queues.

  • Iron.io is a distributed messaging service platform that works with many types of task queues such as Celery. It also is built to work with other IaaS and PaaS environments such as Amazon Web Services and Heroku.

  • Amazon Simple Queue Service (SQS) is a set of five APIs for creating, sending, receiving, modifying and deleting messages.

  • CloudAMQP is at its core managed servers with RabbitMQ installed and configured. This service is an option if you are using RabbitMQ and do not want to maintain RabbitMQ installations on your own servers.

Open source examples that use task queues

  • Take a look at the code in this open source Flask application and this Django application for examples of how to use and deploy Celery with a Redis broker to send text messages with these frameworks.

  • flask-celery-example is a simple Flask application with Celery as a task queue and Redis as the broker.

Task queue resources

Task queue learning checklist

  1. Pick a slow function in your project that is called during an HTTP request.

  2. Determine if you can precompute the results on a fixed interval instead of during the HTTP request. If so, create a separate function you can call from elsewhere then store the precomputed value in the database.

  3. Read the Celery documentation and the links in the resources section below to understand how the project works.

  4. Install a message broker such as RabbitMQ or Redis and then add Celery to your project. Configure Celery to work with the installed message broker.

  5. Use Celery to invoke the function from step one on a regular basis.

  6. Have the HTTP request function use the precomputed value instead of the slow running code it originally relied upon.

Configuration Management

Configuration management involves modifying servers from an existing state to a desired state and automating how an application is deployed.

Configuration management tools

Numerous tools exist to modify server state in a controlled way, including Puppet, Chef, SaltStack, and Ansible. Puppet and Chef are written in Ruby, while SaltStack and Ansible are written in Python.

Ad hoc tasks

Configuration management tools such as Chef, Puppet, Ansible, and SaltStack are not useful for performing ad hoc tasks that require interactive responses. Fabric and Invoke are used for interactive operations, such as querying the database from the Django manage.py shell.

Configuration management tool comparisons

Ansible

Ansible is an open source configuration management and application deployment tool built in Python.

Ansible Resources

Application dependencies learning checklist

  1. Learn about configuration management in the context of deployment automation and infrastructure-as-code.

  2. Pick a configuration management tool and stick with it. My recommendation is Ansible because it is by far the easiest tool to learn and use.

  3. Read your configuration management tool's documentation and, when necessary, the source code.

  4. Automate the configuration management and deployment for your project. Note that this is by far the most time consuming step in this checklist but will pay dividends every time you deploy your project.

  5. Hook the automated deployment tool into your existing deployment process.

Continuous Integration

Continuous integration (CI) automates building, testing and deploying applications.

Why is continuous integration important?

When CI is set up well it can dramatically reduce deployment times by eliminating manual steps and ensure code does not have bugs that are being checked by automated tests. Source code changes as a project evolves. CI combined with unit and integration tests check that code modifications do not break existing tests ensure the software works as intended.

Continuous integration example

The following picture represents a high level perspective on how continuous integration and deployment can work.

One potential way for continuous integration to work.

In the above diagram, when new code is committed to a source repository there is a hook that notifies the continuous integration server that new code needs to be built (the continuous integration server could also poll the source code repository if a notification is not possible).

The continuous integration server pulls the code to build and test it. If all tests pass, the continuous integration server begins the deployment process. The new code is pulled down to the server where the deployment is taking place. Finally the deployment process is completed via restarting services and related deployment activities.

There are many other ways a continuous integration server and its deployments can be structured. The above was just one example of a relatively simple set up.

Open source CI projects

Hosted CI services

  • Travis CI provides free CI for open source projects and has a commercial version for private repositories.

  • Bamboo is Atlassian's hosted continuous integration that is also free for open source projects.

  • Circle CI works with open or closed source projects on GitHub and can deploy them to Heroku if builds are successful.

  • Shippable uses Docker containers to speed the build and integration process. It's free for public repositories.

  • Drone is another CI service that also provides free builds for open source projects.

  • Codeship provides continuous integration for Python 2.7.

  • Snap is a CI server and build pipeline tool for both integrating and deploying code.

Continuous integration resources

Logging

Logging saves output such as errors, warnings and event information to persistent storage for debugging purposes.

Why is logging important?

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 levels

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.

Logging aggregators

When you are running your application on several servers, it is helpful to have a monitoring tool called a "logging aggregator". You can configure your application to forward your system and application logs to one location that provides tools for viewing, searching, and monitoring logging events across your cluster.

Another advantage of log aggregation tools is they allow you to set up custom alerts and alarms so you can get notified when error rates breach a certain threshold.

Open source log aggregators

  • Sentry started as a Django-only exception handling service but now has separate logging clients to cover almost all major languages and frameworks. It still works really well for Python-powered web applications and is often used in conjunction with other monitoring tools. Raven is open source Python client for Sentry.

  • Graylog2 provides a central server for log aggregation as well as a GUI for browsing and searching through log events. There are libraries for most major languages, including python. Saves data in Elasticache.

  • Logstash Similar to Graylog2, logstash offers features to programmatically configure log data workflows.

  • Scribe A project written by Facebook to aggregate logs. It's designed to run on multiple servers and scale with the rest of your cluster. Uses the Thrift messaging format so it can be used with any language.

Hosted logging services

  • Loggly is a third party cloud based application that aggregates logs. They have instructions for every major language, including python. It includes email alerting on custom searches.

  • Splunk offers third party cloud and self hosted solutions for event aggregation. It excels at searching and data mining any text based data.

  • Papertrail is similar to both Loggly and Splunk and provides integration with S3 for long term storage.

  • Raygun logs errors and provides immediate notification when issues arise.

  • Scalyr provides log aggregation, dashboards, alerts and search in a user interface on top of standard logs.

  • There is a hosted version of Sentry in case you do not have the time to set up the open source project yourself.

Logging resources

Logging learning checklist

  1. Read how to integrate logging into your web application framework.

  2. Ensure errors and anomalous results are logged. While these logs can be stored in monitoring solutions, it's best to have your own log storage location to debug issues as they arise to complement other monitoring systems.

  3. Integrate logging for system events you may need to use for debugging purposes later. For example, you may want to know the return values on functions when they are above a certain threshold.

Monitoring

Monitoring tools capture, analyze and display information for a web application's execution. Every application has issues arise throughout all levels of the web stack. Monitoring tools provide transparency so developers and operations teams can respond and fix problems.

Why is monitoring necessary?

Capturing and analyzing data about your production environment is critical to proactively deal with stability, performance, and errors in a web application.

Difference between monitoring and logging

Monitoring and logging are very similar in their purpose of helping to diagnose issues with an application and aid the debugging process. One way to think about the difference is that logging happens based on explicit events while monitoring is a passive background collection of data.

For example, when an error occurs, that event is explicitly logged through code in an exception handler. Meanwhile, a monitoring agent instruments the code and gathers data not only about the logged exception but also the performance of the functions.

This distinction between logging and monitoring is vague and not necessarily the only way to look at it. Pragmatically, both are useful for maintaining a production web application.

Monitoring layers

There are several important resources to monitor on the operating system and network level of a web stack.

  1. CPU utilization
  2. Memory utilization
  3. Persistence storage consumed versus free
  4. Network bandwidth and latency

Application level monitoring encompasses several aspects. The amount of time and resources dedicated to each aspect will vary based on whether an application is read-heavy, write-heavy, or subject to rapid swings in traffic.

  1. Application warnings and errors (500-level HTTP errors)
  2. Application code performance
  3. Template rendering time
  4. Browser rendering time for the application
  5. Database querying performance

Open source monitoring projects

  • statsd is a node.js network daemon that listens for metrics and aggregates them for transfer into another service such as Graphite.

  • Graphite stores time-series data and displays them in graphs through a Django web application.

  • Bucky measures the performance of a web application from end user's browsers and sends that data back to the server for collection.

  • Sensu is an open source monitoring framework written in Ruby but applicable to any programming language web application.

  • Graph Explorer by Vimeo is a Graphite-based dashboard with added features and a slick design.

  • PacketBeat sniffs protocol packets. Elasticsearch then allows developers to search the collected data and visualize what's happening inside their web application using the Kibana user interface.

  • Munin is a client plugin-based monitoring system that sends monitoring traffic to the Munin node where the data can be analyzed and visualized. Note this project is written in Perl so Perl 5 must be installed on the node collecting the data.

Hosted monitoring services

  • New Relic. Application and database monitoring as well as plug ins for capturing and analyzing additional data about tools in your stack.

  • CopperEgg is lower-level monitoring on server and infrastructure. It's popular with DevOps shops that are making changes to their production environments and want immediate feedback on the results of those modifications.

  • Status.io focuses on uptime and response metrics transparency for web applications.

  • StatusPage.io (yes, there's both a Status and StatusPage.io) provides easy set up status pages for monitoring application up time.

  • PagerDuty alerts a designated person or group if there are stability, performance, or uptime issues with an application.

  • App Enlight provides performance, exception and error monitoring and is currently specific to Python web applications.

Monitoring resources

Monitoring learning checklist

  1. Review the software-as-a-service and open source monitoring tools below. Third party services tend to be easier to set up and host the data for you. Open source projects give you more control but you'll need to have additional servers ready for the monitoring.

  2. My recommendation is to install New Relic's free option with the trial period to see how it works with your app. It'll give you a good idea of the capabilities for application-level monitoring tools.

  3. As your app scales take a look at setting up one of the the open source monitoring projects such as StatsD with Graphite. The combination of those two projects will give you fine-grained control over the system metrics you're collecting and visualizing.

Web analytics

Web analytics involves collecting, processing, visualizing web data to enable critical thinking about how users interact with a web application.

Why is web analytics important?

User clients, especially web browsers, generate significant data while users read and interact with webpages. The data provides insight into how visitors use the site and why they stay or leave. The key concept to analytics is learning about your users so you can improve your web application to better suit their needs.

Web analytics concepts

It's easy to get overwhelmed at both the number of analytics services and the numerous types of data points collected. Focus on just a handful of metrics when you're just starting out. As your application scales and you understand more about your users add additional analytics services to gain further insight into their behavior with advanced visualizations such as heatmaps and action funnels. The seven stages of startup analytics grief post is an amusing read and provides context for how to begin and then grow tracked metrics over time.

User funnels

If your application is selling a product or service you can ultimately build a user funnel (often called "sales funnel" prior to a user becoming a customer) to better understand why people buy or don't buy what you're selling. With a funnel you can visualize drop-off points where visitors leave your application before taking some action, such as purchasing your service.

Open source web analytics projects

  • Piwik is a web analytics platform you can host yourself. Piwik is a solid choice if you cannot use Google Analytics or want to customize your own web analytics platform.

  • Open Web Analytics is another self-hosted platform that integrates through a JavaScript snippet that tracks users' interactions with the webpage.

Hosted web analytics services

  • Google Analytics is a widely used free analytics tool for website traffic.

  • Clicky provides real-time analytics comparable to Google Analytics' real-time dashboard.

  • MixPanel's analytics platform focuses on mobile and sales funnel metrics. A developer builds what data points need to be collected into the server side or client side code. MixPanel captures that data and provides metrics and visualizations based on the data.

  • KISSmetrics' analytics provides context for who is visiting a website and what actions they are taking while on the site.

  • Heap is a recently founded analytics service with a free introductory tier to get started.

  • CrazyEgg is tool for understanding a user's focus while using a website based on heatmaps generated from mouse movements.

Python-specific web analytics resources

General web analytics resources

Web analytics learning checklist

  1. Add Google Analytics or Piwik to your application. Both are free and while Piwik is not as powerful as Google Analytics you can self-host the application which is the only option in many environments.

  2. Think critically about the factors that will make your application successful. These factors will vary based on whether it's an internal enterprise app, an e-commerce site or an information-based application.

  3. Add metrics generated from your web traffic based on the factors that drive your application's success. You can add these metrics with either some custom code or with a hosted web analytics service.

  4. Continuously reevaluate whether the metrics you've chosen are still the appropriate ones defining your application's success. Improve and refine the metrics generated by the web analytics as necessary.

Docker

Docker is an open source infrastructure management platform for running and deploying software. The Docker platform is constantly evolving so an exact definition is currently a moving target.

Why is Docker important?

Docker can package up applications along with their necessary operating system dependencies for easier deployment across environments. In the long run it has the potential to be the abstraction layer that easily manages containers running on top of any type of server, regardless of whether that server is on Amazon Web Services, Google Compute Engine, Linode, Rackspace or elsewhere.

Docker resources

Python-specific Docker resources

Caching

Caching can reduce the load on servers by storing the results of common operations and serving the precomputed answers to clients.

For example, instead of retrieving data from database tables that rarely change, you can store the values in-memory. Retrieving values from an in-memory location is far faster than retrieving them from a database (which stores them on a persistent disk like a hard drive.) When the cached values change the system can invalidate the cache and re-retrieve the updated values for future requests.

A cache can be created for multiple layers of the stack.

Caching backends

  • memcached is a common in-memory caching system.

  • Redis is a key-value in-memory data store that can easily be configured for caching with libraries such as django-redis-cache.

Caching resources

  • "Caching: Varnish or Nginx?" reviews some considerations such as SSL and SPDY support when choosing reverse proxy Nginx or Varnish.

  • Caching is Hard, Draw me a Picture has diagrams of how web request caching layers work. The post is relevant reading even though the author is describing his Microsoft code as the impetus for writing the content.

  • While caching is a useful technique in many situations, it's important to also note that there are downsides to caching that many developers fail to take into consideration.

Caching learning checklist

  1. Analyze your web application for the slowest parts. It's likely there are complex database queries that can be precomputed and stored in an in-memory data store.

  2. Leverage your existing in-memory data store already used for session data to cache the results of those complex database queries. A task queue can often be used to precompute the results on a regular basis and save them in the data store.

  3. Incorporate a cache invalidation scheme so the precomputed results remain accurate when served up to the user.

Microservices

Microservices are an application architecture style where independent, self-contained programs with a single purpose each can communicate with each other over a network. Typically, these microservices are able to be deployed independently because they have strong separation of responsibilities via a well-defined specification with significant backwards compatibility to avoid sudden dependency breakage.

Why are microservices getting so much buzz?

Microservices follow in a long trend of software architecture patterns that become all the rage. Previously, CORBA and (mostly XML-based) service-oriented architectures (SOA) were the hip buzzword among ivory tower architects.

However, microservices have more substance because they are typically based on RESTful APIs that are far easier for actual software developers to use compared with the previous complicated XML-based schemas thrown around by enterprise software companies. In addition, successful applications begin with a monolith-first approach using a single, shared application codebase and deployment. Only after the application proves its usefulness is it then broken down into microservice components to ease further development and deployment. This approach is called the "monolith-first" or "MonolithFirst" pattern.

Microservice resources

  • Martin Fowler's microservices article is one of the best in-depth explanations for what microservices are and why to consider them as an architectural pattern.

  • On monoliths and microservices provides some advice on using microservices in a fairly early stage of a software project's lifecycle.

  • Developing a RESTful microservice in Python is a good story of how an aging Java project was replaced with a microservice built with Python and Flask.

  • Microservices: The essential practices first goes over what a monolith application looks like then dives into what operations you need to support potential microservices. For example, you really need to have continuous integration and deployment already set up. This is a good high-level overview of the topics many developers aren't aware of when they embark on converting a monolith to microservices.

Testing

Testing determines whether software runs correctly based on specific inputs and identifies defects that need to be fixed.

Why is testing important?

As software scales in codebase size, it's impossible for a person or even a large team to keep up with all of the changes and the interactions between the changes. Automated testing is the only proven method for building reliable software once they grow past the point of a simple prototype. Many major software program development failures can be traced back to inadequate or a complete lack of testing.

It's impossible to know whether software works properly unless it is tested. While testing can be done manually, by a user clicking buttons or typing in input, it should be performed automatically by writing software programs that test the application under test.

There are many forms of testing and they should all be used together. When a single function of a program is isolated for testing, that is called unit testing. Testing more than a single function in an application at the same time is known as integration testing. User interface testing ensures the correctness of how a user would interact with the software. There are even more forms of testing that large programs need, such as load testing, database testing, and browser testing (for web applications).

Testing in Python

Python software development culture is heavy on software testing. Because Python is a dynamically-typed language as opposed to a statically-typed language, testing takes on even greater importance for ensuring program correctness.

Testing resources

  • The Minimum Viable Test Suite shows how to set unit tests and integration tests for a Flask example application.

  • Good test, bad test explains the difference between a "good" test case and one that is not as useful. Along the way the post breaks down some myths about common testing subjects such as code coverage, assertions and mocking.

  • Python Testing is a site devoted to testing in - you guessed it - the Python programming language.

  • The case for test-driven development by Michael DeHaan explains how automation is the only way to build software at a large scale.

  • Google has a testing blog where they write about various aspects of testing software at scale.

  • Still confused about the difference between unit, functional and integration tests? Check out this top answer on Stack Overflow to that very question.

Unit testing

Unit testing is a method of determining the correctness of a single function isolated from a larger codebase. The idea is that if all the atomic units of an application work as intended in isolation, then integrating them together as intended is much easier.

Why is unit testing important?

Unit testing is just one form of testing that works in combination with other testing approaches to wring out the bugs from a piece of software being developed. When several functions and classes are put together it's often difficult to determine the source of a problem if several bugs are occurring at the same time. Unit testing helps eliminate as many of the individual bugs as possible so when the application comes together as a whole the separate parts work as correct as possible. Then when issues arise they can often be tracked down as unintended consequences of the disparate pieces not fitting together properly.

Unit testing tools

There are many tools for creating tests in Python. Some of these tools, such as pytest, replace the built-in unittest framework. Other tools, such as nose, are extensions that ease test case creation. Note that many of these tools are also used for integration testing by writing the test cases to exercise multiple parts of code at once.

  • unittest is the built-in standard library tool for testing Python code.

  • pytest is a complete testing tool that emphasizes backwards-compatibility and minimizing boilerplate code.

  • nose is an extension to unittest that makes it easier to create and execute test cases.

  • testify was a testing framework meant to replace the common unittest+nose combination. However, the team behind testify is transitioning to pytest so it's recommended you do not use testify for new projects.

Unit testing resources

Integration Testing

Integration testing exercises two or more parts of an application at once, including the interactions between the parts, to determine if they function as intended. This type of testing identifies defects in the interfaces between disparate parts of a codebase as they invoke each other and pass data between themselves.

How is integration testing different from unit testing?

While unit testing is used to find bugs in individual functions, integration testing tests the system as a whole. These two approaches should be used together, instead of doing just one approach over the other. When a system is comprehensively unit tested, it makes integration testing far easier because many of the bugs in the individual components will have already been found and fixed.

As a codebase scales up, both unit and integration testing allow developers to quickly identify breaking changes in their code. Many times these breaking changes are unintended and wouldn't be known about until later in the development cycle, potentially when an end user discovers the issue while using the software. Automated unit and integration tests greatly increase the likelihood that bugs will be found as soon as possible during development so they can be addressed immediately.

Integration testing resources

Code Metrics

Code metrics can be produced by static code analysis tools to determine complexity and non-standard practices.

Why are code metrics important?

Code metrics allow developers to find problematic codebase areas that may need refactoring. In addition, some metrics such as technical debt assist developers in communicating to non-technical audiences why issues with a system are occurring.

Open source code metrics projects

  • Radon is a tool for obtaining raw metrics on line counts, Cyclomatic Complexity, Halstead metrics and maintainability metrics.

  • Pylint contains checkers for PEP8 code style compliance, design, exceptions and many other source code analysis tools.

  • PyFlakes parses source files for errors and reports on them.

  • Pyntch is a static code analyzer that attempts to detect runtime errors. It does not perform code style checking.

Hosted code metrics services

  • Landscape provides free code metrics for open source Python projects. Pricing is available for analyzing private repositories as well.

  • Coveralls shows code coverage from test suites and other metrics to help developers improve the quality of their code.

Code metrics resources

Debugging

Developers often find themselves in situations where the code they've written is not working quite right. When that happens, a developer debugs their code by instrumenting, executing and inspecting the code to determine what state of the application does not match the assumptions of how the code should be correctly running.

Why is debugging important?

There are bugs in every modest sized or larger application. Every developer has to learn how to debug code in order to write programs that work as correctly as time and budget allow.

Debugging resources

  • Debugging your Python code walks through a scenario where pdb can be used to find a defect in a block of Python code.

  • pdb - Interactive Debugger is featured on the Python Module of the Week blog and has some great detail on using the program effectively.

  • Python debugging tools provides a list of tools such as pdb and its derivatives ipdb, pudb and pdb++ along with how they can be used in the hunt for defects.

  • Debugging in Python elaborates on what pdb does and how it can be used.

What "full stack" means

The terms "full stack" and "Full Stack Python" are ambiguous but I am using a specific definition here on this site. These term can be defined for a web stack either to mean

  1. Every layer, from the machine code up to the browser, are written in Python

  2. Python code interacts with code written in other languages such as C and JavaScript to provide a complete web stack

I named this site specifically for the second definition: Python is one programming language among many that are used to build your application stack.

Some folks took the title of the site to mean Python runs everything from the web browser on down. That's simply not practical or possible. While Python is an amazing programming language, there are many tasks it does not do well.

Python is just one language among many that allows our computers to execute software and communicate with each other.

For beginners, learning the syntax and libraries in Python necessary to build a web application or web API is a major undertaking. Even intermediate and advanced Python software developers need to constantly program and learn to keep up with our ever evolving ecosystem. I created Full Stack Python to be just one of many resources that help Python developers build and maintain their programming skills.

Change Log

This is a running list of the major changes to Full Stack Python since its inception in December 2012. Another way to view the modifications is through the source repository's commit log on GitHub.

2015

November

October

September

August

July

June

  • New API Creation and Django resources added.
  • Added new Peewee resources on the ORMs page.
  • Nice little update to the ORMs page with a diagram showing that different ORMs can work with varying web frameworks and backends.
  • Added a new section just for Nginx resources and Removed broken links on the web servers page.
  • Added a new page on Python object-relational mappers, which helped condense that information on a single page and lighten the loads on the Django and relational databases pages.
  • Added new page with a little advice on learning programming.
  • Proofread and tweaked the web frameworks page.
  • Added a new section entitled "Do I have to use a web framework?" to the web frameworks page.
  • Reviewed and updated the introduction with slight modifications.
  • Added new Docker resources.
  • Made some changes to what is contained in the Markdown files versus the HTML Jinja2 templates. Behind the scenes work that needed to be done to move the project forward.
  • Work continues on the Full Stack Python Guide to Deployments book.

May

  • Got a whole lot of work done on my upcoming Full Stack Python Guide to Deployments book. Very close to releasing it (looking at mid-June).
  • Added new Django resources, especially around migrations in Django 1.7+.
  • Worked on making Why Use Python? page specific to that topic now that the Enterprise Python page contains enterprise-y info.
  • New page on the super broad topic of Data with Python. Eventually it'll link to all sorts of data topics such as analysis, visualization and processing.
  • New page on Enterprise Python. A bit op-ed-ish perhaps, but I think it captures some of the spirit of the open source ecosystem with regards to Python for enterprise software development.
  • Added additional Django resources, specifically related to testing.

April

  • Added more NoSQL resources, especially ones involving Redis.
  • New Pyramid resource where the primary author is interviewed about the web framework.
  • New Vim resources.
  • Updated the Django page with new resources. The page is getting unwieldy at its current size so I'll likely pare it down with better categorizes sometime soon.
  • Added new resources on the Flask page.

March

  • Added new source control resources.
  • Major site performance improvements. I removed font awesome and replaced icons with SVG elements that mimic the font awesome icons.
  • Pushed the 1000th commit to Full Stack Python! Screenshot of 1000th commit to Full Stack Python.
  • Major update to Vim page to add screenshots, a better example .vimrc configuration and many more resources.
  • Updated the web analytics page with a new Python-specific section for walkthroughs that are specific to building or using analytics with Python applications.
  • Adding a slew of new Django resources.
  • Working on including Crossbar.io and Autobahn to the websockets page.
  • Added the Muffin framework to the other web frameworks page.
  • Added new Emacs page based on pull request #49 base information. Thank you!
  • Added a new page on best Python videos that breaks out all the videos I had scattered around the site and puts the best ones in a single place.
  • Beefing up the Django resources and will soon break them further down into subcategories so they are easier to find by topic.

February

  • Cleaned up the sidebar and email subscribe messaging.
  • Added new monitoring, websockets and continuous integration resources.
  • New Django resources.
  • Updated the Vim page with a Vimrc configuration file explanation along with Vimrc resources.
  • Added a generators page that's mostly a stub that I will be building out to explain this core language feature.
  • Updated table of contents with new layout that'll allow me to expand on core Python language topics.
  • Updated several out of date resources and added a new logging resource.
  • New Pyramid resources. I definitely need to flesh that page out further.
  • Added a Vim page and resources for learning Vim as a Python code editor.
  • Reorganized content pages to make for better logical groupings as I add new content.
  • Major improvements to Websockets page after suggestions from issue #47 on GitHub repository.

January

  • Rewrote the Mailchimp sign up form for the email list so it doesn't have the external JQuery libraries as dependencies. Site should be even faster now.
  • Stripped a significant portion of unused Bootstrap boilerplate from the CSS file and minified it. The resulting CSS file is over 100KB less (about 25KB down from 130KB) so the site should load faster now.
  • Major update to WebSockets page with new diagrams and better explanations for why server push is useful.
  • New task queue resources.
  • Major update with the beginning of a page on Docker, split out static file handling resources on the Django page and a new section on Python programming language popularity on the "Why Use Python?" page.
  • Working on a Why Use Python? page with my own assessment of the strengths and weaknesses of Python along with links to resources where other folks discuss their own experiences.
  • Continuing to add WebSockets resources, especially Python-specific ones.
  • Added a new separate page for the Morepath framework.
  • Updated the future directions page for 2015.
  • Added new WebSockets resources.
  • Added WebSockets page and some initial resources.

2014

December

  • Added new security resources and splitting HTTPS resources into their own section.
  • Split out Djangular resources into a separate section.
  • New NoSQL Python client resources.
  • Added new API resources for integration and creation.

November

  • Added a nice new continuous integration diagram.
  • More Django and database resources.
  • Revising development environments page and adding new resources.
  • Adding my new Flask blog post on choose your own adventure presentations along with the open source repository.
  • More resources under Best Python Resources.
  • Removing broken links from Best Python Resources and Django pages.
  • New monitoring and development environments resources.

October

  • Added the first new page in awhile! All about development environments.
  • Always adding new links to the best resources. More resources for deployments, web analytics and Flask.
  • More API creation and consumption resources.
  • Merged a bunch of pull requests that cleaned up spelling and grammar errors. Thank you contributors!
  • Adding new Django 1.7-specific resources section on the Django page.
  • New web frameworks, Bottle and Flask resources.

September

  • New resources for Flask, Django and task queues sections.
  • A few new resources for NoSQL data stores.

August

  • New interesting link for web framework code complexity visualizations.
  • Merged pull request that fixed some issues on WSGI page.
  • Updated table of contents so it's easier to read and broken down by sections.
  • Added new Web Design page to cleanly separate CSS from design topics and resources.
  • New resources for code metrics and NoSQL databases.
  • Added another Flask open source example app.
  • Added new Code Metrics page.
  • Updated CI page with more services and open source projects.
  • Added Continuous Integration page.
  • Splitting out deployment from automation so I can add chapters on continuous integration, configuration management (which will be moved from the existing deployment chapter) and related topics.
  • Small tweaks to NoSQL, introduction and a few other pages.
  • Moved topics map from introduction page to deployment page.

July

  • Merged pull request for Django page and updated Django page with project template section.
  • New Django example project resources.
  • Rewrote parts of source control and CSS pages for clarity.
  • Merged typo fixes PR #32.
  • Added my Full Stack Python video from the EuroPython 2014 conference.
  • Updated map with further details.
  • Added a map to the front page. May move that elsewhere later though based on feedback.
  • Merged a pull request for new REST frameworks.
  • Lots of new Django, Flask and task queue resources.
  • Added two new Python libraries lists to the Best Python Resources page.
  • Thanks Hacker News for doubling my traffic so far in 2014! 65k uniques and counting...

June

  • Added more monitoring and logging resources.
  • New resources for Flask and NoSQL projects.
  • Updated NoSQL data store page with specific open source projects.
  • Added diagram to source control page.
  • Split version control resources from Git resources. Added new version control resources.
  • Updated logging page with better explanations and content ordering.
  • Added learning checklists for all sections. The remaining sections that now also have checklists are logging, web analytics and web application security.

May

  • Added link to my O'Reilly Programming blog post on demand for full stack developer capabilities.
  • Updated APIs page with basic information on webhooks.
  • Added learning checklists for source control, application dependencies, configuration management, NoSQL data stores, APIs, API integration, API creation, static content and caching sections.
  • Moving learning checklists to the bottom of the pages since they are specific advice for steps to take after reading a section.
  • Added a stub section for APIs.
  • Cleaned up and polished the task queues and web analytics pages.
  • Added learning checklist to operating systems, web servers, task queues, monitoring pages and WSGI servers.
  • Adding more logging resources.
  • Continuing to add learning checklists to sections such as servers.
  • Moving navigation options into meta tags on markdown pages.

April

  • Adding the concept of "learning checklists" to web frameworks, Django, CSS and JavaScript pages to give readers some guidance for how to learn each topic. Will expand these checklists out into other pages over the next couple of weeks.
  • Added an email sign up form to determine how many people are interested in a full book since I've had a lot of requests in person to write one.
  • Added new resources to the other web frameworks section.
  • Updated the way choices to go from one page to another are generated. It's now done off metadata instead of duplicated HTML content.
  • Huge site update to reorganize the way content is presented and navigated. Kinda has that "choose your own adventure" thing going for it, doesn't it?
  • New logo! This one's way more Python software stack, way less boring folder-thingy. Here's how the old one looked in comparison: Old Full Stack Python logo

  • Added a future direction section to explain current priorities for further developments on the site.

  • More resources for web frameworks and configuration management sections.
  • Added small JavaScript section. Updating witih basic resources.
  • Updated application dependencies with new links to Python library collections.
  • Merged a couple of awesome pull requests that fixed typos and added additional Bottle resources.

March

  • Updated logging page with new resources.
  • Added new CSS page.
  • New intermediate learning links on the best resources page.
  • Updated task queues page with better explanations and many more curated resources.
  • Added why is this piece necessary for databases, WSGI servers, web frameworks and application dependencies.
  • Updating best resources page with newsletters and a few additional beyond the basics resources.
  • Adding 'why is this necessary' sections to servers, operating systems, and web servers pages.
  • Extracting best Python resources from the introduction into a separate page.
  • Cleaned up links on the first ten chapters and added new resources for web frameworks.
  • Updated application dependencies section with new resources and initial content description.
  • Updated the change log (how meta!) to have a cleaner layout.

February

  • Added Bottle as a web framework next to Django and Flask.
  • Added new Django resources.
  • New sitemap.xml.
  • Rewriting all sections to fix first draft typos and grammar mistakes as well as add new content.
  • Added task queues section due to reader feedback.
  • Rewrote intro section.
  • Merged several pull requests (see closed GitHub repo pull requests).
  • New resources for platform-as-a-service section.
  • Added new sections specified by the community as missing.
  • Reorganized ordering of content.
  • Broke out subsections for Django and Flask.
  • Added signficant content to the WSGI section.
  • Converted from RST to Markdown (some of the downstream tools I want to use work better with Markdown than RST).
  • Reorganized content into rough outline of "final" chapters.

January

  • Added configuration management, application dependencies, and source control sections.
  • Updated about section.
  • Fully responsive web design.

2013

December

  • Changed CDN section to static content section.
  • Transitioned diagrams from Paper app drawings to Balsamiq mockups exported to PNG files.
  • Added Python database connectors to database section.

November

  • Modified color scheme.
  • Updated caching and introduction section.
  • Added NoSQL data stores section.

October

  • Created separate monitoring section.

August

  • Added more resources for web servers and other categories.

June

  • Updated styling.
  • Switching around several sections.

January

  • Fleshed out web server, OS, and server sections, particularly IaaS and PaaS topics.
  • Added initial "hand drawn" diagram placeholders for better diagrams later.

2012

December

  • Initial incomplete release on fullstackpython.com, created introduction, CDN, web frameworks, and database sections with stubs for other areas.

Future Directions

Full Stack Python has completely blown away my expectations for what I could accomplish with a side project. I really appreciate all the in-person feedback, emails and pull requests I've received from the community. Keep them coming!

For 2015 I'm building out the scope of the site beyond web development into core Python concepts, data analysis and visualization and some hardware hacking such as with the Raspberry Pi and Arduino Yun.

The biggest update though will come with the release of The Full Stack Python Guide to Deployments, a step-by-step tutorial book for learning how to deploy Python web applications.

Note that these plans can change based on pull requests from the community. I work to integrate PRs within a day or two so please submit one when you see a fix or improvement that needs to be made!

About the Author

This website was coded and written by Matt Makai (@mattmakai), currently a Developer Evangelist at Twilio.

Other projects by Matt include The Full Stack Python Guide to Deployments book, Coding Across America, Underwear and Choose Your Own Adventure Presentations. You can reach him by email at matthew.makai@gmail.com, Twitter @mattmakai or on GitHub at makaimc.

Read my thoughts on the "full stack" trend in a post I wrote for O'Reilly Radar.

Typos, inaccurate statements or general areas for improvement can be handled through an issue ticket or pull request on GitHub.