Comprehensive Guide to Python Web Development

I was inspired to make this guide by three internet personalities.

  1. Miguel Grinberg and his excellent, comprehensive Python Flask tutorial
  2. Luke Smith and his admonition against internet serfdom
  3. Nat Eliason and his honest explanation of blogging success

I have every intention of making this one article as comprehensive as possible, referencing all the sources that helped me out along the way. Any number of people could explain these topics in more depth and with more credibility, so don’t look to me as an authority on best-practice.

My aim here is breadth. I want to compile all the notes I would want to reference if I was starting from scratch and, to some extent, demonstrate my showcase my knowledge and employability.


Programming in General

Information to come from Sebastian Lague’s excellent Exploring How Computers Work video

Python

In the following section I will present the basics of getting started with Python and introduce web development concepts near the end.

Introduction

Python is a dynamically typed, interpreted programming language that supports multiple programming paradigms including functional and object-oriented.

Let’s unpack these terms.

Dynamically typed

This programming language does not expect the user to explicitly declare data types. Specifically, the programmer does not specify that a number should be stored and treated as an integer or a floating point number. Instead, the Python interpreter infers the type of each variable and manages storage and retrieval on its own.

Interpreted programming language

Programming languages can be compiled, interpreted at runtime, or some combination of the two in what is commonly referred to as a just-in-time compiler. At the end of the day all source code needs to be translated into machine code, the ones and zeros that computers use to operate. Interpreted languages like Python are generally much slower to execute in exchange for the convenience of not having a separate compilation step. As a result, Python code is largely platform independent and simple to execute. A separate, but related, consequence is that Python is easier to debug using the built-in REPL (read, evaluate, print, loop). You can test code interactively by typing it in line-by-line.

Functional Programming

A style of programming emphasizing code-reuse through the compartmentalization of larger, specific operations into functions.

Object-Oriented Programming

A higher-level style of programming emphasizing the encapsulation of data and the methods used to operate on that data into a abstract model loosely matching intuitive relationships present in the real-world object. For example, an animal object may have attributes (variables associated with it) for name, number of legs, or fur color and may have methods (functions associated with it) for returning how high the animal can jump considering its number of legs.

Python has grown to widespread use on account of its easy to read, easy to write syntax, cross-compatibility, availability of quality open-source libraries, and culture of fantastic documentation.

This tutorial uses Python 3, which is the standard version in use now that Python 2.X has been phased out.

The classic hello world:

print('Hello World!')

Python emphasizes readability because it uses whitespace and indentation to segment programming blocks rather than brackets.

response = input('What is your name?')

if response == 'Bob':
    print('You have been banned.')
else:
  print('Welcome', response, '!')

Getting Started

Python for Web Development

There are several popular frameworks for creating websites using Python. The main ones are:

  • Django
  • Flask
  • Aiohttp

Of these I have personally used Flask and Aiohttp. Flask is the easier of the two to wrap your head around.

html

javascript

http, http apis, and python-requests

version control: git and github

virtual environments

pandoc

Note that indentation can be messed up depending on the pandoc template, but this can be fixed by dedenting body in the pandoc templates

highlighting code

I use the fantastic highlight.js using the Stack Overflow light theme. This requires the addition of a couple lines in the base template. This is how it looks using the Flask utilities and Jinja2 templating blocks.

{% block styles %}
{{ bootstrap.load_css() }}
{{ render_static('css', 'css/main.css') }}
{{ render_static('css', 'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css', local=False) }}
{% endblock %}

...

{% block scripts %}
{{ bootstrap.load_js() }}
<script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@10.5.0/build/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
{% endblock %}

...

(Note that Jinja2 code samples will be cause errors unless they are wrapped in “raw” blocks)

You can choose more themes by guessing the CDN links from here.

sources

the big three

the rest

Logan Frederick - 2020